Skip to content

Commit c34dad2

Browse files
author
Sergey Ninua
committed
Tiny ClientRequest.update_host refactoring
1 parent 9bfb29d commit c34dad2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

aiohttp/client_reqrep.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ def __init__(self, method, url, *,
9797

9898
def update_host(self, url):
9999
"""Update destination host, port and connection type (ssl)."""
100-
scheme, netloc, path, query, fragment = \
101-
url_parsed = urllib.parse.urlsplit(url)
100+
url_parsed = urllib.parse.urlsplit(url)
101+
102+
# check for network location part
103+
netloc = url_parsed.netloc
104+
if not netloc:
105+
raise ValueError('Host could not be detected.')
102106

103107
# get host/port
104108
host = url_parsed.hostname
@@ -108,9 +112,6 @@ def update_host(self, url):
108112
raise ValueError(
109113
'Port number could not be converted.') from None
110114

111-
if not netloc:
112-
raise ValueError('Host could not be detected.')
113-
114115
# check domain idna encoding
115116
try:
116117
netloc = netloc.encode('idna').decode('utf-8')
@@ -127,8 +128,10 @@ def update_host(self, url):
127128
# Record entire netloc for usage in host header
128129
self.netloc = netloc
129130

130-
# extract host and port
131+
scheme = url_parsed.scheme
131132
self.ssl = scheme == 'https'
133+
134+
# set port number if it isn't already set
132135
if not port:
133136
if self.ssl:
134137
port = HTTPS_PORT

0 commit comments

Comments
 (0)