Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d477e2e

Browse filesBrowse files
atian25MylesBorins
authored andcommitted
http: only set keep-alive when not exists
Backport-PR-URL: #35623 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 8a10916 commit d477e2e
Copy full SHA for d477e2e

File tree

Expand file treeCollapse file tree

3 files changed

+38
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+38
-3
lines changed
Open diff view settings
Collapse file

‎lib/_http_outgoing.js‎

Copy file name to clipboardExpand all lines: lib/_http_outgoing.js
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function OutgoingMessage() {
9898
this._last = false;
9999
this.chunkedEncoding = false;
100100
this.shouldKeepAlive = true;
101+
this._defaultKeepAlive = true;
101102
this.useChunkedEncodingByDefault = true;
102103
this.sendDate = false;
103104
this._removedConnection = false;
@@ -405,8 +406,8 @@ function _storeHeader(firstLine, headers) {
405406
(state.contLen || this.useChunkedEncodingByDefault || this.agent);
406407
if (shouldSendKeepAlive) {
407408
header += 'Connection: keep-alive\r\n';
408-
if (this._keepAliveTimeout) {
409-
const timeoutSeconds = MathFloor(this._keepAliveTimeout) / 1000;
409+
if (this._keepAliveTimeout && this._defaultKeepAlive) {
410+
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
410411
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`;
411412
}
412413
} else {
@@ -502,6 +503,9 @@ function matchHeader(self, state, field, value) {
502503
case 'trailer':
503504
state[field] = true;
504505
break;
506+
case 'keep-alive':
507+
self._defaultKeepAlive = false;
508+
break;
505509
}
506510
}
507511

Collapse file
+31Lines 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+
}));
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-http-keep-alive-timeout.js
+1-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.