Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: call write callback even if there is no message body #27777

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
if (!msg._hasBody) {
debug('This type of response MUST NOT have a body. ' +
'Ignoring write() calls.');
if (callback) process.nextTick(callback);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could replace chunk with an empty chunk and continue without bailing out, but I think it makes sense to short circuit here as all chunks are ignored.

return true;
}

Expand Down
46 changes: 24 additions & 22 deletions test/parallel/test-http-outgoing-message-write-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@
const common = require('../common');

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

const assert = require('assert');
const http = require('http');
const stream = require('stream');

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

const writable = new stream.Writable({
write(chunk, encoding, callback) {
setImmediate(callback);
}
});
const writable = new stream.Writable({
write(chunk, encoding, callback) {
callback();
}
});

const res = new http.ServerResponse({
method: 'GET',
httpVersionMajor: 1,
httpVersionMinor: 1
});
const res = new http.ServerResponse({
method: method,
httpVersionMajor: 1,
httpVersionMinor: 1
});

res.assignSocket(writable);
res.assignSocket(writable);

for (const chunk of expected) {
res.write(chunk, () => {
results.push(chunk);
});
}
for (const chunk of expected) {
res.write(chunk, () => {
results.push(chunk);
});
}

res.end(common.mustCall(() => {
assert.deepStrictEqual(results, expected);
}));
res.end(common.mustCall(() => {
assert.deepStrictEqual(results, expected);
}));
}