Skip to content

Commit 8b38dfb

Browse files
lpincatargos
authored andcommitted
http: call write callback even if there is no message body
Ensure that the callback of `OutgoingMessage.prototype.write()` is called when `outgoingMessage._hasBody` is `false` (HEAD method, 204 status code, etc.). Refs: #27709 PR-URL: #27777 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 9b08c45 commit 8b38dfb

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

‎lib/_http_outgoing.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
573573
if(!msg._hasBody){
574574
debug('This type of response MUST NOT have a body. '+
575575
'Ignoring write() calls.');
576+
if(callback)process.nextTick(callback);
576577
returntrue;
577578
}
578579

‎test/parallel/test-http-outgoing-message-write-callback.js‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@
33
constcommon=require('../common');
44

55
// This test ensures that the callback of `OutgoingMessage.prototype.write()` is
6-
// called also when writing empty chunks.
6+
// called also when writing empty chunks or when the message has no body.
77

88
constassert=require('assert');
99
consthttp=require('http');
1010
conststream=require('stream');
1111

12-
constexpected=['a','b','',Buffer.alloc(0),'c'];
13-
constresults=[];
12+
for(constmethodof['GET, HEAD']){
13+
constexpected=['a','b','',Buffer.alloc(0),'c'];
14+
constresults=[];
1415

15-
constwritable=newstream.Writable({
16-
write(chunk,encoding,callback){
17-
setImmediate(callback);
18-
}
19-
});
16+
constwritable=newstream.Writable({
17+
write(chunk,encoding,callback){
18+
callback();
19+
}
20+
});
2021

21-
constres=newhttp.ServerResponse({
22-
method: 'GET',
23-
httpVersionMajor: 1,
24-
httpVersionMinor: 1
25-
});
22+
constres=newhttp.ServerResponse({
23+
method: method,
24+
httpVersionMajor: 1,
25+
httpVersionMinor: 1
26+
});
2627

27-
res.assignSocket(writable);
28+
res.assignSocket(writable);
2829

29-
for(constchunkofexpected){
30-
res.write(chunk,()=>{
31-
results.push(chunk);
32-
});
33-
}
30+
for(constchunkofexpected){
31+
res.write(chunk,()=>{
32+
results.push(chunk);
33+
});
34+
}
3435

35-
res.end(common.mustCall(()=>{
36-
assert.deepStrictEqual(results,expected);
37-
}));
36+
res.end(common.mustCall(()=>{
37+
assert.deepStrictEqual(results,expected);
38+
}));
39+
}

0 commit comments

Comments
 (0)