From 3944b7f3fa4fc543d30a4465f9f68d04831518e3 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Tue, 24 Jan 2023 13:37:05 +0100 Subject: [PATCH 1/3] 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. --- lib/_http_outgoing.js | 9 +- ...e-connection-header-persists-connection.js | 93 +++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-http-remove-connection-header-persists-connection.js diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index e362ea62472e45..9948d5ef94d044 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -506,8 +506,13 @@ function _storeHeader(firstLine, headers) { // keep-alive logic if (this._removedConnection) { - this._last = true; - this.shouldKeepAlive = false; + // shouldKeepAlive is generally true for HTTP/1.1. In that common case, + // even if the connection header isn't sent, we still persist by default. + if (this.shouldKeepAlive) { + this._last = false; + } else { + this._last = true; + } } else if (!state.connection) { const shouldSendKeepAlive = this.shouldKeepAlive && (state.contLen || this.useChunkedEncodingByDefault || this.agent); diff --git a/test/parallel/test-http-remove-connection-header-persists-connection.js b/test/parallel/test-http-remove-connection-header-persists-connection.js new file mode 100644 index 00000000000000..504e707678ea0b --- /dev/null +++ b/test/parallel/test-http-remove-connection-header-persists-connection.js @@ -0,0 +1,93 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'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(); + }); + }); + }); +}); From 66e6405d2bb7bf7644e6081120ee8723cea0e824 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Tue, 24 Jan 2023 14:58:58 +0100 Subject: [PATCH 2/3] Remove unnecessary copyright header from test file --- ...e-connection-header-persists-connection.js | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/test/parallel/test-http-remove-connection-header-persists-connection.js b/test/parallel/test-http-remove-connection-header-persists-connection.js index 504e707678ea0b..6d05874272df03 100644 --- a/test/parallel/test-http-remove-connection-header-persists-connection.js +++ b/test/parallel/test-http-remove-connection-header-persists-connection.js @@ -1,24 +1,3 @@ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - 'use strict'; require('../common'); const assert = require('assert'); From a57bbeffac36450bb119b3a112e10f63d6ac9865 Mon Sep 17 00:00:00 2001 From: Tim Perry <1526883+pimterry@users.noreply.github.com> Date: Tue, 24 Jan 2023 17:55:12 +0100 Subject: [PATCH 3/3] Simplify no-header connection keep-alive logic Co-authored-by: Mohammed Keyvanzadeh --- lib/_http_outgoing.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 9948d5ef94d044..ea3d7e62556212 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -508,11 +508,7 @@ function _storeHeader(firstLine, headers) { if (this._removedConnection) { // shouldKeepAlive is generally true for HTTP/1.1. In that common case, // even if the connection header isn't sent, we still persist by default. - if (this.shouldKeepAlive) { - this._last = false; - } else { - this._last = true; - } + this._last = !this.shouldKeepAlive; } else if (!state.connection) { const shouldSendKeepAlive = this.shouldKeepAlive && (state.contLen || this.useChunkedEncodingByDefault || this.agent);