Skip to content

Commit 3df7c90

Browse files
committed
http: keep-alive should default with HTTP/1.1 server
As RFC 2616 says we should, assume that servers will provide a persistent connection by default. > A significant difference between HTTP/1.1 and earlier versions of > HTTP is that persistent connections are the default behavior of any > HTTP connection. That is, unless otherwise indicated, the client > SHOULD assume that the server will maintain a persistent connection, > even after error responses from the server. > HTTP/1.1 applications that do not support persistent connections MUST > include the "close" connection option in every message. Fixes #2436.
1 parent f2b1f57 commit 3df7c90

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

lib/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ ClientRequest.prototype.onSocket = function(socket) {
12311231
return true;
12321232
}
12331233

1234-
if (req.shouldKeepAlive && res.headers.connection !== 'keep-alive' && !req.upgraded) {
1234+
if (req.shouldKeepAlive && !shouldKeepAlive && !req.upgraded) {
12351235
// Server MUST respond with Connection:keep-alive for us to enable it.
12361236
// If we've been upgraded (via WebSockets) we also shouldn't try to
12371237
// keep the connection open.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var net = require('net');
26+
27+
var SERVER_RESPONSES = [
28+
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
29+
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n',
30+
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\nConnection: close\r\n\r\n',
31+
'HTTP/1.1 200 ok\r\nContent-Length: 0\r\n\r\n',
32+
'HTTP/1.1 200 ok\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n',
33+
'HTTP/1.1 200 ok\r\nContent-Length: 0\r\nConnection: close\r\n\r\n',
34+
];
35+
var SHOULD_KEEP_ALIVE = [
36+
false, // HTTP/1.0, default
37+
true, // HTTP/1.0, Connection: keep-alive
38+
false, // HTTP/1.0, Connection: close
39+
true, // HTTP/1.1, default
40+
true, // HTTP/1.1, Connection: keep-alive
41+
false, // HTTP/1.1, Connection: close
42+
];
43+
var requests = 0;
44+
var responses = 0;
45+
46+
var server = net.createServer(function(socket) {
47+
socket.write(SERVER_RESPONSES[requests]);
48+
++requests;
49+
}).listen(common.PORT, function() {
50+
function makeRequest() {
51+
var req = http.get({port: common.PORT}, function(res) {
52+
assert.equal(req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses],
53+
SERVER_RESPONSES[responses] + ' should ' +
54+
(SHOULD_KEEP_ALIVE[responses] ? '' : 'not ') +
55+
'Keep-Alive');
56+
++responses;
57+
if (responses < SHOULD_KEEP_ALIVE.length) {
58+
makeRequest();
59+
} else {
60+
server.close();
61+
}
62+
});
63+
}
64+
65+
makeRequest();
66+
});
67+
68+
process.on('exit', function() {
69+
assert.equal(requests, SERVER_RESPONSES.length);
70+
assert.equal(responses, SHOULD_KEEP_ALIVE.length);
71+
});

0 commit comments

Comments
 (0)