Skip to content

Commit

Permalink
http: send connection: close when closing conn
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
yannh authored and BethGriggs committed Apr 16, 2019
1 parent 55d3be7 commit 277271c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http-blank-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 277271c

Please sign in to comment.