Skip to content

Commit

Permalink
Avoid re-encoding hosts when changing user/pass/port
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 12, 2024
1 parent dc97f68 commit bbf4088
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,9 @@ def with_user(self, user: Union[str, None]) -> "URL":
raise TypeError("Invalid user type")
if not self.absolute:
raise ValueError("user replacement is not allowed for relative URLs")
encoded_host = self._encode_host(val.hostname) if val.hostname else ""
netloc = self._make_netloc(user, password, encoded_host, val.port)
_, _, host, port = self._split_netloc(val.netloc)
host = f"[{host}]" if host and ":" in host else host
netloc = self._make_netloc(user, password, host, port)
return self._from_val(val._replace(netloc=netloc))

def with_password(self, password: Union[str, None]) -> "URL":
Expand All @@ -1181,8 +1182,9 @@ def with_password(self, password: Union[str, None]) -> "URL":
if not self.absolute:
raise ValueError("password replacement is not allowed for relative URLs")
val = self._val
encoded_host = self._encode_host(val.hostname) if val.hostname else ""
netloc = self._make_netloc(val.username, password, encoded_host, val.port)
user, _, host, port = self._split_netloc(val.netloc)
host = f"[{host}]" if host and ":" in host else host
netloc = self._make_netloc(user, password, host, port)
return self._from_val(val._replace(netloc=netloc))

def with_host(self, host: str) -> "URL":
Expand Down Expand Up @@ -1221,8 +1223,9 @@ def with_port(self, port: Union[int, None]) -> "URL":
if not self.absolute:
raise ValueError("port replacement is not allowed for relative URLs")
val = self._val
encoded_host = self._encode_host(val.hostname) if val.hostname else ""
netloc = self._make_netloc(val.username, val.password, encoded_host, port)
user, password, host, _ = self._split_netloc(val.netloc)
host = f"[{host}]" if host and ":" in host else host
netloc = self._make_netloc(user, password, host, port)
return self._from_val(val._replace(netloc=netloc))

def with_path(self, path: str, *, encoded: bool = False) -> "URL":
Expand Down

0 comments on commit bbf4088

Please sign in to comment.