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 <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 9b08c45 commit 8b38dfb

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

lib/_http_outgoing.js

+1
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
return true;
577578
}
578579

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

+24-22
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@
33
const common = 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
const assert = require('assert');
99
const http = require('http');
1010
const stream = require('stream');
1111

12-
const expected = ['a', 'b', '', Buffer.alloc(0), 'c'];
13-
const results = [];
12+
for (const method of ['GET, HEAD']) {
13+
const expected = ['a', 'b', '', Buffer.alloc(0), 'c'];
14+
const results = [];
1415

15-
const writable = new stream.Writable({
16-
write(chunk, encoding, callback) {
17-
setImmediate(callback);
18-
}
19-
});
16+
const writable = new stream.Writable({
17+
write(chunk, encoding, callback) {
18+
callback();
19+
}
20+
});
2021

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

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

29-
for (const chunk of expected) {
30-
res.write(chunk, () => {
31-
results.push(chunk);
32-
});
33-
}
30+
for (const chunk of expected) {
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)