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: send implicit headers on HEAD with no body #48108

Merged
merged 2 commits into from
May 26, 2023
Merged
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
22 changes: 11 additions & 11 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,17 +887,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 @@ -930,6 +919,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.
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a spurious change introduced by the previous PR.

});
server.listen(0);

Expand Down