Skip to content

Commit 8129d26

Browse files
[PR #7480/1fb06bbc backport][3.8] Fix error pointer on linebreaks (#7482)
**This is a backport of PR #7480 as merged into master (1fb06bb).** Fixes #7468. Co-authored-by: Sam Bull <[email protected]>
1 parent 8d701c3 commit 8129d26

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGES/7468.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed output of parsing errors on `\n`. -- by :user:`Dreamsorcerer`

aiohttp/_http_parser.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ cdef class HttpParser:
548548
else:
549549
after = cparser.llhttp_get_error_pos(self._cparser)
550550
before = data[:after - <char*>self.py_buf.buf]
551-
after_b = after.split(b"\n", 1)[0]
552-
before = before.rsplit(b"\n", 1)[-1]
551+
after_b = after.split(b"\r\n", 1)[0]
552+
before = before.rsplit(b"\r\n", 1)[-1]
553553
data = before + after_b
554554
pointer = " " * (len(repr(before))-1) + "^"
555555
ex = parser_error_from_errno(self._cparser, data, pointer)

tests/test_http_parser.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,27 @@ def test_invalid_character(loop: Any, protocol: Any, request: Any) -> None:
132132
error_detail = re.escape(
133133
r""":
134134
135-
b'Set-Cookie: abc\x01def\r'
135+
b'Set-Cookie: abc\x01def'
136+
^"""
137+
)
138+
with pytest.raises(http_exceptions.BadHttpMessage, match=error_detail):
139+
parser.feed_data(text)
140+
141+
142+
@pytest.mark.skipif(NO_EXTENSIONS, reason="Only tests C parser.")
143+
def test_invalid_linebreak(loop: Any, protocol: Any, request: Any) -> None:
144+
parser = HttpRequestParserC(
145+
protocol,
146+
loop,
147+
2**16,
148+
max_line_size=8190,
149+
max_field_size=8190,
150+
)
151+
text = b"GET /world HTTP/1.1\r\nHost: 127.0.0.1\n\r\n"
152+
error_detail = re.escape(
153+
r""":
154+
155+
b'Host: 127.0.0.1\n'
136156
^"""
137157
)
138158
with pytest.raises(http_exceptions.BadHttpMessage, match=error_detail):

0 commit comments

Comments
 (0)