Skip to content

Commit 954a369

Browse files
josephhackmandanielleadams
authored andcommitted
http: make HEAD method to work with keep-alive
Fixes: #28438 PR-URL: #34231 Reviewed-By: James M Snell <[email protected]>
1 parent 9bc2cec commit 954a369

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/_http_outgoing.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ function _storeHeader(firstLine, headers) {
457457
}
458458

459459
if (!state.contLen && !state.te) {
460-
if (!this._hasBody) {
460+
if (!this._hasBody && (this.statusCode === 204 ||
461+
this.statusCode === 304)) {
461462
// Make sure we don't end the 0\r\n\r\n at the end of the message.
462463
this.chunkedEncoding = false;
463464
} else if (!this.useChunkedEncodingByDefault) {
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
const assert = require('assert');
5+
const Countdown = require('../common/countdown');
6+
7+
// The HEAD:204, GET:200 is the most pathological test case.
8+
// GETs following a 204 response with a content-encoding header failed.
9+
// Responses without bodies and without content-length or encoding caused
10+
// the socket to be closed.
11+
const codes = [204, 200, 200, 304, 200];
12+
const methods = ['HEAD', 'HEAD', 'GET', 'HEAD', 'GET'];
13+
14+
const sockets = [];
15+
const agent = new http.Agent();
16+
agent.maxSockets = 1;
17+
18+
const countdown = new Countdown(codes.length, () => server.close());
19+
20+
const server = http.createServer(common.mustCall((req, res) => {
21+
const code = codes.shift();
22+
assert.strictEqual(typeof code, 'number');
23+
assert.ok(code > 0);
24+
res.writeHead(code, {});
25+
res.end();
26+
}, codes.length));
27+
28+
function nextRequest() {
29+
const request = http.request({
30+
port: server.address().port,
31+
path: '/',
32+
agent: agent,
33+
method: methods.shift()
34+
}, common.mustCall((response) => {
35+
response.on('end', common.mustCall(() => {
36+
if (countdown.dec()) {
37+
nextRequest();
38+
}
39+
assert.strictEqual(sockets.length, 1);
40+
}));
41+
response.resume();
42+
}));
43+
request.on('socket', common.mustCall((socket) => {
44+
if (!sockets.includes(socket)) {
45+
sockets.push(socket);
46+
}
47+
}));
48+
request.end();
49+
}
50+
51+
server.listen(0, common.mustCall(nextRequest));

0 commit comments

Comments
 (0)