Skip to content
Myr

Always encode forward slashes as `%2F` in query parameters (#2723)

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

* Always encode forward slashes as `%2F` in query parameters * Revert inclusion of "%" This is expected to fail tests due to double escaping * Update `urlencode` ---------

authored by Zanie Adkins · committed by GitHub · parent 301b8fb · Browse files · 3 files+15 added−13 removed
This commit

State

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

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

3 files+15 added−13 removed

  • tests:+5 added−5 removed
  • other:+10 added−8 removed

3 files+15 added−13 removed

modified:httpx/_urlparse.py+10 added-8 removed
258 unmodified lines
259260 # For 'path' we need to drop ? and # from the GEN_DELIMS set.261 parsed_path: str = quote(path, safe=SUB_DELIMS + ":/[]@")262 # For 'query' we need to drop '#' from the GEN_DELIMS set.263 # We also exclude '/' because it is more robust to replace it with a percent264 # encoding despite it not being a requirement of the spec.265 parsed_query: typing.Optional[str] = (264 None if query is None else quote(query, safe=SUB_DELIMS + ":/?[]@")266 None if query is None else quote(query, safe=SUB_DELIMS + ":?[]@")267 )268 # For 'fragment' we can include all of the GEN_DELIMS set.269 parsed_fragment: typing.Optional[str] = (270 None if fragment is None else quote(fragment, safe=SUB_DELIMS + ":/?#[]@")
182 unmodified lines
453 # we don't need to handle a bunch of different typing cases, such as bytes vs str.454 #455 # https://github.com/python/cpython/blob/b2f7b2ef0b5421e01efb8c7bee2ef95d3bab77eb/Lib/urllib/parse.py#L926456 #455 # Note that we use '%20' encoding for spaces, and treat '/' as a safe456 # character. This means our query params have the same escaping as other457 # characters in the URL path. This is slightly different to `requests`,458 # but is the behaviour that browsers use.457 # Note that we use '%20' encoding for spaces. and '%2F for '/'.458 # This is slightly different than `requests`, but is the behaviour that browsers use.459 #460 # See https://github.com/encode/httpx/issues/2536 and461 # https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode462 return "&".join([quote(k) + "=" + quote(v) for k, v in items])460 # See461 # - https://github.com/encode/httpx/issues/2536462 # - https://github.com/encode/httpx/issues/2721463 # - https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode464 return "&".join([quote(k, safe="") + "=" + quote(v, safe="") for k, v in items])
modified:tests/models/test_url.py+2 added-2 removed
358 unmodified lines
359 See https://github.com/encode/httpx/issues/2536360 and https://github.com/encode/httpx/discussions/2460361 """362 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")363 assert url.raw_path == b"/?a=b%20c&d=e/f"363 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"364365 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})366 assert url.raw_path == b"/?a=b%20c&d=e/f"366 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"367368369def test_url_with_url_encoded_path():370 url = httpx.URL("https://www.example.com/path%20to%20somewhere")
54 unmodified lines
modified:tests/test_urlparse.py+3 added-3 removed
139 unmodified lines
140141142def test_param_with_existing_escape_requires_encoding():143 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})144 assert str(url) == "http://webservice?u=http%3A//example.com%3Fq%3Dfoo%252Fa"144 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"145146147# Tests for invalid URLs148
114 unmodified lines
263264def 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:/[]@?"267 assert url.raw_path == b"/?!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"268 assert url.path == "/"269 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?"269 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"270 assert url.fragment == ""271272273def test_fragment_percent_encoding():
6 unmodified lines