Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #4213: Content-Length when entity is unallowed #4214

Merged

Conversation

bursauxa
Copy link
Contributor

Adds Content-Length: 0 header on responses that do not allow a response body (204, 205 and 304).

I did not add a specific test, as there was already a test for rendering a 304 response - I simply made that test more correct 馃檪

Fixes #4213

@lightbend-cla-validator

@bursauxa bursauxa force-pushed the bugfix/content-length-on-empty-entities branch from dab29c9 to 471e6ca Compare January 12, 2023 14:34
@johanandren
Copy link
Member

Trying to verify this is the right thing I found RFC 9110 (which obsoletes 7231) saying:

https://httpwg.org/specs/rfc9110.html#field.content-length

A server MAY send a Content-Length header field in a 304 (Not Modified) response to a conditional GET request (Section 15.4.5); a server MUST NOT send Content-Length in such a response unless its field value equals the decimal number of octets that would have been sent in the content of a 200 (OK) response to the same request.

And

A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational) or 204 (No Content). A server MUST NOT send a Content-Length header field in any 2xx (Successful) response to a CONNECT request (Section 9.3.6).

So I wonder if a fix can really be this easy, I think it needs to be specifically/selectively emitted based on the status code and request method. It also explicitly contradicts emitting it for 204.

@bursauxa
Copy link
Contributor Author

bursauxa commented Jan 12, 2023

Thanks @johanandren for pointing that out. You are right, it will require some case-by-case handling, I will proceed with the changes.

Also I inadvertently added the header to 1xx responses (allowsEntity was set to false in the base class Informational for all codes of that region, not for specific codes like it was for 204, 205 and 304). It will be fixed along the way.

@johanandren
Copy link
Member

Wdyt about the explicit MUST NOT emit content length for 204, that seems like it contradicts what the clients expect if it is as you describe in the issue?

@bursauxa
Copy link
Contributor Author

Wdyt about the explicit MUST NOT emit content length for 204, that seems like it contradicts what the clients expect if it is as you describe in the issue?

I specifically encountered the issue with a 205 code. It was mentioned in the linked issue, but I oversimplified in this pull request. Testing with curl shows that indeed, a 204 code without Content-Length header works fine - the client does not hang.

It turns out to be very specific to the 205 code... The previous condition on status.allowsEntity matches all codes where Content-Length must not be used, with the exception of 205 that does not allow an entity but should still provide a Content-Length.

There are also cases with HEAD and CONNECT methods that I did not encounter, I do not even know to what degree they would realistically cause issues to clients (a response body seems very unlikely for these methods), but might as well align them with the RFC while working on it.

@bursauxa bursauxa force-pushed the bugfix/content-length-on-empty-entities branch from 471e6ca to ab3bcab Compare January 12, 2023 17:06
@bursauxa
Copy link
Contributor Author

Not entirely related, but you made me think @johanandren ... And I stumbled upon these tests with HEAD requests containing bodies (ResponseRendererSpec.scala, around line 150). This is not valid according to the same RFC 9110 you mentioned earlier:

The HEAD method is identical to GET except that the server MUST NOT send content in the response.

@johanandren
Copy link
Member

I think we can leave HEAD returning bodies alone for now, as you say it is against spec, but in case anyone is relying on doing that already.

@bursauxa bursauxa force-pushed the bugfix/content-length-on-empty-entities branch 2 times, most recently from 70a2675 to cf2e29c Compare January 13, 2023 10:41
@bursauxa bursauxa force-pushed the bugfix/content-length-on-empty-entities branch 2 times, most recently from 14e1a82 to 16bd97c Compare January 13, 2023 13:22
@johanandren
Copy link
Member

Some related test failures, and some I'm not entirely sure about, to look into.

Also, failures from MiMa which is our binary incompatibility detection, I think all of them can now safely be filtered as described here: https://github.com/akka/akka-http/blob/main/CONTRIBUTING.md#binary-compatibility

@bursauxa bursauxa force-pushed the bugfix/content-length-on-empty-entities branch from 16bd97c to 7672d21 Compare January 13, 2023 15:08
@bursauxa bursauxa force-pushed the bugfix/content-length-on-empty-entities branch from 7672d21 to 616e374 Compare January 13, 2023 15:52
@bursauxa
Copy link
Contributor Author

Tests were good with previous run, this one should take care of binary incompatibilities.

Copy link
Member

@johanandren johanandren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johanandren johanandren merged commit f198c61 into akka:main Jan 25, 2023
@johanandren johanandren added this to the 10.5.0-M2 milestone Jan 25, 2023
@johanandren johanandren modified the milestones: 10.5.0-M2, 10.5.0 Feb 21, 2023
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.

Empty entity should set Content-Length header
3 participants