From c3af7ec5f7c286e8ab8b24ec606e2e1fffe8272b Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Thu, 11 Apr 2024 17:11:42 +0100 Subject: [PATCH] Fix Python parser to mark responses without length as closing (#8320) (cherry picked from commit 9ba9a4e531599b9cb2f8cc80effbde40c7eab0bd) --- CHANGES/8320.bugfix.rst | 1 + aiohttp/http_parser.py | 11 ++++++++++- tests/test_http_parser.py | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 CHANGES/8320.bugfix.rst diff --git a/CHANGES/8320.bugfix.rst b/CHANGES/8320.bugfix.rst new file mode 100644 index 00000000000..027074f743b --- /dev/null +++ b/CHANGES/8320.bugfix.rst @@ -0,0 +1 @@ +Fixed the pure python parser to mark a connection as closing when a response has no length -- by :user:`Dreamsorcerer`. diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index cce0b788d46..013511917e8 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -718,7 +718,16 @@ def parse_message(self, lines: List[bytes]) -> RawResponseMessage: ) = self.parse_headers(lines) if close is None: - close = version_o <= HttpVersion10 + if version_o <= HttpVersion10: + close = True + # https://www.rfc-editor.org/rfc/rfc9112.html#name-message-body-length + elif 100 <= status_i < 200 or status_i in {204, 304}: + close = False + elif hdrs.CONTENT_LENGTH in headers or hdrs.TRANSFER_ENCODING in headers: + close = False + else: + # https://www.rfc-editor.org/rfc/rfc9112.html#section-6.3-2.8 + close = True return RawResponseMessage( version_o, diff --git a/tests/test_http_parser.py b/tests/test_http_parser.py index 04b254c0ae8..32dd0e68b57 100644 --- a/tests/test_http_parser.py +++ b/tests/test_http_parser.py @@ -743,7 +743,7 @@ def test_max_header_value_size_continuation_under_limit(response) -> None: assert msg.version == (1, 1) assert msg.headers == CIMultiDict({"data": "test " + value.decode()}) assert msg.raw_headers == ((b"data", b"test " + value),) - # assert not msg.should_close # TODO: https://github.com/nodejs/llhttp/issues/354 + assert msg.should_close assert msg.compression is None assert not msg.upgrade assert not msg.chunked