Skip to content

Commit

Permalink
http: resume kept-alive when no body allowed
Browse files Browse the repository at this point in the history
In accordance with https://www.rfc-editor.org/rfc/rfc9112#name-message-body-length: HEAD, 1xx, 204, and 304 responses cannot contain a message body.

If a socket will be kept-alive, resume the socket during parsing so that it may be returned to the free pool.

Fixes nodejs#47228
  • Loading branch information
mweberxyz committed Apr 2, 2024
1 parent 9efc84a commit 2de97af
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,14 @@ function statusIsInformational(status) {
return (status < 200 && status >= 100 && status !== 101);
}

function responseCannotContainMessageBody(status, method) {
// RFC9112 Section 6.1.1
return method === 'HEAD'
|| status === 204
|| status === 304
|| statusIsInformational(status)
}

// client
function parserOnIncomingClient(res, shouldKeepAlive) {
const socket = this.socket;
Expand Down Expand Up @@ -654,11 +662,19 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
return 1; // Skip body but don't treat as Upgrade.
}

if (req.shouldKeepAlive && !shouldKeepAlive && !req.upgradeOrConnect) {
if (req.shouldKeepAlive && !req.upgradeOrConnect) {
// Server MUST respond with Connection:keep-alive for us to enable it.
// If we've been upgraded (via WebSockets) we also shouldn't try to
// keep the connection open.
req.shouldKeepAlive = false;
if (!shouldKeepAlive) {
req.shouldKeepAlive = false;
}

// Socket will be kept alive and response cannot contain
// a message body, resume to allow socket to return to free pool
else if (responseCannotContainMessageBody(res.statusCode, method)){
res.resume()
}
}

if (req[kClientRequestStatistics] && hasObserver('http')) {
Expand Down

0 comments on commit 2de97af

Please sign in to comment.