Skip to content

Commit 3214116

Browse files
mikealry
authored andcommitted
Implement keep-alive for http.Client
Send the 'Connection: keep-alive' header in your request to enable.
1 parent 71009ad commit 3214116

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

doc/api.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,16 @@ Example of connecting to `google.com`:
19111911
});
19121912
});
19131913

1914+
There are a few special headers that should be noted.
1915+
1916+
* The 'Host' header is not added by Node, and is usually required by
1917+
website.
1918+
1919+
* Sending a 'Connection: keep-alive' will notify Node that the connection to
1920+
the server should be persisted until the next request.
1921+
1922+
* Sending a 'Content-length' header will disable the default chunked encoding.
1923+
19141924

19151925
### Event: 'upgrade'
19161926

lib/http.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) {
374374

375375
if (connectionExpression.test(field)) {
376376
sentConnectionHeader = true;
377-
if (closeExpression.test(value)) this._last = true;
377+
if (closeExpression.test(value)) {
378+
this._last = true;
379+
} else {
380+
this.shouldKeepAlive = true;
381+
}
378382

379383
} else if (transferEncodingExpression.test(field)) {
380384
sentTransferEncodingHeader = true;
@@ -855,7 +859,13 @@ function Client ( ) {
855859
// For the moment we reconnect for every request. FIXME!
856860
// All that should be required for keep-alive is to not reconnect,
857861
// but outgoingFlush instead.
858-
self.end();
862+
if (req.shouldKeepAlive) {
863+
outgoingFlush(self)
864+
self._outgoing.shift()
865+
outgoingFlush(self)
866+
} else {
867+
self.end();
868+
}
859869
});
860870

861871
req.emit("response", res);

test/simple/test-http-keep-alive.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
common = require("../common");
2+
assert = common.assert
3+
4+
assert = require("assert");
5+
http = require("http");
6+
sys = require("sys");
7+
8+
body = "hello world\n";
9+
headers = {'connection':'keep-alive'}
10+
11+
server = http.createServer(function (req, res) {
12+
res.writeHead(200, {"Content-Length": body.length});
13+
res.write(body);
14+
res.end();
15+
});
16+
17+
connectCount = 0;
18+
19+
server.listen(common.PORT, function () {
20+
var client = http.createClient(common.PORT);
21+
22+
client.addListener("connect", function () {
23+
common.error("CONNECTED")
24+
connectCount++;
25+
})
26+
27+
var request = client.request("GET", "/", headers);
28+
request.end();
29+
request.addListener('response', function (response) {
30+
common.error('response start');
31+
32+
33+
response.addListener("end", function () {
34+
common.error('response end');
35+
var req = client.request("GET", "/", headers);
36+
req.addListener('response', function (response) {
37+
response.addListener("end", function () {
38+
client.end();
39+
server.close();
40+
})
41+
})
42+
req.end();
43+
});
44+
});
45+
});
46+
47+
process.addListener('exit', function () {
48+
assert.equal(1, connectCount);
49+
});

0 commit comments

Comments
 (0)