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

[PR #9172/b93ef57c backport][3.10] Improve performance of starting web requests when content length is not set #9192

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/9172.misc.rst
6 changes: 3 additions & 3 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,10 @@ async def write_eof(self, data: bytes = b"") -> None:
await super().write_eof()

async def _start(self, request: "BaseRequest") -> AbstractStreamWriter:
if should_remove_content_length(request.method, self.status):
if hdrs.CONTENT_LENGTH in self._headers:
if hdrs.CONTENT_LENGTH in self._headers:
if should_remove_content_length(request.method, self.status):
del self._headers[hdrs.CONTENT_LENGTH]
elif not self._chunked and hdrs.CONTENT_LENGTH not in self._headers:
elif not self._chunked:
if isinstance(self._body, Payload):
if self._body.size is not None:
self._headers[hdrs.CONTENT_LENGTH] = str(self._body.size)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,22 @@ async def write_headers(status_line, headers):
assert resp.content_length is None


async def test_rm_content_length_if_204() -> None:
"""Ensure content-length is removed for 204 responses."""
writer = mock.create_autospec(StreamWriter, spec_set=True, instance=True)

async def write_headers(status_line, headers):
assert hdrs.CONTENT_LENGTH not in headers

writer.write_headers.side_effect = write_headers
req = make_request("GET", "/", writer=writer)
payload = BytesPayload(b"answer", headers={"Content-Length": "6"})
resp = Response(body=payload, status=204)
resp.body = payload
await resp.prepare(req)
assert resp.content_length is None


@pytest.mark.parametrize("status", (100, 101, 204, 304))
async def test_rm_transfer_encoding_rfc_9112_6_3_http_11(status: int) -> None:
"""Remove transfer encoding for RFC 9112 sec 6.3 with HTTP/1.1."""
Expand Down
Loading