Handle redirect with malformed Location headers missing host. (#774)
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 5a63540 · Browse files · 3 files·+24 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−2 removed
- tests:+14 added−0 removed
- other:+10 added−2 removed
Provenance
- Commit
1f8fb28- Tree
10844c0- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
5a63540
3 files changed+24 added−2 removed
modified:httpx/client.py+5 added
328 unmodified lines329 location = response.headers["Location"]330 331 url = URL(location, allow_relative=True)332 333+ # Handle malformed 'Location' headers that are "absolute" form, have no host.334+ # See: https://github.com/encode/httpx/issues/771335+ if url.scheme and not url.host:336+ url = url.copy_with(host=request.url.host)337+338 # Facilitate relative 'Location' headers, as allowed by RFC 7231.339 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')340 if url.is_relative_url:341 url = request.url.join(url)1131 unmodified linesmodified:httpx/models.py+5 added-2 removed
199 unmodified lines200 or "host" in kwargs201 or "port" in kwargs202 ):203 host = kwargs.pop("host", self.host)204- port = kwargs.pop("port", self.port)204+ port = kwargs.pop("port", None if self.is_relative_url else self.port)205 username = kwargs.pop("username", self.username)206 password = kwargs.pop("password", self.password)207 208 authority = host6 unmodified lines215 authority = f"{userpass}@{authority}"216 217 kwargs["authority"] = authority218 219- return URL(self._uri_reference.copy_with(**kwargs).unsplit())219+ return URL(220+ self._uri_reference.copy_with(**kwargs).unsplit(),221+ allow_relative=self.is_relative_url,222+ )223 224 def join(self, relative_url: URLTypes) -> "URL":225 """226 Return an absolute URL, using given this URL as the base.981 unmodified linesmodified:tests/client/test_redirects.py+14 added
54 unmodified lines55 elif request.url.path == "/relative_redirect":56 headers = {"location": "/"}57 return Response(codes.SEE_OTHER, headers=headers, request=request)58 59+ elif request.url.path == "/malformed_redirect":60+ headers = {"location": "https://:443/"}61+ return Response(codes.SEE_OTHER, headers=headers, request=request)62+63 elif request.url.path == "/no_scheme_redirect":64 headers = {"location": "//example.org/"}65 return Response(codes.SEE_OTHER, headers=headers, request=request)66 113 unmodified lines180 assert len(response.history) == 1181 182 183 @pytest.mark.usefixtures("async_environment")184+async def test_malformed_redirect():185+ # https://github.com/encode/httpx/issues/771186+ client = AsyncClient(dispatch=MockDispatch())187+ response = await client.get("http://example.org/malformed_redirect")188+ assert response.status_code == codes.OK189+ assert response.url == URL("https://example.org/")190+ assert len(response.history) == 1191+192+193+@pytest.mark.usefixtures("async_environment")194 async def test_no_scheme_redirect():195 client = AsyncClient(dispatch=MockDispatch())196 response = await client.get("https://example.org/no_scheme_redirect")197 assert response.status_code == codes.OK209 unmodified lines