Skip to content

Commit

Permalink
http: close the connection after sending a body without declared length
Browse files Browse the repository at this point in the history
Previously, if you removed both content-length and transfer-encoding
headers, the connection would still be kept-alive by default. This isn't
helpful, because without those headers, the only way the client knows
when the body is completed is when the connection closes.

See https://www.rfc-editor.org/rfc/rfc7230#section-3.3.3 for more
details on this message body handling logic (this is case 7).

This meant that in effect, if you removed both headers every response
came with a 5 second delay at the end (the default KA timeout) before
the client could process it. Now, if you remove both headers the
connection closes automatically immediately, so the client knows that
it has received the whole message body.

PR-URL: #46333
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
  • Loading branch information
pimterry authored Feb 20, 2023
1 parent ac7ef31 commit ff92b40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,10 @@ function _storeHeader(firstLine, headers) {
// Transfer-Encoding are removed by the user.
// See: test/parallel/test-http-remove-header-stays-removed.js
debug('Both Content-Length and Transfer-Encoding are removed');

// We can't keep alive in this case, because with no header info the body
// is defined as all data until the connection is closed.
this._last = true;
}
}

Expand Down
9 changes: 7 additions & 2 deletions test/parallel/test-http-remove-header-stays-removed.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const server = http.createServer(function(request, response) {
response.setHeader('date', 'coffee o clock');

response.end('beep boop\n');

this.close();
});

let response = '';
Expand All @@ -57,5 +55,12 @@ server.listen(0, function() {
res.on('data', function(chunk) {
response += chunk;
});

setTimeout(function() {
// The socket should be closed immediately, with no keep-alive, because
// no content-length or transfer-encoding are used:
assert.strictEqual(res.socket.closed, true);
server.close();
}, 10);
});
});

0 comments on commit ff92b40

Please sign in to comment.