Skip to content

Commit 405c7fc

Browse files
committed
fix: only set keep-alive when not exists
1 parent 6f2af08 commit 405c7fc

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

lib/_http_outgoing.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function OutgoingMessage() {
104104
this._last = false;
105105
this.chunkedEncoding = false;
106106
this.shouldKeepAlive = true;
107+
this._defaultKeepAlive = true;
107108
this.useChunkedEncodingByDefault = true;
108109
this.sendDate = false;
109110
this._removedConnection = false;
@@ -427,8 +428,8 @@ function _storeHeader(firstLine, headers) {
427428
(state.contLen || this.useChunkedEncodingByDefault || this.agent);
428429
if (shouldSendKeepAlive) {
429430
header += 'Connection: keep-alive\r\n';
430-
if (this._keepAliveTimeout) {
431-
const timeoutSeconds = MathFloor(this._keepAliveTimeout) / 1000;
431+
if (this._keepAliveTimeout && this._defaultKeepAlive) {
432+
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
432433
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`;
433434
}
434435
} else {
@@ -524,6 +525,9 @@ function matchHeader(self, state, field, value) {
524525
case 'trailer':
525526
state[field] = true;
526527
break;
528+
case 'keep-alive':
529+
self._defaultKeepAlive = false;
530+
break;
527531
}
528532
}
529533

Lines changed: 31 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)