http: include Content-Length in HEAD responses for keep-alive#61919
Open
erdinccurebal wants to merge 1 commit intonodejs:mainfrom
Open
http: include Content-Length in HEAD responses for keep-alive#61919erdinccurebal wants to merge 1 commit intonodejs:mainfrom
erdinccurebal wants to merge 1 commit intonodejs:mainfrom
Conversation
When an HTTP server responds to a HEAD request with res.end(), the response does not include a Content-Length header by default. This causes the HTTP parser on the client side to be unable to determine message boundaries, which breaks keep-alive connections for HEAD requests. GET responses already include Content-Length: 0 by default when res.end() is called without data. This change applies the same behavior to HEAD responses (and other bodyless responses) by adding Content-Length when known and not explicitly removed. Fixes: nodejs#28438
Collaborator
|
Review requested:
|
ronag
approved these changes
Feb 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
HEAD responses from
http.createServerdo not include aContent-Lengthheader by default whenres.end()is called without explicit headers. This breaks HTTP keep-alive for HEAD requests because the client-side HTTP parser cannot determine message boundaries.GET responses already include
Content-Length: 0by default. This change applies the same behavior to HEAD (and other bodyless) responses.Before
After
Changes
lib/_http_outgoing.js: In_storeHeader(), when the response has no body (!this._hasBody), also emitContent-Lengthheader if the content length is known and was not explicitly removed by the user.test/parallel/test-http-head-response-content-length-keep-alive.js: New test verifying that both GET and HEAD responses includeContent-Length: 0whenres.end()is called.Reproduction
Fixes: #28438