Skip to content
Myr

Don't include username/password components in `Host` header (#417)

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

* removed auth and port from host of header * used URL attribute rather _uri_reference * reverted removing port into host * reverted username and password from header * applied new copy_with with username and password

authored by Can Sarıgöl · committed by Tom Christie · parent e6da325 · Browse files · 2 files+27 added−1 removed
This commit

State

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

Diff stats

2 files+27 added−1 removed

  • tests:+19 added−0 removed
  • other:+8 added−1 removed

2 files+27 added−1 removed

modified:httpx/models.py+8 added-1 removed
135 unmodified lines
136 def authority(self) -> str:137 return self._uri_reference.authority or ""138139 @property140 def userinfo(self) -> str:141 return self._uri_reference.userinfo or ""142143 @property144 def username(self) -> str:145 userinfo = self._uri_reference.userinfo or ""146 return userinfo.partition(":")[0]147
490 unmodified lines
638 has_accept_encoding = "accept-encoding" in self.headers639 has_connection = "connection" in self.headers640641 if not has_host:642 url = self.url643 if url.userinfo:644 url = url.copy_with(username=None, password=None)638 auto_headers.append((b"host", self.url.authority.encode("ascii")))645 auto_headers.append((b"host", url.authority.encode("ascii")))646 if not has_user_agent:647 auto_headers.append((b"user-agent", USER_AGENT.encode("ascii")))648 if not has_accept:649 auto_headers.append((b"accept", b"*/*"))
672 unmodified lines
modified:tests/client/test_headers.py+19 added
129 unmodified lines
130def test_header_does_not_exist():131 headers = models.Headers({"foo": "bar"})132 with pytest.raises(KeyError):133 del headers["baz"]134135136def test_host_without_auth_in_header():137 url = "http://username:password@example.org:80/echo_headers"138139 with Client(dispatch=MockDispatch()) as client:140 response = client.get(url)141142 assert response.status_code == 200143 assert response.json() == {144 "headers": {145 "accept": "*/*",146 "accept-encoding": "gzip, deflate, br",147 "connection": "keep-alive",148 "host": "example.org:80",149 "user-agent": f"python-httpx/{__version__}",150 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",151 }152 }