Skip to content

Commit

Permalink
[PR #9172/b93ef57c backport][3.10] Improve performance of starting we…
Browse files Browse the repository at this point in the history
…b requests when content length is not set (#9192)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Sep 18, 2024
1 parent 2770e61 commit c717b25
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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

0 comments on commit c717b25

Please sign in to comment.