Skip to content
Myr

Fix Host header and HSTS when the default port is explicitly included in URL (#649)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 6c69e09 · Browse files · 4 files+41 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+41 added−7 removed

  • tests:+33 added−4 removed
  • other:+8 added−3 removed

4 files+41 added−7 removed

modified:httpx/client.py+2 added-1 removed
336 unmodified lines
337 to create the URL used for the outgoing request.338 """339 url = self.base_url.join(relative_url=url)340 if url.scheme == "http" and hstspreload.in_hsts_preload(url.host):341 port = None if url.port == 80 else url.port341 url = url.copy_with(scheme="https")342 url = url.copy_with(scheme="https", port=port)343 return url344345 def merge_cookies(346 self, cookies: CookieTypes = None
637 unmodified lines
modified:httpx/models.py+6 added-2 removed
110 unmodified lines
111 return self._uri_reference.scheme or ""112113 @property114 def authority(self) -> str:115 port_str = self._uri_reference.port116 default_port_str = {"https": "443", "http": "80"}.get(self.scheme, "")117 if port_str is None or port_str == default_port_str:118 return self._uri_reference.host or ""119 return self._uri_reference.authority or ""120121 @property122 def userinfo(self) -> str:
83 unmodified lines
206207 authority = host208 if port is not None:209 authority += f":{port}"206 if username is not None:210 if username:211 userpass = username208 if password is not None:212 if password:213 userpass += f":{password}"214 authority = f"{userpass}@{authority}"215216 kwargs["authority"] = authority
900 unmodified lines
modified:tests/client/test_headers.py+31 added-2 removed
128 unmodified lines
129 del headers["baz"]130131132@pytest.mark.asyncio133async def test_host_without_auth_in_header():133async def test_host_with_auth_and_port_in_url():134 """135 The Host header should only include the hostname, or hostname:port136 (for non-default ports only). Any userinfo or default port should not137 be present.138 """139 url = "http://username:password@example.org:80/echo_headers"140141 client = Client(dispatch=MockDispatch())142 response = await client.get(url)
3 unmodified lines
146 "headers": {147 "accept": "*/*",148 "accept-encoding": "gzip, deflate, br",149 "connection": "keep-alive",145 "host": "example.org:80",150 "host": "example.org",151 "user-agent": f"python-httpx/{__version__}",152 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",153 }154 }155156157@pytest.mark.asyncio158async def test_host_with_non_default_port_in_url():159 """160 If the URL includes a non-default port, then it should be included in161 the Host header.162 """163 url = "http://username:password@example.org:123/echo_headers"164165 client = Client(dispatch=MockDispatch())166 response = await client.get(url)167168 assert response.status_code == 200169 assert response.json() == {170 "headers": {171 "accept": "*/*",172 "accept-encoding": "gzip, deflate, br",173 "connection": "keep-alive",174 "host": "example.org:123",175 "user-agent": f"python-httpx/{__version__}",176 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",177 }178 }
modified:tests/models/test_url.py+2 added-2 removed
71 unmodified lines
72 assert (73 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"74 )7576 new = url.copy_with(scheme="http")77 assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")76 new = url.copy_with(scheme="http", port=None)77 assert new == URL("http://example.org/path/to/somewhere?abc=123#anchor")78 assert new.scheme == "http"798081def test_url_eq_str():
124 unmodified lines