From 277271c4a9572124e46d193cf8f826380bbe45c3 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Wed, 6 Mar 2019 12:07:47 +0100 Subject: [PATCH] http: send connection: close when closing conn HTTP/1.1 mandates connections which do not support keep-alive and close the connection send the connection: close header, see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.10 This page also provides more information: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection I understand that HTTP/1.1 defaults to keep-alive - and that the Connection: close header is required when closing a connection. This adds the Connection: close header in the 400 responses sent on client errors. Backport-PR-URL: https://github.com/nodejs/node/pull/26627 PR-URL: https://github.com/nodejs/node/pull/26467 Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca --- lib/_http_server.js | 3 ++- test/parallel/test-http-blank-header.js | 4 +++- .../test-http-server-destroy-socket-on-client-error.js | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 2c7c6c29b8646e..46b786b07c862c 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -501,7 +501,8 @@ function onParserExecute(server, socket, parser, state, ret) { } const badRequestResponse = Buffer.from( - `HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}${CRLF}`, 'ascii' + `HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}` + + `Connection: close${CRLF}${CRLF}`, 'ascii' ); function socketOnError(e) { // Ignore further errors diff --git a/test/parallel/test-http-blank-header.js b/test/parallel/test-http-blank-header.js index 3f2b512fc85567..ca2b5005312b47 100644 --- a/test/parallel/test-http-blank-header.js +++ b/test/parallel/test-http-blank-header.js @@ -52,7 +52,9 @@ server.listen(0, common.mustCall(() => { received += data.toString(); })); c.on('end', common.mustCall(() => { - assert.strictEqual('HTTP/1.1 400 Bad Request\r\n\r\n', received); + assert.strictEqual(received, + 'HTTP/1.1 400 Bad Request\r\n' + + 'Connection: close\r\n\r\n'); c.end(); })); c.on('close', common.mustCall(() => server.close())); diff --git a/test/parallel/test-http-server-destroy-socket-on-client-error.js b/test/parallel/test-http-server-destroy-socket-on-client-error.js index 9d51364183385b..7085401883933b 100644 --- a/test/parallel/test-http-server-destroy-socket-on-client-error.js +++ b/test/parallel/test-http-server-destroy-socket-on-client-error.js @@ -37,7 +37,9 @@ server.listen(0, () => { }); socket.on('end', mustCall(() => { - const expected = Buffer.from('HTTP/1.1 400 Bad Request\r\n\r\n'); + const expected = Buffer.from( + 'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n' + ); assert(Buffer.concat(chunks).equals(expected)); server.close();