Skip to content

Commit

Permalink
Fix httpx is not encoding with symbol '%s' (encode#3140)
Browse files Browse the repository at this point in the history
Add parsing symbol '%s' test

rewrite fn::_urlparse::is_safe
  • Loading branch information
Tunglies committed Mar 15, 2024
1 parent 7df47ce commit 7424984
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 6 additions & 6 deletions httpx/_urlparse.py
Expand Up @@ -424,13 +424,13 @@ def is_safe(string: str, safe: str = "/") -> bool:
"""
Determine if a given string is already quote-safe.
"""
NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe + "%"

# All characters must already be non-escaping or '%'
for char in string:
if char not in NON_ESCAPED_CHARS:
NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe
# All characters must already be non-escaping or valid percent-encoded
for index, char in enumerate(string):
if char not in NON_ESCAPED_CHARS and not PERCENT_ENCODED_REGEX.match(
string[index : index + 3]
):
return False

return True


Expand Down
2 changes: 2 additions & 0 deletions tests/models/test_url.py
Expand Up @@ -284,6 +284,8 @@ def test_url_leading_dot_prefix_on_relative_url():
def test_param_requires_encoding():
url = httpx.URL("http://webservice", params={"u": "with spaces"})
assert str(url) == "http://webservice?u=with%20spaces"
url = httpx.URL("http://webservice", params={"u": "%"})
assert str(url) == "http://webservice?u=%25"


def test_param_does_not_require_encoding():
Expand Down

0 comments on commit 7424984

Please sign in to comment.