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

Test invalid chunked body #10587

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 8 additions & 8 deletions aiohttp/_http_parser.pyx
Original file line number Diff line number Diff line change
@@ -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):
14 changes: 14 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
@@ -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]