Skip to content

fix: render Content-Length for HEAD responses with a declared length - #1244

Open
pjfanning wants to merge 1 commit into
apache:1.4.xfrom
pjfanning:header-1.4
Open

fix: render Content-Length for HEAD responses with a declared length#1244
pjfanning wants to merge 1 commit into
apache:1.4.xfrom
pjfanning:header-1.4

Conversation

@pjfanning

Copy link
Copy Markdown
Member

cherry pick dd0a4a3 (#1237)

Motivation:
PR #962 corrected Content-Length rendering for 205 and CONNECT, but it also made HEAD responses drop the header unconditionally. RFC 9110 section 8.6 allows a server to send Content-Length in a HEAD response, and RFC 9112 section 6.3 rule 1 exempts HEAD responses from body framing, so the header is metadata there and cannot desync a connection. Suppressing it breaks the common case of using HEAD to learn the size of a resource without fetching it, and it is asymmetric: our own client parser honours a HEAD Content-Length, and the HTTP/2 renderer never consulted contentLengthAllowed at all.

Modification:
Restore the pre-1.4 predicate for HEAD (contentLengthAllowedForHead now defers to StatusCode.allowsEntity) and add the length-based policy at render time: a HEAD response renders Content-Length only when the entity declares a length greater than zero. A zero length nearly always means the application had no body to hand over rather than that the resource is empty, and the client parser already only honours a HEAD Content-Length when it is greater than zero. Chunked and CloseDelimited entities have no known length and are unchanged. 205, 204, 304 and CONNECT behaviour from #962 is untouched.

Result:
A HEAD response completed with HttpEntity.Strict or HttpEntity.Default(contentType, length, Source.empty) renders the declared Content-Length again, while an empty entity renders none.

Tests:

  • sbt "http-core / Test / testOnly org.apache.pekko.http.impl.engine.rendering.ResponseRendererSpec org.apache.pekko.http.impl.engine.server.HttpServerSpec org.apache.pekko.http.scaladsl.model.HttpMethodsSpec" - 110 passed
  • sbt "http-core / Test / testOnly org.apache.pekko.http.impl.engine.client.HostConnectionPoolSpec" - 66 passed
  • sbt +mimaReportBinaryIssues - success
  • scalafmt --mode diff-ref=upstream/main - clean
  • git diff --check - clean
  • sbt "http-core / test" and sbt "docs / paradox" - not run to completion locally, left to CI

References:
Fixes #1236, Refs #962

…pache#1237)

Motivation:
PR apache#962 corrected Content-Length rendering for 205 and CONNECT, but it also
made HEAD responses drop the header unconditionally. RFC 9110 section 8.6
allows a server to send Content-Length in a HEAD response, and RFC 9112
section 6.3 rule 1 exempts HEAD responses from body framing, so the header is
metadata there and cannot desync a connection. Suppressing it breaks the
common case of using HEAD to learn the size of a resource without fetching it,
and it is asymmetric: our own client parser honours a HEAD Content-Length, and
the HTTP/2 renderer never consulted contentLengthAllowed at all.

Modification:
Restore the pre-1.4 predicate for HEAD (contentLengthAllowedForHead now
defers to StatusCode.allowsEntity) and add the length-based policy at render
time: a HEAD response renders Content-Length only when the entity declares a
length greater than zero. A zero length nearly always means the application
had no body to hand over rather than that the resource is empty, and the
client parser already only honours a HEAD Content-Length when it is greater
than zero. Chunked and CloseDelimited entities have no known length and are
unchanged. 205, 204, 304 and CONNECT behaviour from apache#962 is untouched.

Result:
A HEAD response completed with HttpEntity.Strict or
HttpEntity.Default(contentType, length, Source.empty) renders the declared
Content-Length again, while an empty entity renders none.

Tests:
- sbt "http-core / Test / testOnly org.apache.pekko.http.impl.engine.rendering.ResponseRendererSpec org.apache.pekko.http.impl.engine.server.HttpServerSpec org.apache.pekko.http.scaladsl.model.HttpMethodsSpec" - 110 passed
- sbt "http-core / Test / testOnly org.apache.pekko.http.impl.engine.client.HostConnectionPoolSpec" - 66 passed
- sbt +mimaReportBinaryIssues - success
- scalafmt --mode diff-ref=upstream/main - clean
- git diff --check - clean
- sbt "http-core / test" and sbt "docs / paradox" - not run to completion locally, left to CI

References:
Fixes apache#1236, Refs apache#962
@pjfanning pjfanning added this to the 1.4.1 milestone Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant