diff --git a/aiohttp/_http_parser.pyx b/aiohttp/_http_parser.pyx index de986c9dbc7..9e7c4cb4e49 100644 --- a/aiohttp/_http_parser.pyx +++ b/aiohttp/_http_parser.pyx @@ -453,11 +453,14 @@ cdef class HttpParser: upgrade, chunked) if ( - ULLONG_MAX > self._cparser.content_length > 0 or chunked or - self._cparser.method == cparser.HTTP_CONNECT or - (self._cparser.status_code >= 199 and - self._cparser.content_length == 0 and - self._read_until_eof) + self._response_with_body + and ( + ULLONG_MAX > self._cparser.content_length > 0 or chunked or + self._cparser.method == cparser.HTTP_CONNECT or + (self._cparser.status_code >= 199 and + self._cparser.content_length == 0 and + self._read_until_eof) + ) ): payload = StreamReader( self._protocol, timer=self._timer, loop=self._loop, @@ -469,9 +472,6 @@ cdef class HttpParser: if encoding is not None and self._auto_decompress: self._payload = DeflateBuffer(payload, encoding) - if not self._response_with_body: - payload = EMPTY_PAYLOAD - self._messages.append((msg, payload)) cdef _on_message_complete(self): diff --git a/tests/test_http_parser.py b/tests/test_http_parser.py index 6bb06159f21..24795ba589a 100644 --- a/tests/test_http_parser.py +++ b/tests/test_http_parser.py @@ -1361,6 +1361,20 @@ def test_parse_payload_response_without_body( assert payload.is_eof() +def test_parse_payload_response_with_invalid_body( + loop: asyncio.AbstractEventLoop, + protocol: BaseProtocol, + response_cls: Type[HttpResponseParser], +) -> None: + parser = response_cls(protocol, loop, 2**16, response_with_body=False) + text = ( + b"HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n" + b"7\r\nchunked\r\n0\r\n\r\n" + ) + with pytest.raises(http_exceptions.BadHttpMessage, match="status line"): + parser.feed_data(text)[0][0] + + def test_parse_length_payload(response: HttpResponseParser) -> None: text = b"HTTP/1.1 200 Ok\r\ncontent-length: 4\r\n\r\n" msg, payload = response.feed_data(text)[0][0]