Skip to content

Commit

Permalink
http: don't write empty data on req/res end()
Browse files Browse the repository at this point in the history
When calling OutgoingMessage.end() with empty data argument, avoid
writing to the socket unless there's still pending data to be sent.

Fixes: #41062

PR-URL: #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>
  • Loading branch information
santigimeno authored and danielleadams committed Dec 13, 2021
1 parent e6dde46 commit a8cb89e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http-sync-write-error-during-continue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));
Expand Down

0 comments on commit a8cb89e

Please sign in to comment.