Skip to content

Commit 8ea2853

Browse files
atian25ruyadorno
authored andcommitted
http: only set keep-alive when not exists
PR-URL: #35138 Fixes: #34561 Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
1 parent 5229ffa commit 8ea2853

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

lib/_http_outgoing.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function OutgoingMessage() {
100100
this._last = false;
101101
this.chunkedEncoding = false;
102102
this.shouldKeepAlive = true;
103+
this._defaultKeepAlive = true;
103104
this.useChunkedEncodingByDefault = true;
104105
this.sendDate = false;
105106
this._removedConnection = false;
@@ -422,8 +423,8 @@ function _storeHeader(firstLine, headers) {
422423
(state.contLen || this.useChunkedEncodingByDefault || this.agent);
423424
if (shouldSendKeepAlive) {
424425
header += 'Connection: keep-alive\r\n';
425-
if (this._keepAliveTimeout) {
426-
const timeoutSeconds = MathFloor(this._keepAliveTimeout) / 1000;
426+
if (this._keepAliveTimeout && this._defaultKeepAlive) {
427+
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
427428
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`;
428429
}
429430
} else {
@@ -519,6 +520,9 @@ function matchHeader(self, state, field, value) {
519520
case 'trailer':
520521
state[field] = true;
521522
break;
523+
case 'keep-alive':
524+
self._defaultKeepAlive = false;
525+
break;
522526
}
523527
}
524528

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const http = require('http');
5+
const assert = require('assert');
6+
7+
const server = http.createServer(common.mustCall((req, res) => {
8+
const body = 'hello world\n';
9+
10+
res.writeHead(200, {
11+
'Content-Length': body.length,
12+
'Keep-Alive': 'timeout=50'
13+
});
14+
res.write(body);
15+
res.end();
16+
}));
17+
server.keepAliveTimeout = 12010;
18+
19+
const agent = new http.Agent({ maxSockets: 1, keepAlive: true });
20+
21+
server.listen(0, common.mustCall(function() {
22+
http.get({
23+
path: '/', port: this.address().port, agent: agent
24+
}, common.mustCall((response) => {
25+
response.resume();
26+
assert.strictEqual(
27+
response.headers['keep-alive'], 'timeout=50');
28+
server.close();
29+
agent.destroy();
30+
}));
31+
}));

test/parallel/test-http-keep-alive-timeout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const server = http.createServer(common.mustCall((req, res) => {
1111
res.write(body);
1212
res.end();
1313
}));
14-
server.keepAliveTimeout = 12000;
14+
server.keepAliveTimeout = 12010;
1515

1616
const agent = new http.Agent({ maxSockets: 1, keepAlive: true });
1717

0 commit comments

Comments
 (0)