Skip to content

Commit

Permalink
Fix error pointer on linebreaks (#7480)
Browse files Browse the repository at this point in the history
Fixes #7468.
  • Loading branch information
Dreamsorcerer authored Aug 5, 2023
1 parent 878ac7d commit 1fb06bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/7468.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed output of parsing errors on `\n`. -- by :user:`Dreamsorcerer`
4 changes: 2 additions & 2 deletions aiohttp/_http_parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ cdef class HttpParser:
else:
after = cparser.llhttp_get_error_pos(self._cparser)
before = data[:after - <char*>self.py_buf.buf]
after_b = after.split(b"\n", 1)[0]
before = before.rsplit(b"\n", 1)[-1]
after_b = after.split(b"\r\n", 1)[0]
before = before.rsplit(b"\r\n", 1)[-1]
data = before + after_b
pointer = " " * (len(repr(before))-1) + "^"
ex = parser_error_from_errno(self._cparser, data, pointer)
Expand Down
22 changes: 21 additions & 1 deletion tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,27 @@ def test_invalid_character(loop: Any, protocol: Any, request: Any) -> None:
error_detail = re.escape(
r""":
b'Set-Cookie: abc\x01def\r'
b'Set-Cookie: abc\x01def'
^"""
)
with pytest.raises(http_exceptions.BadHttpMessage, match=error_detail):
parser.feed_data(text)


@pytest.mark.skipif(NO_EXTENSIONS, reason="Only tests C parser.")
def test_invalid_linebreak(loop: Any, protocol: Any, request: Any) -> None:
parser = HttpRequestParserC(
protocol,
loop,
2**16,
max_line_size=8190,
max_field_size=8190,
)
text = b"GET /world HTTP/1.1\r\nHost: 127.0.0.1\n\r\n"
error_detail = re.escape(
r""":
b'Host: 127.0.0.1\n'
^"""
)
with pytest.raises(http_exceptions.BadHttpMessage, match=error_detail):
Expand Down

0 comments on commit 1fb06bb

Please sign in to comment.