Skip to content
Myr

Fix for gen-delims escaping behaviour in path/query/fragment (#2701)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent df5dbc0 · Browse files · 2 files+40 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+40 added−3 removed

  • tests:+30 added−0 removed
  • other:+10 added−3 removed

2 files+40 added−3 removed

modified:httpx/_urlparse.py+10 added-3 removed
251 unmodified lines
252 validate_path(path, has_scheme=has_scheme, has_authority=has_authority)253 if has_authority:254 path = normalize_path(path)255256 # The GEN_DELIMS set is... : / ? # [ ] @257 # These do not need to be percent-quoted unless they serve as delimiters for the258 # specific component.259260 # For 'path' we need to drop ? and # from the GEN_DELIMS set.256 parsed_path: str = quote(path, safe=SUB_DELIMS + ":@/")261 parsed_path: str = quote(path, safe=SUB_DELIMS + ":/[]@")262 # For 'query' we need to drop '#' from the GEN_DELIMS set.263 parsed_query: typing.Optional[str] = (258 None if query is None else quote(query, safe=SUB_DELIMS + "/?")264 None if query is None else quote(query, safe=SUB_DELIMS + ":/?[]@")265 )266 # For 'fragment' we can include all of the GEN_DELIMS set.267 parsed_fragment: typing.Optional[str] = (261 None if fragment is None else quote(fragment, safe=SUB_DELIMS + "/?")268 None if fragment is None else quote(fragment, safe=SUB_DELIMS + ":/?#[]@")269 )270271 # The parsed ASCII bytestrings are our canonical form.272 # All properties of the URL are derived from these.
190 unmodified lines
modified:tests/test_urlparse.py+30 added
245 unmodified lines
246 assert str(url) == "http://example.com/"247248 url = url.copy_with(path="/abc")249 assert str(url) == "http://example.com/abc"250251252# Tests for percent encoding across path, query, and fragement...253254255def test_path_percent_encoding():256 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS257 url = httpx.URL("https://example.com/!$&'()*+,;= abc ABC 123 :/[]@")258 assert url.raw_path == b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@"259 assert url.path == "/!$&'()*+,;= abc ABC 123 :/[]@"260 assert url.query == b""261 assert url.fragment == ""262263264def test_query_percent_encoding():265 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS266 url = httpx.URL("https://example.com/?!$&'()*+,;= abc ABC 123 :/[]@" + "?")267 assert url.raw_path == b"/?!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?"268 assert url.path == "/"269 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?"270 assert url.fragment == ""271272273def test_fragment_percent_encoding():274 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS275 url = httpx.URL("https://example.com/#!$&'()*+,;= abc ABC 123 :/[]@" + "?#")276 assert url.raw_path == b"/"277 assert url.path == "/"278 assert url.query == b""279 assert url.fragment == "!$&'()*+,;= abc ABC 123 :/[]@?#"