fix: render Content-Length for HEAD responses with a declared length - #1244
Open
pjfanning wants to merge 1 commit into
Open
fix: render Content-Length for HEAD responses with a declared length#1244pjfanning wants to merge 1 commit into
pjfanning wants to merge 1 commit into
Conversation
…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
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.
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:
References:
Fixes #1236, Refs #962