Skip to content

Commit 277271c

Browse files
yannhBethGriggs
authored andcommitted
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: #26627 PR-URL: #26467 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 55d3be7 commit 277271c

3 files changed

+8
-3
lines changed

lib/_http_server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ function onParserExecute(server, socket, parser, state, ret) {
501501
}
502502

503503
const badRequestResponse = Buffer.from(
504-
`HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}${CRLF}`, 'ascii'
504+
`HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}` +
505+
`Connection: close${CRLF}${CRLF}`, 'ascii'
505506
);
506507
function socketOnError(e) {
507508
// Ignore further errors

test/parallel/test-http-blank-header.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ server.listen(0, common.mustCall(() => {
5252
received += data.toString();
5353
}));
5454
c.on('end', common.mustCall(() => {
55-
assert.strictEqual('HTTP/1.1 400 Bad Request\r\n\r\n', received);
55+
assert.strictEqual(received,
56+
'HTTP/1.1 400 Bad Request\r\n' +
57+
'Connection: close\r\n\r\n');
5658
c.end();
5759
}));
5860
c.on('close', common.mustCall(() => server.close()));

test/parallel/test-http-server-destroy-socket-on-client-error.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ server.listen(0, () => {
3737
});
3838

3939
socket.on('end', mustCall(() => {
40-
const expected = Buffer.from('HTTP/1.1 400 Bad Request\r\n\r\n');
40+
const expected = Buffer.from(
41+
'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
42+
);
4143
assert(Buffer.concat(chunks).equals(expected));
4244

4345
server.close();

0 commit comments

Comments
 (0)