Skip to content

Commit 0bf8291

Browse files
santigimenodanielleadams
authored andcommitted
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 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>
1 parent 5356dfd commit 0bf8291

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

‎lib/_http_outgoing.js‎

Lines changed: 3 additions & 2 deletions
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+
}elseif(!this._headerSent||this.writableLength||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‎

Lines changed: 2 additions & 2 deletions
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(newError('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)