Skip to content

Commit 973153d

Browse files
reidkoichik
authored andcommitted
Properly respond to HEAD during end(body) hot path
During write(), _hasBody is checked to make sure a body is allowed -- this is now also checked during end(body) when write() isn't used. Concise final chunk for HEAD req's res.end(data). Instead of simply clearing data, check _hasBody earlier to avoid sending cruft when chunkedEncoding is used. Fixes #1291.
1 parent 9f9a4cb commit 973153d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/http.js

+7
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ OutgoingMessage.prototype.end = function(data, encoding) {
652652
this._implicitHeader();
653653
}
654654

655+
if (data && !this._hasBody) {
656+
console.error('This type of response MUST NOT have a body. ' +
657+
'Ignoring data passed to end().');
658+
data = false;
659+
}
660+
655661
var ret;
656662

657663
var hot = this._headerSent === false &&
@@ -667,6 +673,7 @@ OutgoingMessage.prototype.end = function(data, encoding) {
667673
// res.writeHead();
668674
// res.end(blah);
669675
// HACKY.
676+
670677
if (this.chunkedEncoding) {
671678
var l = Buffer.byteLength(data, encoding).toString(16);
672679
ret = this.connection.write(this._header + l + CRLF +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var http = require('http');
26+
27+
// This test is to make sure that when the HTTP server
28+
// responds to a HEAD request with data to res.end,
29+
// it does not send any body.
30+
31+
var server = http.createServer(function(req, res) {
32+
res.writeHead(200);
33+
res.end('FAIL'); // broken: sends FAIL from hot path.
34+
});
35+
server.listen(common.PORT);
36+
37+
var responseComplete = false;
38+
39+
server.addListener('listening', function() {
40+
var req = http.createClient(common.PORT).request('HEAD', '/');
41+
common.error('req');
42+
req.end();
43+
req.addListener('response', function(res) {
44+
common.error('response');
45+
res.addListener('end', function() {
46+
common.error('response end');
47+
server.close();
48+
responseComplete = true;
49+
});
50+
});
51+
});
52+
53+
process.addListener('exit', function() {
54+
assert.ok(responseComplete);
55+
});

0 commit comments

Comments
 (0)