Skip to content

Commit b61f0fd

Browse files
committedFeb 25, 2021
Fix how pure-Python HTTP parser interprets //
(cherry picked from commit f2afa2f)
1 parent 5c1efbc commit b61f0fd

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
@@ -498,6 +498,9 @@ def parse_message(self, lines: List[bytes]) -> Any:
498498
"Status line is too long", str(self.max_line_size), str(len(path))
499499
)
500500

501+
path_part, _hash_separator, url_fragment = path.partition("#")
502+
path_part, _question_mark_separator, qs_part = path_part.partition("?")
503+
501504
# method
502505
if not METHRE.match(method):
503506
raise BadStatusLine(method)
@@ -538,7 +541,16 @@ def parse_message(self, lines: List[bytes]) -> Any:
538541
compression,
539542
upgrade,
540543
chunked,
541-
URL(path),
544+
# NOTE: `yarl.URL.build()` is used to mimic what the Cython-based
545+
# NOTE: parser does, otherwise it results into the same
546+
# NOTE: HTTP Request-Line input producing different
547+
# NOTE: `yarl.URL()` objects
548+
URL.build(
549+
path=path_part,
550+
query_string=qs_part,
551+
fragment=url_fragment,
552+
encoded=True,
553+
),
542554
)
543555

544556

‎tests/test_http_parser.py

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

529529
assert msg.method == "GET"
530530
assert msg.path == "//path"
531+
assert msg.url.path == "//path"
531532
assert msg.version == (1, 1)
532533
assert not msg.should_close
533534
assert msg.compression is None

0 commit comments

Comments
 (0)
Please sign in to comment.