Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 3 additions & 54 deletions src/hip/core/urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,6 @@ class InvalidURL(ValueError):
]
)

# The userinfo percent-encode set is the path percent-encode set
# and U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@),
# U+005B ([) to U+005E (^), inclusive, and U+007C (|).
USERNAME_SAFE = "".join(
[
chr(i)
for i in range(0x20, 0x7F)
if i
not in (0x20, 0x22, 0x23, 0x3C, 0x3E)
+ (0x3F, 0x60, 0x7B, 0x7D)
+ (0x2F, 0x3A, 0x3B, 0x3D, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x7C)
]
)
PASSWORD_SAFE = "".join(
[
chr(i)
for i in range(0x20, 0x7F)
if i
not in (0x20, 0x22, 0x23, 0x3C, 0x3E)
+ (0x3F, 0x60, 0x7B, 0x7D)
+ (0x2F, 0x3A, 0x3B, 0x3D, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x7C)
]
)
# Note... The terminology 'userinfo' percent-encode set in the WHATWG document
# is used for the username and password quoting. For the joint userinfo component
# we remove U+003A (:) from the safe set.
USERINFO_SAFE = "".join(
[
chr(i)
for i in range(0x20, 0x7F)
if i
not in (0x20, 0x22, 0x23, 0x3C, 0x3E)
+ (0x3F, 0x60, 0x7B, 0x7D)
+ (0x2F, 0x3B, 0x3D, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x7C)
]
)


# {scheme}: (optional)
# //{authority} (optional)
# {path}
Expand Down Expand Up @@ -142,7 +104,6 @@ class InvalidURL(ValueError):
"path": re.compile("[^?#]*"),
"query": re.compile("[^#]*"),
"fragment": re.compile(".*"),
"userinfo": re.compile("[^@]*"),
"host": re.compile("(\\[.*\\]|[^:]*)"),
"port": re.compile(".*"),
}
Expand All @@ -156,7 +117,6 @@ class InvalidURL(ValueError):

class ParseResult(typing.NamedTuple):
scheme: str
userinfo: str
host: str
port: int | None
path: str
Expand All @@ -167,7 +127,6 @@ class ParseResult(typing.NamedTuple):
def authority(self) -> str:
return "".join(
[
f"{self.userinfo}@" if self.userinfo else "",
f"[{self.host}]" if ":" in self.host else self.host,
f":{self.port}" if self.port is not None else "",
]
Expand Down Expand Up @@ -240,12 +199,6 @@ def urlparse(url: str = "", **kwargs: str | None) -> ParseResult:
netloc = kwargs.pop("netloc") or ""
kwargs["host"], _, kwargs["port"] = netloc.partition(":")

# Replace "username" and/or "password" with "userinfo".
if "username" in kwargs or "password" in kwargs:
username = quote(kwargs.pop("username", "") or "", safe=USERNAME_SAFE)
password = quote(kwargs.pop("password", "") or "", safe=PASSWORD_SAFE)
kwargs["userinfo"] = f"{username}:{password}" if password else username

# Replace "raw_path" with "path" and "query".
if "raw_path" in kwargs:
raw_path = kwargs.pop("raw_path") or ""
Expand Down Expand Up @@ -305,24 +258,21 @@ def urlparse(url: str = "", **kwargs: str | None) -> ParseResult:
assert authority_match is not None
authority_dict = authority_match.groupdict()

# * 'userinfo' and 'host' may be empty strings.
# Userinfo is recognized by AUTHORITY_REGEX but deliberately discarded.
# * 'host' may be an empty string.
# * 'port' may be 'None'.
userinfo = kwargs.get("userinfo", authority_dict["userinfo"]) or ""
host = kwargs.get("host", authority_dict["host"]) or ""
port = kwargs.get("port", authority_dict["port"])

# Normalize and validate each component.
# We end up with a parsed representation of the URL,
# with components that are plain ASCII bytestrings.
parsed_scheme: str = scheme.lower()
parsed_userinfo: str = quote(userinfo, safe=USERINFO_SAFE)
parsed_host: str = encode_host(host)
parsed_port: int | None = normalize_port(port, scheme)

has_scheme = parsed_scheme != ""
has_authority = (
parsed_userinfo != "" or parsed_host != "" or parsed_port is not None
)
has_authority = parsed_host != "" or parsed_port is not None
validate_path(path, has_scheme=has_scheme, has_authority=has_authority)
if has_scheme or has_authority:
path = normalize_path(path)
Expand All @@ -335,7 +285,6 @@ def urlparse(url: str = "", **kwargs: str | None) -> ParseResult:
# All properties of the URL are derived from these.
return ParseResult(
parsed_scheme,
parsed_userinfo,
parsed_host,
parsed_port,
parsed_path,
Expand Down
42 changes: 6 additions & 36 deletions src/hip/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class URL:
"""
The components of a URL are broken down like this:

https://jo%40email.com:a%20secret@müller.de:1234/pa%20th?search=ab#anchorlink
[scheme] [ username ] [password] [ host ][port][ path ] [ query ] [fragment]
[ userinfo ] [ netloc ][ raw_path ]
https://example.com:1234/pa%20th?search=ab#anchorlink
[scheme] [ host ][port][ path ] [ query ] [fragment]
[ netloc ][ target ]

Note that:

Expand Down Expand Up @@ -44,8 +44,7 @@ class URL:
assert hip.URL("http://example.com").port is None
assert hip.URL("http://example.com:80").port is None

* `url.userinfo` is raw bytes, without URL escaping. Usually you'll want to work
with `url.username` and `url.password` instead, which handle the URL escaping.
* User information in the URL is ignored and omitted from the resulting URL.

* `url.raw_path` is raw bytes of both the path and query, without URL escaping.
This portion is used as the target when constructing HTTP requests. Usually you'll
Expand All @@ -60,9 +59,6 @@ def __init__(self, url: URL | str = "", **kwargs: typing.Any) -> None:
if kwargs:
allowed = {
"scheme": str,
"username": str,
"password": str,
"userinfo": bytes,
"host": str,
"port": int,
"netloc": str,
Expand Down Expand Up @@ -113,32 +109,6 @@ def scheme(self) -> str:
"""
return self._uri_reference.scheme

@property
def userinfo(self) -> bytes:
"""
The URL userinfo as a raw bytestring.
For example: b"jo%40email.com:a%20secret".
"""
return self._uri_reference.userinfo.encode("ascii")

@property
def username(self) -> str:
"""
The URL username as a string, with URL decoding applied.
For example: "jo@email.com"
"""
userinfo = self._uri_reference.userinfo
return unquote(userinfo.partition(":")[0])

@property
def password(self) -> str:
"""
The URL password as a string, with URL decoding applied.
For example: "a secret"
"""
userinfo = self._uri_reference.userinfo
return unquote(userinfo.partition(":")[2])

@property
def host(self) -> str:
"""
Expand Down Expand Up @@ -280,9 +250,9 @@ def copy_with(self, **kwargs: typing.Any) -> URL:
For example:

url = hip.URL("https://www.example.com").copy_with(
username="jo@gmail.com", password="a secret"
path="/search"
)
assert url == "https://jo%40email.com:a%20secret@www.example.com"
assert url == "https://www.example.com/search"
"""
return URL(self, **kwargs)

Expand Down
54 changes: 47 additions & 7 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ def test_url_properties():
)

assert url.scheme == "https"
assert url.username == "jo@email.com"
assert url.password == "a secret"
assert url.userinfo == b"jo%40email.com:a%20secret"
assert str(url) == "https://muller.de:1234/pa%20th?search=ab#anchorlink"
assert url.host == "muller.de"
assert url.netloc == "muller.de:1234"
assert url.port == 1234
Expand Down Expand Up @@ -136,11 +134,53 @@ def test_url_constructor_accepts_netloc():
assert str(url) == "//example.com:8080"


def test_url_constructor_builds_userinfo_from_username_and_password():
url = hip.URL(
host="example.com", port=8080, username="user@example.com", password="a secret"
@pytest.mark.parametrize(
"userinfo",
["user:password", "user", ":password", "", "user%40example.com:a%20secret"],
)
@pytest.mark.parametrize("host", ["example.com:8080", "[::1]:8080"])
def test_url_ignores_userinfo(userinfo, host):
url = hip.URL(f"https://{userinfo}@{host}/path?q=1#fragment")
expected = hip.URL(f"https://{host}/path?q=1#fragment")
assert str(url) == str(expected)
assert repr(url) == repr(expected)
assert url == expected
assert hash(url) == hash(expected)
assert hip.URL(url) == expected
assert url.copy_with(path="/other") == expected.copy_with(path="/other")
assert url.join("next") == expected.join("next")


def test_url_join_ignores_userinfo():
url = hip.URL("https://example.com/base")
assert url.join("//user%40example.com:a%20secret@other.example/path") == (
"https://other.example/path"
)
assert url.userinfo == b"user%40example.com:a%20secret"


@pytest.mark.parametrize("name", ["username", "password", "userinfo"])
def test_url_has_no_userinfo_properties(name):
url = hip.URL("https://user%40example.com:a%20secret@example.com/path")
with pytest.raises(AttributeError):
getattr(url, name)


@pytest.mark.parametrize(
"kwargs",
[
{"username": "user@example.com"},
{"password": "a secret"},
{"userinfo": b"user%40example.com:a%20secret"},
],
)
def test_url_rejects_userinfo_kwargs(kwargs):
url = hip.URL("https://example.com/path")
name = next(iter(kwargs))
msg = re.escape(f"{name!r} is an invalid keyword argument for URL()")
with pytest.raises(TypeError, match=msg):
hip.URL("https://example.com/path", **kwargs)
with pytest.raises(TypeError, match=msg):
url.copy_with(**kwargs)


def test_url_constructor_accepts_raw_path():
Expand Down
Loading