Skip to content

Commit

Permalink
Fix httpx is not encoding with symbol '%s' (#3140)
Browse files Browse the repository at this point in the history
Add parsing symbol '%s' test
  • Loading branch information
Tunglies committed Mar 14, 2024
1 parent 7df47ce commit 678a3d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 16 additions & 6 deletions httpx/_urlparse.py
Expand Up @@ -424,13 +424,23 @@ 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
i = 0
while i < len(string):
char = string[i]
if char in NON_ESCAPED_CHARS:
i += 1
continue
# Check if the character is a start of a valid percent-encoded sequence
elif (
char == "%"
and i + 2 < len(string)
and string[i + 1 : i + 3].isalnum()
and len(string[i + 1 : i + 3]) == 2
):
i += 3
else:
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 678a3d3

Please sign in to comment.