Skip to content

Commit

Permalink
Improve performance of building URLs with authority (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 27, 2024
1 parent 38ef0f9 commit 07b2e84
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/1163.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of calling :py:meth:`~yarl.URL.build` with ``authority`` -- by :user:`bdraco`.
25 changes: 25 additions & 0 deletions tests/test_url_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ def test_build_with_authority_without_encoding():
assert str(url) == "http://foo:bar@host.com:8000/path"


def test_build_with_authority_empty_host_no_scheme():
url = URL.build(authority="", path="path")
assert str(url) == "path"


def test_build_with_authority_and_only_user():
url = URL.build(scheme="https", authority="user:@foo.com", path="/path")
assert str(url) == "https://user:@foo.com/path"


def test_build_with_authority_with_port():
url = URL.build(scheme="https", authority="foo.com:8080", path="/path")
assert str(url) == "https://foo.com:8080/path"


def test_build_with_authority_with_ipv6():
url = URL.build(scheme="https", authority="[::1]", path="/path")
assert str(url) == "https://[::1]/path"


def test_build_with_authority_with_ipv6_and_port():
url = URL.build(scheme="https", authority="[::1]:81", path="/path")
assert str(url) == "https://[::1]:81/path"


def test_query_str():
u = URL.build(scheme="http", host="127.0.0.1", path="/", query_string="arg=value1")
assert str(u) == "http://127.0.0.1/?arg=value1"
Expand Down
11 changes: 8 additions & 3 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,15 @@ def build(
if encoded:
netloc = authority
else:
tmp = SplitResult("", authority, "", "", "")
port = None if tmp.port == DEFAULT_PORTS.get(scheme) else tmp.port
_user, _password, _host, _port = cls._split_netloc(authority)
port = None if _port == DEFAULT_PORTS.get(scheme) else _port
_host = (
""
if _host is None
else cls._encode_host(_host, validate_host=False)
)
netloc = cls._make_netloc(
tmp.username, tmp.password, tmp.hostname, port, encode=True
_user, _password, _host, port, encode=True, encode_host=False
)
elif not user and not password and not host and not port:
netloc = ""
Expand Down

0 comments on commit 07b2e84

Please sign in to comment.