Skip to content

Commit 71b57b4

Browse files
[PR #13172/a57747ed backport][3.14] Fix C parser folding fragment into query_string for empty-query origin-form target (#13190)
**This is a backport of PR #13172 as merged into master (a57747e).** Co-authored-by: giulio d'erme <giulio@derme.dev>
1 parent 64a03fb commit 71b57b4

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎CHANGES/13171.bugfix.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed the C HTTP parser folding the fragment into the query string for an
2+
origin-form request target with an empty query (e.g. ``/path?#frag``),
3+
which diverged from the pure-Python parser -- by :user:`GiulioDER`.

‎aiohttp/_http_parser.pyx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ cdef class HttpRequestParser(HttpParser):
734734
else:
735735
path = self._path[0:idx1]
736736
idx1 += 1
737-
idx2 = self._path.find("#", idx1+1)
737+
idx2 = self._path.find("#", idx1)
738738
if idx2 == -1:
739739
query = self._path[idx1:]
740740
fragment = ""

‎tests/test_http_parser.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,19 @@ def test_parse_uri_utf8_percent_encoded(parser) -> None:
24112411
assert msg.url.fragment == "фраг"
24122412

24132413

2414+
def test_parse_uri_empty_query_with_fragment(parser: HttpRequestParser) -> None:
2415+
# Origin-form target with an empty query but a fragment: the ``#`` sits
2416+
# immediately after ``?``. Regression for the C parser folding ``#frag``
2417+
# into the query string instead of the fragment.
2418+
text = b"GET /path?#frag HTTP/1.1\r\nHost: a\r\n\r\n"
2419+
messages, upgrade, tail = parser.feed_data(text)
2420+
msg = messages[0][0]
2421+
2422+
assert msg.url.path == "/path"
2423+
assert msg.url.query == {}
2424+
assert msg.url.fragment == "frag"
2425+
2426+
24142427
@pytest.mark.skipif(
24152428
"HttpRequestParserC" not in dir(aiohttp.http_parser),
24162429
reason="C based HTTP parser not available",

0 commit comments

Comments
 (0)