diff --git a/lib/_http_client.js b/lib/_http_client.js index f9fd3700c3d19d..ac281f83942295 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -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; @@ -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')) {