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
Provenance
- Commit
a7092af- Tree
f71f75a- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
be56b74
2 files changed+55 added−32 removed
modified:httpx/_urlparse.py+7 added-29 removed
404 unmodified lines405 output.append(component)406 return "/".join(output)407 408 409-def percent_encode(char: str) -> str:410- """411- Replace a single character with the percent-encoded representation.409+def PERCENT(string: str) -> str:410+ return "".join([f"%{byte:02X}" for byte in string.encode("utf-8")])411 413- Characters outside the ASCII range are represented with their a percent-encoded414- representation of their UTF-8 byte sequence.412 416- For example:417-418- percent_encode(" ") == "%20"419- """420- return "".join([f"%{byte:02x}" for byte in char.encode("utf-8")]).upper()421-422-423-def 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 + "%"428-429- # All characters must already be non-escaping or '%'430- for char in string:431- if char not in NON_ESCAPED_CHARS:432- return False433-434- return True435-436-413 def 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 + safe418+419+ # Fast path for strings that don't need escaping.420+ if not string.rstrip(NON_ESCAPED_CHARS):421 return string422 444- 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 )426 427 428 def quote(string: str, safe: str = "/") -> str:53 unmodified linesmodified:tests/models/test_url.py+48 added-3 removed
227 unmodified lines228 url = httpx.URL("https://EXAMPLE.com/")229 assert url.host == "example.com"230 231 232+def test_url_percent_escape_host():233+ url = httpx.URL("https://exam%le.com/")234+ assert url.host == "exam%25le.com"235+236+237 def 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 lines282 url = httpx.URL("../abc")283 assert url.path == "../abc"284 285 281-# Tests for optional percent encoding286+# Tests for query parameter percent encoding.287+#288+# Percent-encoding in `params={}` should match browser form behavior.289 290 284-def test_param_requires_encoding():291+def 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"295 296 297 def 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"301+302+303+def 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"309 310 311 def 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"317 318 319+# Tests for query parameter percent encoding.320+#321+# Percent-encoding in `url={}` should match browser URL bar behavior.322+323+324+def 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"328+329+330+def 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"334+335+336+def 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"342+343+344 # Tests for invalid URLs345 346 347 def test_url_invalid_hostname():540 unmodified lines