Skip to content

Commit 5064822

Browse files
josephhackmantargos
authored andcommitted
http: make HEAD method to work with keep-alive
Fixes: #28438 PR-URL: #34231 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 71b94e1 commit 5064822

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎lib/_http_outgoing.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ function _storeHeader(firstLine, headers) {
445445
}
446446

447447
if(!state.contLen&&!state.te){
448-
if(!this._hasBody){
448+
if(!this._hasBody&&(this.statusCode===204||
449+
this.statusCode===304)){
449450
// Make sure we don't end the 0\r\n\r\n at the end of the message.
450451
this.chunkedEncoding=false;
451452
}elseif(!this.useChunkedEncodingByDefault){
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
consthttp=require('http');
4+
constassert=require('assert');
5+
constCountdown=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+
constcodes=[204,200,200,304,200];
12+
constmethods=['HEAD','HEAD','GET','HEAD','GET'];
13+
14+
constsockets=[];
15+
constagent=newhttp.Agent();
16+
agent.maxSockets=1;
17+
18+
constcountdown=newCountdown(codes.length,()=>server.close());
19+
20+
constserver=http.createServer(common.mustCall((req,res)=>{
21+
constcode=codes.shift();
22+
assert.strictEqual(typeofcode,'number');
23+
assert.ok(code>0);
24+
res.writeHead(code,{});
25+
res.end();
26+
},codes.length));
27+
28+
functionnextRequest(){
29+
constrequest=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)