Skip to content
Myr

Resolve queryparam quoting (#3187)

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 be56b74 · Browse files · 2 files+55 added−32 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+55 added−32 removed

  • tests:+48 added−3 removed
  • other:+7 added−29 removed

2 files+55 added−32 removed

modified:httpx/_urlparse.py+7 added-29 removed
404 unmodified lines
405 output.append(component)406 return "/".join(output)407408409def percent_encode(char: str) -> str:410 """411 Replace a single character with the percent-encoded representation.409def PERCENT(string: str) -> str:410 return "".join([f"%{byte:02X}" for byte in string.encode("utf-8")])411413 Characters outside the ASCII range are represented with their a percent-encoded414 representation of their UTF-8 byte sequence.412416 For example:417418 percent_encode(" ") == "%20"419 """420 return "".join([f"%{byte:02x}" for byte in char.encode("utf-8")]).upper()421422423def is_safe(string: str, safe: str = "/") -> bool:424 """425 Determine if a given string is already quote-safe.426 """427 NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe + "%"428429 # All characters must already be non-escaping or '%'430 for char in string:431 if char not in NON_ESCAPED_CHARS:432 return False433434 return True435436413def percent_encoded(string: str, safe: str = "/") -> str:414 """415 Use percent-encoding to quote a string.416 """441 if is_safe(string, safe=safe):417 NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe418419 # Fast path for strings that don't need escaping.420 if not string.rstrip(NON_ESCAPED_CHARS):421 return string422444 NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe423 return "".join(446 [char if char in NON_ESCAPED_CHARS else percent_encode(char) for char in string]424 [char if char in NON_ESCAPED_CHARS else PERCENT(char) for char in string]425 )426427428def quote(string: str, safe: str = "/") -> str:
53 unmodified lines
modified:tests/models/test_url.py+48 added-3 removed
227 unmodified lines
228 url = httpx.URL("https://EXAMPLE.com/")229 assert url.host == "example.com"230231232def test_url_percent_escape_host():233 url = httpx.URL("https://exam%le.com/")234 assert url.host == "exam%25le.com"235236237def test_url_ipv4_like_host():238 """rare host names used to quality as IPv4"""239 url = httpx.URL("https://023b76x43144/")240 assert url.host == "023b76x43144"
41 unmodified lines
282 url = httpx.URL("../abc")283 assert url.path == "../abc"284285281# Tests for optional percent encoding286# Tests for query parameter percent encoding.287#288# Percent-encoding in `params={}` should match browser form behavior.289290284def test_param_requires_encoding():291def test_param_with_space():292 # Params passed as form key-value pairs should be escaped.293 url = httpx.URL("http://webservice", params={"u": "with spaces"})294 assert str(url) == "http://webservice?u=with%20spaces"295296297def test_param_does_not_require_encoding():298 # Params passed as form key-value pairs should be escaped.299 url = httpx.URL("http://webservice", params={"u": "%"})300 assert str(url) == "http://webservice?u=%25"301302303def test_param_with_percent_encoded():304 # Params passed as form key-value pairs should always be escaped,305 # even if they include a valid escape sequence.306 # We want to match browser form behaviour here.307 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})291 assert str(url) == "http://webservice?u=with%20spaces"308 assert str(url) == "http://webservice?u=with%2520spaces"309310311def test_param_with_existing_escape_requires_encoding():312 # Params passed as form key-value pairs should always be escaped,313 # even if they include a valid escape sequence.314 # We want to match browser form behaviour here.315 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})316 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"317318319# Tests for query parameter percent encoding.320#321# Percent-encoding in `url={}` should match browser URL bar behavior.322323324def test_query_with_existing_percent_encoding():325 # Valid percent encoded sequences should not be double encoded.326 url = httpx.URL("http://webservice?u=phrase%20with%20spaces")327 assert str(url) == "http://webservice?u=phrase%20with%20spaces"328329330def test_query_requiring_percent_encoding():331 # Characters that require percent encoding should be encoded.332 url = httpx.URL("http://webservice?u=phrase with spaces")333 assert str(url) == "http://webservice?u=phrase%20with%20spaces"334335336def test_query_with_mixed_percent_encoding():337 # When a mix of encoded and unencoded characters are present,338 # characters that require percent encoding should be encoded,339 # while existing sequences should not be double encoded.340 url = httpx.URL("http://webservice?u=phrase%20with spaces")341 assert str(url) == "http://webservice?u=phrase%20with%20spaces"342343344# Tests for invalid URLs345346347def test_url_invalid_hostname():
540 unmodified lines