-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: keep HTTP/1.1 conns alive even if the Connection header is removed
Previously persistence was completed disabled if you removed this header, which is not correct for modern HTTP, where the header is optional and all connections should persist by default regardless. PR-URL: #46331 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ricky Zhou <[email protected]>
- Loading branch information
Showing
3 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,8 +346,6 @@ For information about the governance of the Node.js project, see | |
**Zeyu "Alex" Yang** <<[email protected]>> (he/him) | ||
* [iansu](https://github.com/iansu) - | ||
**Ian Sutherland** <<[email protected]>> | ||
* [indutny](https://github.com/indutny) - | ||
**Fedor Indutny** <<[email protected]>> | ||
* [JacksonTian](https://github.com/JacksonTian) - | ||
**Jackson Tian** <<[email protected]>> | ||
* [JakobJingleheimer](https://github.com/JakobJingleheimer) - | ||
|
@@ -534,6 +532,8 @@ For information about the governance of the Node.js project, see | |
**Imran Iqbal** <<[email protected]>> | ||
* [imyller](https://github.com/imyller) - | ||
**Ilkka Myller** <<[email protected]>> | ||
* [indutny](https://github.com/indutny) - | ||
**Fedor Indutny** <<[email protected]>> | ||
* [isaacs](https://github.com/isaacs) - | ||
**Isaac Z. Schlueter** <<[email protected]>> | ||
* [italoacasas](https://github.com/italoacasas) - | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
test/parallel/test-http-remove-connection-header-persists-connection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const net = require('net'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer(function(request, response) { | ||
// When the connection header is removed, for HTTP/1.1 the connection should still persist. | ||
// For HTTP/1.0, the connection should be closed after the response automatically. | ||
response.removeHeader('connection'); | ||
|
||
response.end('beep boop\n'); | ||
}); | ||
|
||
const agent = new http.Agent({ keepAlive: true }); | ||
|
||
function makeHttp11Request(cb) { | ||
http.get({ | ||
port: server.address().port, | ||
agent | ||
}, function(res) { | ||
const socket = res.socket; | ||
|
||
assert.strictEqual(res.statusCode, 200); | ||
assert.strictEqual(res.headers.connection, undefined); | ||
|
||
res.setEncoding('ascii'); | ||
let response = ''; | ||
res.on('data', function(chunk) { | ||
response += chunk; | ||
}); | ||
res.on('end', function() { | ||
assert.strictEqual(response, 'beep boop\n'); | ||
|
||
// Wait till next tick to ensure that the socket is returned to the agent before | ||
// we continue to the next request | ||
process.nextTick(function() { | ||
cb(socket); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function makeHttp10Request(cb) { | ||
// We have to manually make HTTP/1.0 requests since Node does not allow sending them: | ||
const socket = net.connect({ port: server.address().port }, function() { | ||
socket.write('GET / HTTP/1.0\r\n' + | ||
'Host: localhost:' + server.address().port + '\r\n' + | ||
'\r\n'); | ||
socket.resume(); // Ignore the response itself | ||
|
||
setTimeout(function() { | ||
cb(socket); | ||
}, 10); | ||
}); | ||
} | ||
|
||
server.listen(0, function() { | ||
makeHttp11Request(function(firstSocket) { | ||
makeHttp11Request(function(secondSocket) { | ||
// Both HTTP/1.1 requests should have used the same socket: | ||
assert.strictEqual(firstSocket, secondSocket); | ||
|
||
makeHttp10Request(function(socket) { | ||
// The server should have immediately closed the HTTP/1.0 socket: | ||
assert.strictEqual(socket.closed, true); | ||
server.close(); | ||
}); | ||
}); | ||
}); | ||
}); |