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

Fix url parsing of ipv6 urls on URL.replace #1965

Merged
merged 3 commits into from Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion starlette/datastructures.py
Expand Up @@ -114,11 +114,18 @@ def replace(self, **kwargs: typing.Any) -> "URL":
or "hostname" in kwargs
or "port" in kwargs
):
hostname = kwargs.pop("hostname", self.hostname)
hostname = kwargs.pop("hostname", None)
port = kwargs.pop("port", self.port)
username = kwargs.pop("username", self.username)
password = kwargs.pop("password", self.password)

if not hostname:
Kludex marked this conversation as resolved.
Show resolved Hide resolved
netloc = self.netloc
_, _, hostname = netloc.rpartition("@")

if hostname[-1] != "]":
hostname = hostname.rsplit(":", 1)[0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the new approach breaks the existing undocumented, untested functionality...

May I propose a test like this:

assert URL("http://u:p@host/").replace(hostname="bar") == URL("http://u:p@bar/")

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe like this:

assert URL("http://u:p@host:80").replace(port=88) == URL("http://u:p@host:88")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added, these are passing


netloc = hostname
if port is not None:
netloc += f":{port}"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_datastructures.py
Expand Up @@ -38,6 +38,24 @@ def test_url():
assert new == "https://example.com:123/path/to/somewhere?abc=123#anchor"
assert new.hostname == "example.com"

ipv6_url = URL("https://[fe::2]:12345")
new = ipv6_url.replace(port=8080)
assert new == "https://[fe::2]:8080"

new = ipv6_url.replace(username="username", password="password")
assert new == "https://username:password@[fe::2]:12345"
assert new.netloc == "username:password@[fe::2]:12345"

ipv6_url = URL("https://[fe::2]")
new = ipv6_url.replace(port=123)
assert new == "https://[fe::2]:123"

url = URL("http://u:p@host/")
assert url.replace(hostname="bar") == URL("http://u:p@bar/")

url = URL("http://u:p@host:80")
assert url.replace(port=88) == URL("http://u:p@host:88")


def test_url_query_params():
u = URL("https://example.org/path/?page=3")
Expand Down