http: do not send 0\r\n\r\n
in TE HEAD responses
When replying to a HEAD request, do not attempt to send the trailers and EOF sequence (`0\r\n\r\n`). The HEAD request MUST not have body. Quote from RFC: The presence of a message body in a response depends on both the request method to which it is responding and the response status code (Section 3.1.2). Responses to the HEAD request method (Section 4.3.2 of [RFC7231]) never include a message body because the associated response header fields (e.g., Transfer-Encoding, Content-Length, etc.), if present, indicate only what their values would have been if the request method had been GET (Section 4.3.1 of [RFC7231]). fix #8361 Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
This commit is contained in:
parent
6e689ece46
commit
1fddc1fee8
@ -945,6 +945,10 @@ OutgoingMessage.prototype.end = function(data, encoding) {
|
|||||||
if (encoding === 'hex' || encoding === 'base64')
|
if (encoding === 'hex' || encoding === 'base64')
|
||||||
hot = false;
|
hot = false;
|
||||||
|
|
||||||
|
// Transfer-encoding: chunked responses to HEAD requests
|
||||||
|
if (this._hasBody && this.chunkedEncoding)
|
||||||
|
hot = false;
|
||||||
|
|
||||||
if (hot) {
|
if (hot) {
|
||||||
// Hot path. They're doing
|
// Hot path. They're doing
|
||||||
// res.writeHead();
|
// res.writeHead();
|
||||||
@ -982,7 +986,7 @@ OutgoingMessage.prototype.end = function(data, encoding) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!hot) {
|
if (!hot) {
|
||||||
if (this.chunkedEncoding) {
|
if (this._hasBody && this.chunkedEncoding) {
|
||||||
ret = this._send('0\r\n' + this._trailer + '\r\n', 'ascii');
|
ret = this._send('0\r\n' + this._trailer + '\r\n', 'ascii');
|
||||||
} else {
|
} else {
|
||||||
// Force a flush, HACK.
|
// Force a flush, HACK.
|
||||||
|
@ -26,32 +26,44 @@ var util = require('util');
|
|||||||
|
|
||||||
|
|
||||||
var body = 'hello world\n';
|
var body = 'hello world\n';
|
||||||
|
var id = 0;
|
||||||
|
|
||||||
var server = http.createServer(function(req, res) {
|
function test(headers) {
|
||||||
common.error('req: ' + req.method);
|
var port = common.PORT + id++;
|
||||||
res.writeHead(200, {'Content-Length': body.length});
|
|
||||||
res.end();
|
|
||||||
server.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
var gotEnd = false;
|
var server = http.createServer(function(req, res) {
|
||||||
|
console.error('req: %s headers: %j', req.method, headers);
|
||||||
server.listen(common.PORT, function() {
|
res.writeHead(200, headers);
|
||||||
var request = http.request({
|
res.end();
|
||||||
port: common.PORT,
|
server.close();
|
||||||
method: 'HEAD',
|
|
||||||
path: '/'
|
|
||||||
}, function(response) {
|
|
||||||
common.error('response start');
|
|
||||||
response.on('end', function() {
|
|
||||||
common.error('response end');
|
|
||||||
gotEnd = true;
|
|
||||||
});
|
|
||||||
response.resume();
|
|
||||||
});
|
});
|
||||||
request.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('exit', function() {
|
var gotEnd = false;
|
||||||
assert.ok(gotEnd);
|
|
||||||
|
server.listen(port, function() {
|
||||||
|
var request = http.request({
|
||||||
|
port: port,
|
||||||
|
method: 'HEAD',
|
||||||
|
path: '/'
|
||||||
|
}, function(response) {
|
||||||
|
console.error('response start');
|
||||||
|
response.on('end', function() {
|
||||||
|
console.error('response end');
|
||||||
|
gotEnd = true;
|
||||||
|
});
|
||||||
|
response.resume();
|
||||||
|
});
|
||||||
|
request.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('exit', function() {
|
||||||
|
assert.ok(gotEnd);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test({
|
||||||
|
'Transfer-Encoding': 'chunked'
|
||||||
|
});
|
||||||
|
test({
|
||||||
|
'Content-Length': body.length
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user