Skip to content

Commit 60ce2bc

Browse files
mcollinatargos
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 <[email protected]> PR-URL: #48108 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent 801573b commit 60ce2bc

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

lib/_http_outgoing.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -887,17 +887,6 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
887887
err = new ERR_STREAM_DESTROYED('write');
888888
}
889889

890-
if (!msg._hasBody) {
891-
if (msg[kRejectNonStandardBodyWrites]) {
892-
throw new ERR_HTTP_BODY_NOT_ALLOWED();
893-
} else {
894-
debug('This type of response MUST NOT have a body. ' +
895-
'Ignoring write() calls.');
896-
process.nextTick(callback);
897-
return true;
898-
}
899-
}
900-
901890
if (err) {
902891
if (!msg.destroyed) {
903892
onError(msg, err, callback);
@@ -930,6 +919,17 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
930919
msg._implicitHeader();
931920
}
932921

922+
if (!msg._hasBody) {
923+
if (msg[kRejectNonStandardBodyWrites]) {
924+
throw new ERR_HTTP_BODY_NOT_ALLOWED();
925+
} else {
926+
debug('This type of response MUST NOT have a body. ' +
927+
'Ignoring write() calls.');
928+
process.nextTick(callback);
929+
return true;
930+
}
931+
}
932+
933933
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
934934
msg.socket.cork();
935935
process.nextTick(connectionCorkNT, msg.socket);
Lines changed: 27 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)