feat: add encode_percent function and update imports in urlparse module

This commit is contained in:
Bakyt Niiazaliev 2025-06-13 17:30:54 +07:00
parent 18047ea4b8
commit 4b83ffa98f
2 changed files with 19 additions and 45 deletions

View File

@ -10,6 +10,7 @@ QueryParamTypes = typing.Union[
bytes,
]
class QueryParams(typing.Mapping[str, str]):
def __init__(self, *args: QueryParamTypes | None, **kwargs: typing.Any) -> None:...
def keys(self) -> typing.KeysView[str]:
@ -151,6 +152,7 @@ class QueryParams(typing.Mapping[str, str]):
def update(self, params: QueryParamTypes | None = None) -> None:...
def __setitem__(self, key: str, value: str) -> None:...
def normalize_path(path: str) -> str:
"""
Drop "." and ".." segments from a URL path.
@ -159,3 +161,6 @@ def normalize_path(path: str) -> str:
normalize_path("/path/./to/somewhere/..") == "/path/to"
"""
def encode_percent(string: str, safe: str) -> str:...

View File

@ -25,7 +25,7 @@ import typing
import idna
from ._exceptions import InvalidURL
from ._httpx import normalize_path
from ._httpx import normalize_path, encode_percent as percent_encoded
MAX_URL_LENGTH = 65536
@ -445,54 +445,23 @@ def validate_path(path: str, has_scheme: bool, has_authority: bool) -> None:
raise InvalidURL("Relative URLs cannot have a path starting with ':'")
# def normalize_path(path: str) -> str:
# 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:
# """
# Drop "." and ".." segments from a URL path.
# For example:
# normalize_path("/path/./to/somewhere/..") == "/path/to"
# Use percent-encoding to quote a string.
# """
# # Fast return when no '.' characters in the path.
# if "." not in path:
# return path
# NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe
# components = path.split("/")
# # Fast path for strings that don't need escaping.
# if not string.rstrip(NON_ESCAPED_CHARS):
# return string
# # Fast return when no '.' or '..' components in the path.
# if "." not in components and ".." not in components:
# return path
# # https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
# output: list[str] = []
# for component in components:
# if component == ".":
# pass
# elif component == "..":
# if output and output != [""]:
# output.pop()
# else:
# output.append(component)
# return "/".join(output)
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.
"""
NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe
# Fast path for strings that don't need escaping.
if not string.rstrip(NON_ESCAPED_CHARS):
return string
return "".join(
[char if char in NON_ESCAPED_CHARS else PERCENT(char) for char in string]
)
# return "".join(
# [char if char in NON_ESCAPED_CHARS else PERCENT(char) for char in string]
# )
def quote(string: str, safe: str) -> str: