Skip to content

Commit c957b05

Browse files
yannhBridgeAR
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 and 414 responses sent on client errors. PR-URL: #26467 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 914d908 commit c957b05

4 files changed

+12
-5
lines changed

lib/_http_server.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,12 @@ function onParserExecute(server, socket, parser, state, ret) {
506506

507507
const noop = () => {};
508508
const badRequestResponse = Buffer.from(
509-
`HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}${CRLF}`, 'ascii'
509+
`HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}` +
510+
`Connection: close${CRLF}${CRLF}`, 'ascii'
510511
);
511512
const requestHeaderFieldsTooLargeResponse = Buffer.from(
512-
`HTTP/1.1 431 ${STATUS_CODES[431]}${CRLF}${CRLF}`, 'ascii'
513+
`HTTP/1.1 431 ${STATUS_CODES[431]}${CRLF}` +
514+
`Connection: close${CRLF}${CRLF}`, 'ascii'
513515
);
514516
function socketOnError(e) {
515517
// 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(received, 'HTTP/1.1 400 Bad Request\r\n\r\n');
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-header-overflow.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ server.listen(0, mustCall(() => {
3939
c.on('end', mustCall(() => {
4040
assert.strictEqual(
4141
received,
42-
'HTTP/1.1 431 Request Header Fields Too Large\r\n\r\n'
42+
'HTTP/1.1 431 Request Header Fields Too Large\r\n' +
43+
'Connection: close\r\n\r\n'
4344
);
4445
c.end();
4546
}));

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)