Skip to content

Commit 9946887

Browse files
committed
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: #41062
1 parent f3fbeaf commit 9946887

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

lib/_http_outgoing.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
878878

879879
if (this._hasBody && this.chunkedEncoding) {
880880
this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
881-
} else {
882-
// Force a flush, HACK.
881+
} else if (!this._headerSent || this.outputSize || chunk) {
883882
this._send('', 'latin1', finish);
883+
} else {
884+
process.nextTick(finish);
884885
}
885886

886887
if (this.socket) {

test/parallel/test-http-sync-write-error-during-continue.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ Connection: close
4242
// parser.finish() to be called while we are here in the 'continue'
4343
// callback, which is inside a parser.execute() call.
4444

45-
assert.strictEqual(chunk.length, 0);
45+
assert.strictEqual(chunk.length, 4);
4646
clientSide.destroy(new Error('sometimes the code just doesn’t work'), cb);
4747
});
4848
req.on('error', common.mustCall());
49-
req.end();
49+
req.end('data');
5050

5151
sync = false;
5252
}));

0 commit comments

Comments
 (0)