Skip to content

Commit

Permalink
http: send implicit headers on HEAD with no body
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mcollina authored and MoLow committed Jul 6, 2023
1 parent 24abe07 commit 709e368
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,17 +883,6 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
err = new ERR_STREAM_DESTROYED('write');
}

if (!msg._hasBody) {
if (msg[kRejectNonStandardBodyWrites]) {
throw new ERR_HTTP_BODY_NOT_ALLOWED();
} else {
debug('This type of response MUST NOT have a body. ' +
'Ignoring write() calls.');
process.nextTick(callback);
return true;
}
}

if (err) {
if (!msg.destroyed) {
onError(msg, err, callback);
Expand Down Expand Up @@ -926,6 +915,17 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
msg._implicitHeader();
}

if (!msg._hasBody) {
if (msg[kRejectNonStandardBodyWrites]) {
throw new ERR_HTTP_BODY_NOT_ALLOWED();
} else {
debug('This type of response MUST NOT have a body. ' +
'Ignoring write() calls.');
process.nextTick(callback);
return true;
}
}

if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
msg.socket.cork();
process.nextTick(connectionCorkNT, msg.socket);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const http = require('http');

// This test is to make sure that when the HTTP server
// responds to a HEAD request with data to res.end,
// it does not send any body but the response is sent
// anyway.

const server = http.createServer(function(req, res) {
res.end('FAIL'); // broken: sends FAIL from hot path.
});
server.listen(0);

server.on('listening', common.mustCall(function() {
const req = http.request({
port: this.address().port,
method: 'HEAD',
path: '/'
}, common.mustCall(function(res) {
res.on('end', common.mustCall(function() {
server.close();
}));
res.resume();
}));
req.end();
}));
2 changes: 1 addition & 1 deletion test/parallel/test-http-head-response-has-no-body-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const http = require('http');

const server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();
res.end('FAIL'); // broken: sends FAIL from hot path.
});
server.listen(0);

Expand Down

0 comments on commit 709e368

Please sign in to comment.