From bb909fb7f32e087ca4f63be57a3c0bad4fce56a1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 10 Oct 2024 08:15:13 -0500 Subject: [PATCH] Improve performance of fetching the content-length for web responses (#9448) Co-authored-by: Sam Bull (cherry picked from commit 93e87c2e9cf0fc3a9af29ea9efec0f4c2e37a5b3) --- aiohttp/helpers.py | 6 +----- aiohttp/web_response.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 1ea6a56db46..6ee70786cfb 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -814,11 +814,7 @@ def charset(self) -> Optional[str]: def content_length(self) -> Optional[int]: """The value of Content-Length HTTP header.""" content_length = self._headers.get(hdrs.CONTENT_LENGTH) - - if content_length is not None: - return int(content_length) - else: - return None + return None if content_length is None else int(content_length) def set_result(fut: "asyncio.Future[_T]", result: _T) -> None: diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index 4d5095a4fea..2036a8d088b 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -709,7 +709,7 @@ def content_length(self) -> Optional[int]: return None if hdrs.CONTENT_LENGTH in self._headers: - return super().content_length + return int(self._headers[hdrs.CONTENT_LENGTH]) if self._compressed_body is not None: # Return length of the compressed body