Skip to content

Commit

Permalink
Switch to using URL.host_port_subcomponent in yarl for the client…
Browse files Browse the repository at this point in the history
… host header (#9575)
  • Loading branch information
bdraco authored Oct 28, 2024
1 parent 998204d commit 951def1
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,30 +362,11 @@ def update_headers(self, headers: Optional[LooseHeaders]) -> None:
self.headers: CIMultiDict[str] = CIMultiDict()

# Build the host header
host = self.url.host_subcomponent
host = self.url.host_port_subcomponent

# host_subcomponent is None when the URL is a relative URL.
# host_port_subcomponent is None when the URL is a relative URL.
# but we know we do not have a relative URL here.
assert host is not None

if host[-1] == ".":
# Remove all trailing dots from the netloc as while
# they are valid FQDNs in DNS, TLS validation fails.
# See https://github.com/aio-libs/aiohttp/issues/3636.
# To avoid string manipulation we only call rstrip if
# the last character is a dot.
host = host.rstrip(".")

# If explicit port is not None, it means that the port was
# explicitly specified in the URL. In this case we check
# if its not the default port for the scheme and add it to
# the host header. We check explicit_port first because
# yarl caches explicit_port and its likely to already be
# in the cache and non-default port URLs are far less common.
explicit_port = self.url.explicit_port
if explicit_port is not None and not self.url.is_default_port():
host = f"{host}:{explicit_port}"

self.headers[hdrs.HOST] = host

if not headers:
Expand Down

0 comments on commit 951def1

Please sign in to comment.