http: don't write empty data on req/res end()

When calling OutgoingMessage.end() with empty data argument, avoid
writing to the socket unless there's still pending data to be sent.

Fixes: https://github.com/nodejs/node/issues/41062

PR-URL: https://github.com/nodejs/node/pull/41116
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Santiago Gimeno authored and Danielle Adams committed 2022-01-31 23:08:57 -05:00
1 parent 5356dfd296
commit 0bf829192a
2 files changed
+5 -4

No files matched your search

+3 -2
View File
@@ -878,9 +878,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
if (this._hasBody && this.chunkedEncoding) {
this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
} else {
// Force a flush, HACK.
} else if (!this._headerSent || this.writableLength || chunk) {
this._send('', 'latin1', finish);
} else {
process.nextTick(finish);
}
if (this.socket) {
@@ -42,11 +42,11 @@ Connection: close
// parser.finish() to be called while we are here in the 'continue'
// callback, which is inside a parser.execute() call.
assert.strictEqual(chunk.length, 0);
assert.strictEqual(chunk.length, 4);
clientSide.destroy(new Error('sometimes the code just doesn’t work'), cb);
});
req.on('error', common.mustCall());
req.end();
req.end('data');
sync = false;
}));