Skip to content

Commit 709e368

Browse files
mcollinaMoLow
authored andcommitted
http: send implicit headers on HEAD with no body
If we respond to a HEAD request with a body, we ignore all writes. However, we must still include all implicit headers. Fixes a regressions introduced in #47732. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #48108 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 24abe07 commit 709e368

3 files changed

+39
-12
lines changed

lib/_http_outgoing.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -883,17 +883,6 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
883883
err = new ERR_STREAM_DESTROYED('write');
884884
}
885885

886-
if (!msg._hasBody) {
887-
if (msg[kRejectNonStandardBodyWrites]) {
888-
throw new ERR_HTTP_BODY_NOT_ALLOWED();
889-
} else {
890-
debug('This type of response MUST NOT have a body. ' +
891-
'Ignoring write() calls.');
892-
process.nextTick(callback);
893-
return true;
894-
}
895-
}
896-
897886
if (err) {
898887
if (!msg.destroyed) {
899888
onError(msg, err, callback);
@@ -926,6 +915,17 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
926915
msg._implicitHeader();
927916
}
928917

918+
if (!msg._hasBody) {
919+
if (msg[kRejectNonStandardBodyWrites]) {
920+
throw new ERR_HTTP_BODY_NOT_ALLOWED();
921+
} else {
922+
debug('This type of response MUST NOT have a body. ' +
923+
'Ignoring write() calls.');
924+
process.nextTick(callback);
925+
return true;
926+
}
927+
}
928+
929929
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
930930
msg.socket.cork();
931931
process.nextTick(connectionCorkNT, msg.socket);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
5+
// This test is to make sure that when the HTTP server
6+
// responds to a HEAD request with data to res.end,
7+
// it does not send any body but the response is sent
8+
// anyway.
9+
10+
const server = http.createServer(function(req, res) {
11+
res.end('FAIL'); // broken: sends FAIL from hot path.
12+
});
13+
server.listen(0);
14+
15+
server.on('listening', common.mustCall(function() {
16+
const req = http.request({
17+
port: this.address().port,
18+
method: 'HEAD',
19+
path: '/'
20+
}, common.mustCall(function(res) {
21+
res.on('end', common.mustCall(function() {
22+
server.close();
23+
}));
24+
res.resume();
25+
}));
26+
req.end();
27+
}));

test/parallel/test-http-head-response-has-no-body-end.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const http = require('http');
2929

3030
const server = http.createServer(function(req, res) {
3131
res.writeHead(200);
32-
res.end();
32+
res.end('FAIL'); // broken: sends FAIL from hot path.
3333
});
3434
server.listen(0);
3535

0 commit comments

Comments
 (0)