Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize URL string ports per RFC 3986 section 3.2.3 #1033

Merged
merged 19 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGES/1033.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Normalize default ports according to :rfc:`3986`.
webknjaz marked this conversation as resolved.
Show resolved Hide resolved

Specified ports are removed from the str representation of an URL if the port matches
the schemes default port. -- by :user:`commonism`.
commonism marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@
assert url.authority == "степан:пароль@слава.укр:8080"


def test_authority_unknown_scheme():
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
v = "scheme://user:[email protected]:43/path/to?a=1&b=2"
url = URL(v)
assert str(url) == v

Check warning on line 225 in tests/test_url.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url.py#L222-L225

Added lines #L222 - L225 were not covered by tests


def test_lowercase():
url = URL("http://gitHUB.com")
assert url.raw_host == "github.com"
Expand Down Expand Up @@ -1332,6 +1338,7 @@
def test_is_default_port_for_absolute_url_with_default_port():
url = URL("http://example.com:80")
assert url.is_default_port()
assert str(url) == "http://example.com"

Check warning on line 1341 in tests/test_url.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url.py#L1341

Added line #L1341 was not covered by tests


def test_is_default_port_for_absolute_url_with_nondefault_port():
Expand Down
20 changes: 19 additions & 1 deletion tests/test_url_update_netloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,32 @@
assert str(url.with_port(8888)) == "http://example.com:8888"


def test_with_port_normalization():
commonism marked this conversation as resolved.
Show resolved Hide resolved
url = URL("http://example.com")

Check warning on line 195 in tests/test_url_update_netloc.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url_update_netloc.py#L194-L195

Added lines #L194 - L195 were not covered by tests

assert str(url.with_scheme("https")) == "https://example.com"
assert str(url.with_scheme("https").with_port(443)) == "https://example.com"
assert str(url.with_port(443).with_scheme("https")) == "https://example.com"

Check warning on line 199 in tests/test_url_update_netloc.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url_update_netloc.py#L197-L199

Added lines #L197 - L199 were not covered by tests

u88 = url.with_port(88)
assert str(u88) == "http://example.com:88"
assert str(u88.with_port(80)) == "http://example.com"
assert str(u88.with_scheme("https")) == "https://example.com:88"

Check warning on line 204 in tests/test_url_update_netloc.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url_update_netloc.py#L201-L204

Added lines #L201 - L204 were not covered by tests

u80 = url.with_port(80)
assert str(u80) == "http://example.com"
assert str(u80.with_port(81)) == "http://example.com:81"
assert str(u80.with_scheme("https")) == "https://example.com:80"

Check warning on line 209 in tests/test_url_update_netloc.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url_update_netloc.py#L206-L209

Added lines #L206 - L209 were not covered by tests


def test_with_port_with_no_port():
url = URL("http://example.com")
assert str(url.with_port(None)) == "http://example.com"


def test_with_port_ipv6():
url = URL("http://[::1]:8080/")
assert str(url.with_port(80)) == "http://[::1]:80/"
assert str(url.with_port(81)) == "http://[::1]:81/"

Check warning on line 219 in tests/test_url_update_netloc.py

View check run for this annotation

Codecov / codecov/patch

tests/test_url_update_netloc.py#L219

Added line #L219 was not covered by tests


def test_with_port_keeps_query_and_fragment():
Expand Down
41 changes: 35 additions & 6 deletions yarl/_url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import math
import socket
import warnings
from collections.abc import Mapping, Sequence
from contextlib import suppress
Expand Down Expand Up @@ -291,6 +292,16 @@
val = self._val
if not val.path and self.is_absolute() and (val.query or val.fragment):
val = val._replace(path="/")
if (port := self._get_port()) is None:
commonism marked this conversation as resolved.
Show resolved Hide resolved
val = val._replace(
netloc=self._make_netloc(
self.raw_user,
self.raw_password,
self.raw_host,

Check warning on line 300 in yarl/_url.py

View check run for this annotation

Codecov / codecov/patch

yarl/_url.py#L297-L300

Added lines #L297 - L300 were not covered by tests
port,
encode_host=False,

Check warning on line 302 in yarl/_url.py

View check run for this annotation

Codecov / codecov/patch

yarl/_url.py#L302

Added line #L302 was not covered by tests
)
)
return urlunsplit(val)

def __repr__(self):
Expand Down Expand Up @@ -382,10 +393,15 @@
e.g. 'http://python.org' or 'http://python.org:80', False
otherwise.

Return False for relative URLs
commonism marked this conversation as resolved.
Show resolved Hide resolved

"""
if self.port is None:
return False
default = DEFAULT_PORTS.get(self.scheme)
if self.explicit_port is None:
"""
a relative URL does not have an implicit port / default port
"""
commonism marked this conversation as resolved.
Show resolved Hide resolved
return self.port is not None
default = self._get_default_port()
if default is None:
return False
return self.port == default
Expand Down Expand Up @@ -435,6 +451,21 @@
"""
return self._val.netloc

def _get_default_port(self):
commonism marked this conversation as resolved.
Show resolved Hide resolved
port = None
if self.scheme:
port = DEFAULT_PORTS.get(self.scheme)
if port is None:
with suppress(OSError):
port = socket.getservbyname(self.scheme)
return port
commonism marked this conversation as resolved.
Show resolved Hide resolved

def _get_port(self):
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
"""Port or None if default port"""
if self._get_default_port() == self.port:
return None
return self.port

@cached_property
def authority(self):
"""Decoded authority part of URL.
Expand Down Expand Up @@ -727,9 +758,7 @@
f"Appending path {path!r} starting from slash is forbidden"
)
path = path if encoded else self._PATH_QUOTER(path)
segments = [
segment for segment in reversed(path.split("/")) if segment != "."
commonism marked this conversation as resolved.
Show resolved Hide resolved
]
segments = list(reversed(path.split("/")))
if not segments:
continue
# remove trailing empty segment for all but the last path
Expand Down
Loading