Skip to content
Myr

Fix optional percent encoding behaviour. (#2671)

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

* Tests for failing optional percent encoding * Linting * Fix for optional percent escaping

authored by Tom Christie · committed by GitHub · parent 15d09a3 · Browse files · 2 files+40 added−6 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−6 removed

  • tests:+18 added−0 removed
  • other:+22 added−6 removed

2 files+40 added−6 removed

modified:httpx/_urlparse.py+22 added-6 removed
397 unmodified lines
398399400def percent_encode(char: str) -> str:401 """402 Replace every character in a string with the percent-encoded representation.402 Replace a single character with the percent-encoded representation.403404 Characters outside the ASCII range are represented with their a percent-encoded405 representation of their UTF-8 byte sequence.406
3 unmodified lines
410 """411 return "".join([f"%{byte:02x}" for byte in char.encode("utf-8")]).upper()412413414def is_safe(string: str, safe: str = "/") -> bool:415 """416 Determine if a given string is already quote-safe.417 """418 NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe + "%"419420 # All characters must already be non-escaping or '%'421 for char in string:422 if char not in NON_ESCAPED_CHARS:423 return False424425 # Any '%' characters must be valid '%xx' escape sequences.426 return string.count("%") == len(PERCENT_ENCODED_REGEX.findall(string))427428429def quote(string: str, safe: str = "/") -> str:415 NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe416 if string.count("%") == len(PERCENT_ENCODED_REGEX.findall(string)):417 # If all occurances of '%' are valid '%xx' escapes, then treat418 # percent as a non-escaping character.419 NON_ESCAPED_CHARS += "%"430 """431 Use percent-encoding to quote a string if required.432 """433 if is_safe(string, safe=safe):434 return string435436 NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe437 return "".join(438 [char if char in NON_ESCAPED_CHARS else percent_encode(char) for char in string]439 )440
15 unmodified lines
modified:tests/test_urlparse.py+18 added
124 unmodified lines
125 url = httpx.URL("../abc")126 assert url.path == "../abc"127128129# Tests for optional percent encoding130131132def test_param_requires_encoding():133 url = httpx.URL("http://webservice", params={"u": "with spaces"})134 assert str(url) == "http://webservice?u=with%20spaces"135136137def test_param_does_not_require_encoding():138 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})139 assert str(url) == "http://webservice?u=with%20spaces"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"145146147# Tests for invalid URLs148149150def test_urlparse_excessively_long_url():
99 unmodified lines