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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't set headers for responses with 1xx, 204 and 304 status code #1397

Merged
merged 4 commits into from
Jan 10, 2022

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Jan 7, 2022

Closes #1178 (?)
Closes #1282

tests/test_responses.py Outdated Show resolved Hide resolved
@Kludex Kludex mentioned this pull request Jan 7, 2022
8 tasks
@TBBle
Copy link

TBBle commented Jan 8, 2022

Looking at this change and prompted by #1395 (comment), what happens if you still pass a body in, i.e. Response(status_code=204, content="hi") in the test?

My skimming of the relevant ASGI 3 spec suggests the ASGI server may add Transfer-Encoding: chunked, which then becomes RFC-invalid again, as the presence of either of Content-Length or Transfer-Encoding signals a body, which is disallowed for 204 and related responses.

Perhaps the implicit expectation is that the ASGI server will use "may" here and recognise response codes for which Transfer-Encoding should not be added (and also implicitly drop the body data, perhaps)? Although that seems like a gap in the spec that it's not explicit, and a gap in behaviour that the same thing is not done for Content-Length, as the same HTTP spec restrictions apply.

I apologise if I'm asking a question with an obvious/known answer already. I'm new to the ASGI ecosystem, and may be misunderstanding the division of responsibility or otherwise.

@tomchristie
Copy link
Member

what happens if you still pass a body in, i.e. Response(status_code=204, content="hi") in the test?

Probably you've just got yourself an RFC-invalid response.

We could potentially guard against those kinds of cases, and I'd be happy for us to discuss that as a follow-up issue if anyone's sufficiently motivated to do so.

The most important thing for this pull request tho, is to make sure we're doing the right thing for the sensible cases.

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

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

Looks great, yup.

Observations prompted by the PR...

I think our naming of populate_content_length / populate_content_type in the init_headers method isn't really ideal at this point, and I'd also consider us switching it to a plain function rather than a stateful method (because I think it's easier to understand the behaviour then).

Not something to address here, but perhaps worth consideration.

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.

JSONResponse Wrong Content-Length Value Add EmptyResponse class
3 participants