Skip to content

Commit

Permalink
[PR #7480/1fb06bbc backport][3.8] Fix error pointer on linebreaks (#7482
Browse files Browse the repository at this point in the history
)

**This is a backport of PR #7480 as merged into master
(1fb06bb).**

Fixes #7468.

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Aug 6, 2023
1 parent 8d701c3 commit 8129d26
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 @@ -132,7 +132,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 8129d26

Please sign in to comment.