Skip to content

Commit

Permalink
http: make HEAD method to work with keep-alive
Browse files Browse the repository at this point in the history
Fixes: #28438

PR-URL: #34231
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
josephhackman authored and aduh95 committed Jan 3, 2021
1 parent 25b21e4 commit 7afa533
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ function _storeHeader(firstLine, headers) {
}

if (!state.contLen && !state.te) {
if (!this._hasBody) {
if (!this._hasBody && (this.statusCode === 204 ||
this.statusCode === 304)) {
// Make sure we don't end the 0\r\n\r\n at the end of the message.
this.chunkedEncoding = false;
} else if (!this.useChunkedEncodingByDefault) {
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-http-reuse-socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const Countdown = require('../common/countdown');

// The HEAD:204, GET:200 is the most pathological test case.
// GETs following a 204 response with a content-encoding header failed.
// Responses without bodies and without content-length or encoding caused
// the socket to be closed.
const codes = [204, 200, 200, 304, 200];
const methods = ['HEAD', 'HEAD', 'GET', 'HEAD', 'GET'];

const sockets = [];
const agent = new http.Agent();
agent.maxSockets = 1;

const countdown = new Countdown(codes.length, () => server.close());

const server = http.createServer(common.mustCall((req, res) => {
const code = codes.shift();
assert.strictEqual(typeof code, 'number');
assert.ok(code > 0);
res.writeHead(code, {});
res.end();
}, codes.length));

function nextRequest() {
const request = http.request({
port: server.address().port,
path: '/',
agent: agent,
method: methods.shift()
}, common.mustCall((response) => {
response.on('end', common.mustCall(() => {
if (countdown.dec()) {
nextRequest();
}
assert.strictEqual(sockets.length, 1);
}));
response.resume();
}));
request.on('socket', common.mustCall((socket) => {
if (!sockets.includes(socket)) {
sockets.push(socket);
}
}));
request.end();
}

server.listen(0, common.mustCall(nextRequest));

0 comments on commit 7afa533

Please sign in to comment.