Skip to content

Commit f2afa2f

Browse files
committed
Fix how pure-Python HTTP parser interprets //
1 parent e44f21d commit f2afa2f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGES/5498.bugfix

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fix interpretation difference of the pure-Python and the Cython-based
2+
HTTP parsers construct a ``yarl.URL`` object for HTTP request-target.
3+
4+
Before this fix, the Python parser would turn the URI's absolute-path
5+
for ``//some-path`` into ``/`` while the Cython code preserved it as
6+
``//some-path``. Now, both do the latter.

aiohttp/http_parser.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ def parse_message(self, lines: List[bytes]) -> RawRequestMessage:
522522
"Status line is too long", str(self.max_line_size), str(len(path))
523523
)
524524

525+
path_part, _hash_separator, url_fragment = path.partition("#")
526+
path_part, _question_mark_separator, qs_part = path_part.partition("?")
527+
525528
# method
526529
if not METHRE.match(method):
527530
raise BadStatusLine(method)
@@ -562,7 +565,16 @@ def parse_message(self, lines: List[bytes]) -> RawRequestMessage:
562565
compression,
563566
upgrade,
564567
chunked,
565-
URL(path),
568+
# NOTE: `yarl.URL.build()` is used to mimic what the Cython-based
569+
# NOTE: parser does, otherwise it results into the same
570+
# NOTE: HTTP Request-Line input producing different
571+
# NOTE: `yarl.URL()` objects
572+
URL.build(
573+
path=path_part,
574+
query_string=qs_part,
575+
fragment=url_fragment,
576+
encoded=True,
577+
),
566578
)
567579

568580

tests/test_http_parser.py

+1
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ def test_http_request_parser_two_slashes(parser: Any) -> None:
530530

531531
assert msg.method == "GET"
532532
assert msg.path == "//path"
533+
assert msg.url.path == "//path"
533534
assert msg.version == (1, 1)
534535
assert not msg.should_close
535536
assert msg.compression is None

0 commit comments

Comments
 (0)