Don't omit Content-Length header for Content-Length: 0 cases#1395
Don't omit Content-Length header for Content-Length: 0 cases#1395
Content-Length header for Content-Length: 0 cases#1395Conversation
starlette/responses.py
Outdated
| etag = hashlib.md5(etag_base.encode()).hexdigest() | ||
|
|
||
| self.headers.setdefault("content-length", content_length) | ||
| self.headers["content-length"] = content_length |
There was a problem hiding this comment.
We are sure that content-length exists at this point, considering the change of conditional in the init_headers().
There was a problem hiding this comment.
I'd be a bit careful about that assumption. Personally I'd start by reverting this bit, making sure we've got the different test cases I've described. The StreamingResponse and FileResponse won't yet pass, but let's look at how we should resolve them once we've identified the test failures fully.
…rlette into add-content-length-by-default
|
Okay, so forgetting for a moment about
For I'd suggest the next step here is expanding the tests in this PR so that we're covering each case, and can see which ones need fixing up. |
tests/test_responses.py
Outdated
| assert response.text == "" | ||
|
|
||
|
|
||
| def test_empty_response(): |
There was a problem hiding this comment.
This is the test case that we should probably expand to cover the 6 different cases described. I guess we want these test cases?...
test_empty_responsetest_non_empty_responsetest_file_response_unknown_sizetest_file_response_known_sizetest_streaming_response_unknown_sizetest_streaming_response_known_size
There was a problem hiding this comment.
When do we have a FileResponse with unknown size?
Content-Length header for Content-Length: 0 cases.
|
I've retitled this PR to be more representative of the change. |
|
Expressing the thoughts I've shared on Gitter here as well: I need to be sure that the response class is not I don't have (or I can't see) any attribute I can check to be sure it's not a "Transfer-Encoding: chunked" on the at the I've added a conditional on the |
|
Updated the solution to check if body is present on the response. If it's not, then it doesn't add the |
lovelydinosaur
left a comment
There was a problem hiding this comment.
Great! This is a really nicely targeted fix now. 👍
Possibly improvements still for the 1xx, 204, 304 cases, but we can consider those as a separate conversation.
|
For future reference, some discussions about this PR can be retrieved on Gitter: https://gitter.im/encode/community?at=61d6e204526fb77b3166a445 |
Content-Length header for Content-Length: 0 cases.Content-Length header for Content-Length: 0 cases
|
I've removed the period at the end to squash the commit. Thanks @tomchristie :) |
| assert response.headers["content-length"] == "2" | ||
|
|
||
|
|
||
| def test_file_response_known_size(tmpdir, test_client_factory): |
There was a problem hiding this comment.
I didn't realise at the time, but we could just name this one test_file_response.
There was a problem hiding this comment.
There's one above already (that also checks the content-length being present), maybe it's just redundant.
This PR adds the
content-lengthheader by default, if not already included. For more information, please check on #1099 and the referred RFC.Next step would be to add a conditional to check if
status_code< 200 andstatus_code!= (204, 304)`Closes #1099