-
-
Notifications
You must be signed in to change notification settings - Fork 933
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 omit Content-Length
header for Content-Length: 0
cases
#1395
Conversation
starlette/responses.py
Outdated
@@ -289,7 +289,7 @@ def set_stat_headers(self, stat_result: os.stat_result) -> None: | |||
etag_base = str(stat_result.st_mtime) + "-" + str(stat_result.st_size) | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -309,3 +324,8 @@ def test_head_method(test_client_factory): | |||
client = test_client_factory(app) | |||
response = client.head("/") | |||
assert response.text == "" | |||
|
|||
|
|||
def test_empty_response(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_response
test_non_empty_response
test_file_response_unknown_size
test_file_response_known_size
test_streaming_response_unknown_size
test_streaming_response_known_size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realise at the time, but we could just name this one test_file_response
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's one above already (that also checks the content-length
being present), maybe it's just redundant.
This PR adds the
content-length
header 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