Skip to content

Commit 2de97af

Browse files
committed
http: resume kept-alive when no body allowed
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 #47228
1 parent 9efc84a commit 2de97af

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/_http_client.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ function statusIsInformational(status) {
607607
return (status < 200 && status >= 100 && status !== 101);
608608
}
609609

610+
function responseCannotContainMessageBody(status, method) {
611+
// RFC9112 Section 6.1.1
612+
return method === 'HEAD'
613+
|| status === 204
614+
|| status === 304
615+
|| statusIsInformational(status)
616+
}
617+
610618
// client
611619
function parserOnIncomingClient(res, shouldKeepAlive) {
612620
const socket = this.socket;
@@ -654,11 +662,19 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
654662
return 1; // Skip body but don't treat as Upgrade.
655663
}
656664

657-
if (req.shouldKeepAlive && !shouldKeepAlive && !req.upgradeOrConnect) {
665+
if (req.shouldKeepAlive && !req.upgradeOrConnect) {
658666
// Server MUST respond with Connection:keep-alive for us to enable it.
659667
// If we've been upgraded (via WebSockets) we also shouldn't try to
660668
// keep the connection open.
661-
req.shouldKeepAlive = false;
669+
if (!shouldKeepAlive) {
670+
req.shouldKeepAlive = false;
671+
}
672+
673+
// Socket will be kept alive and response cannot contain
674+
// a message body, resume to allow socket to return to free pool
675+
else if (responseCannotContainMessageBody(res.statusCode, method)){
676+
res.resume()
677+
}
662678
}
663679

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

0 commit comments

Comments
 (0)