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
3 changes: 3 additions & 0 deletions src/rfc3986/_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def authority_info(self) -> _AuthorityInfo:
# valid bytes, it is an InvalidAuthority.
raise exc.InvalidAuthority(self.authority.encode(self.encoding))

if matches.get("port") == "":
matches["port"] = None

return matches

def _match_subauthority(self) -> t.Optional[t.Match[str]]:
Expand Down
2 changes: 1 addition & 1 deletion src/rfc3986/abnf_regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
USERINFO_RE = (
"^([" + UNRESERVED_RE + SUB_DELIMITERS_RE + ":]|%s)+" % (PCT_ENCODED)
)
PORT_RE = "[0-9]{1,5}"
PORT_RE = "[0-9]{0,5}"

# ####################
# Path Matcher Section
Expand Down
24 changes: 24 additions & 0 deletions tests/test_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,27 @@ def test_wide_domain_bypass_check():
ref = URIReference.from_string(url)
assert ref.scheme == "https"
assert ref.host == "xdavidhu.me"


@pytest.mark.parametrize(
("raw", "expected_host", "expected_normalized"),
[
("HTTP://example.com:/a", "example.com", "http://example.com/a"),
(
"HTTPS://user@EXAMPLE.COM:/a/b/../c",
"EXAMPLE.COM",
"https://user@example.com/a/c",
),
("http://[::1]:/path", "[::1]", "http://[::1]/path"),
],
)
def test_empty_port_preserves_host_on_normalize(
raw, expected_host, expected_normalized
):
"""RFC 3986 section 3.2.3 & 6.2.3: empty port must preserve host."""
ref = URIReference.from_string(raw)
assert ref.host == expected_host
assert ref.port is None
assert ref.authority_info()["host"] == expected_host
assert ref.authority_info()["port"] is None
assert ref.normalize().unsplit() == expected_normalized