diff --git a/lib/http.js b/lib/http.js index 0623668c9de..f87df173b31 100644 --- a/lib/http.js +++ b/lib/http.js @@ -945,6 +945,10 @@ OutgoingMessage.prototype.end = function(data, encoding) { if (encoding === 'hex' || encoding === 'base64') hot = false; + // Transfer-encoding: chunked responses to HEAD requests + if (this._hasBody && this.chunkedEncoding) + hot = false; + if (hot) { // Hot path. They're doing // res.writeHead(); @@ -982,7 +986,7 @@ OutgoingMessage.prototype.end = function(data, encoding) { } if (!hot) { - if (this.chunkedEncoding) { + if (this._hasBody && this.chunkedEncoding) { ret = this._send('0\r\n' + this._trailer + '\r\n', 'ascii'); } else { // Force a flush, HACK. diff --git a/test/simple/test-http-head-request.js b/test/simple/test-http-head-request.js index 60982abcc00..ca0f13a1a28 100644 --- a/test/simple/test-http-head-request.js +++ b/test/simple/test-http-head-request.js @@ -26,32 +26,44 @@ var util = require('util'); var body = 'hello world\n'; +var id = 0; -var server = http.createServer(function(req, res) { - common.error('req: ' + req.method); - res.writeHead(200, {'Content-Length': body.length}); - res.end(); - server.close(); -}); +function test(headers) { + var port = common.PORT + id++; + + var server = http.createServer(function(req, res) { + console.error('req: %s headers: %j', req.method, headers); + res.writeHead(200, headers); + res.end(); + server.close(); + }); + + var gotEnd = false; -var gotEnd = false; - -server.listen(common.PORT, function() { - var request = http.request({ - port: common.PORT, - method: 'HEAD', - path: '/' - }, function(response) { - common.error('response start'); - response.on('end', function() { - common.error('response end'); - gotEnd = true; + 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(); }); - response.resume(); + request.end(); }); - request.end(); -}); -process.on('exit', function() { - assert.ok(gotEnd); + process.on('exit', function() { + assert.ok(gotEnd); + }); +} + +test({ + 'Transfer-Encoding': 'chunked' +}); +test({ + 'Content-Length': body.length });