Skip to content

Commit

Permalink
Resolve query param encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed May 2, 2024
1 parent 6b0bec4 commit 39966fa
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions httpx/_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,44 +405,22 @@ def normalize_path(path: str) -> str:
return "/".join(output)


def percent_encode(char: str) -> str:
"""
Replace a single character with the percent-encoded representation.
Characters outside the ASCII range are represented with their a percent-encoded
representation of their UTF-8 byte sequence.
For example:
percent_encode(" ") == "%20"
"""
return "".join([f"%{byte:02x}" for byte in char.encode("utf-8")]).upper()


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:
return False

return True
def PERCENT(string: str) -> str:
return "".join([f"%{byte:02X}" for byte in string.encode("utf-8")])


def percent_encoded(string: str, safe: str = "/") -> str:
"""
Use percent-encoding to quote a string.
"""
if is_safe(string, safe=safe):
NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe

# Fast path for strings that don't need escaping.
if not string.rstrip(NON_ESCAPED_CHARS):
return string

NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe
return "".join(
[char if char in NON_ESCAPED_CHARS else percent_encode(char) for char in string]
[char if char in NON_ESCAPED_CHARS else PERCENT(char) for char in string]
)


Expand Down

0 comments on commit 39966fa

Please sign in to comment.