Skip to content
Myr

Review URL percent escaping sets, from whatwg. (#3371)

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 489fef4 · Browse files · 2 files+71 added−27 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+71 added−27 removed

  • tests:+3 added−3 removed
  • other:+68 added−24 removed

2 files+71 added−27 removed

modified:httpx/_urlparse.py+68 added-24 removed
34 unmodified lines
35SUB_DELIMS = "!$&'()*+,;="3637PERCENT_ENCODED_REGEX = re.compile("%[A-Fa-f0-9]{2}")3839# https://url.spec.whatwg.org/#percent-encoded-bytes4041# The fragment percent-encode set is the C0 control percent-encode set42# and U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>), and U+0060 (`).43FRAG_SAFE = "".join(44 [chr(i) for i in range(0x20, 0x7F) if i not in (0x20, 0x22, 0x3C, 0x3E, 0x60)]45)4647# The query percent-encode set is the C0 control percent-encode set48# and U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), and U+003E (>).49QUERY_SAFE = "".join(50 [chr(i) for i in range(0x20, 0x7F) if i not in (0x20, 0x22, 0x23, 0x3C, 0x3E)]51)5253# The path percent-encode set is the query percent-encode set54# and U+003F (?), U+0060 (`), U+007B ({), and U+007D (}).55PATH_SAFE = "".join(56 [57 chr(i)58 for i in range(0x20, 0x7F)59 if i not in (0x20, 0x22, 0x23, 0x3C, 0x3E) + (0x3F, 0x60, 0x7B, 0x7D)60 ]61)6263# The userinfo percent-encode set is the path percent-encode set64# and U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@),65# U+005B ([) to U+005E (^), inclusive, and U+007C (|).66USERNAME_SAFE = "".join(67 [68 chr(i)69 for i in range(0x20, 0x7F)70 if i71 not in (0x20, 0x22, 0x23, 0x3C, 0x3E)72 + (0x3F, 0x60, 0x7B, 0x7D)73 + (0x2F, 0x3A, 0x3B, 0x3D, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x7C)74 ]75)76PASSWORD_SAFE = "".join(77 [78 chr(i)79 for i in range(0x20, 0x7F)80 if i81 not in (0x20, 0x22, 0x23, 0x3C, 0x3E)82 + (0x3F, 0x60, 0x7B, 0x7D)83 + (0x2F, 0x3A, 0x3B, 0x3D, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x7C)84 ]85)86# Note... The terminology 'userinfo' percent-encode set in the WHATWG document87# is used for the username and password quoting. For the joint userinfo component88# we remove U+003A (:) from the safe set.89USERINFO_SAFE = "".join(90 [91 chr(i)92 for i in range(0x20, 0x7F)93 if i94 not in (0x20, 0x22, 0x23, 0x3C, 0x3E)95 + (0x3F, 0x60, 0x7B, 0x7D)96 + (0x2F, 0x3B, 0x3D, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x7C)97 ]98)99100# {scheme}: (optional)101# //{authority} (optional)102# {path}103# ?{query} (optional)
137 unmodified lines
241 kwargs["host"], _, kwargs["port"] = netloc.partition(":")242243 # Replace "username" and/or "password" with "userinfo".244 if "username" in kwargs or "password" in kwargs:185 username = quote(kwargs.pop("username", "") or "")186 password = quote(kwargs.pop("password", "") or "")245 username = quote(kwargs.pop("username", "") or "", safe=USERNAME_SAFE)246 password = quote(kwargs.pop("password", "") or "", safe=PASSWORD_SAFE)247 kwargs["userinfo"] = f"{username}:{password}" if password else username248249 # Replace "raw_path" with "path" and "query".250 if "raw_path" in kwargs:
46 unmodified lines
297 scheme = kwargs.get("scheme", url_dict["scheme"]) or ""298 authority = kwargs.get("authority", url_dict["authority"]) or ""299 path = kwargs.get("path", url_dict["path"]) or ""300 query = kwargs.get("query", url_dict["query"])241 fragment = kwargs.get("fragment", url_dict["fragment"])301 frag = kwargs.get("fragment", url_dict["fragment"])302303 # The AUTHORITY_REGEX will always match, but may have empty components.304 authority_match = AUTHORITY_REGEX.match(authority)305 assert authority_match is not None
8 unmodified lines
314 # Normalize and validate each component.315 # We end up with a parsed representation of the URL,316 # with components that are plain ASCII bytestrings.317 parsed_scheme: str = scheme.lower()258 parsed_userinfo: str = quote(userinfo, safe=SUB_DELIMS + ":")318 parsed_userinfo: str = quote(userinfo, safe=USERINFO_SAFE)319 parsed_host: str = encode_host(host)320 parsed_port: int | None = normalize_port(port, scheme)321322 has_scheme = parsed_scheme != ""
3 unmodified lines
326 validate_path(path, has_scheme=has_scheme, has_authority=has_authority)327 if has_scheme or has_authority:328 path = normalize_path(path)329270 # The GEN_DELIMS set is... : / ? # [ ] @271 # These do not need to be percent-quoted unless they serve as delimiters for the272 # specific component.273 WHATWG_SAFE = '`{}%|^\\"'330 parsed_path: str = quote(path, safe=PATH_SAFE)331 parsed_query: str | None = None if query is None else quote(query, safe=QUERY_SAFE)332 parsed_frag: str | None = None if frag is None else quote(frag, safe=FRAG_SAFE)333275 # For 'path' we need to drop ? and # from the GEN_DELIMS set.276 parsed_path: str = quote(path, safe=SUB_DELIMS + WHATWG_SAFE + ":/[]@")277 # For 'query' we need to drop '#' from the GEN_DELIMS set.278 parsed_query: str | None = (279 None280 if query is None281 else quote(query, safe=SUB_DELIMS + WHATWG_SAFE + ":/?[]@")282 )283 # For 'fragment' we can include all of the GEN_DELIMS set.284 parsed_fragment: str | None = (285 None286 if fragment is None287 else quote(fragment, safe=SUB_DELIMS + WHATWG_SAFE + ":/?#[]@")288 )289334 # The parsed ASCII bytestrings are our canonical form.335 # All properties of the URL are derived from these.336 return ParseResult(337 parsed_scheme,
1 unmodified line
339 parsed_host,340 parsed_port,341 parsed_path,342 parsed_query,299 parsed_fragment,343 parsed_frag,344 )345346347def encode_host(host: str) -> str:
202 unmodified lines
modified:tests/models/test_url.py+3 added-3 removed
292 unmodified lines
293 url = httpx.URL("http://webservice", params={"u": "with spaces"})294 assert str(url) == "http://webservice?u=with%20spaces"295296297def test_param_does_not_require_encoding():297def test_param_requires_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
311 unmodified lines
613 "password": "abc123@ %",614 }615 url = httpx.URL("https://example.org")616 new = url.copy_with(**copy_with_kwargs)617 assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"617 assert str(new) == "https://tom%40example.org:abc123%40%20%@example.org"618 assert new.username == "tom@example.org"619 assert new.password == "abc123@ %"620 assert new.userinfo == b"tom%40example.org:abc123%40%20%25"620 assert new.userinfo == b"tom%40example.org:abc123%40%20%"621622623def test_url_copywith_invalid_component():624 url = httpx.URL("https://example.org")
243 unmodified lines