Skip to content
Myr

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

3 files+24 added−2 removed

modified:httpx/client.py+5 added
328 unmodified lines
329 location = response.headers["Location"]330331 url = URL(location, allow_relative=True)332333 # 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)337338 # 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 lines
modified:httpx/models.py+5 added-2 removed
199 unmodified lines
200 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)207208 authority = host
6 unmodified lines
215 authority = f"{userpass}@{authority}"216217 kwargs["authority"] = authority218219 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 )223224 def join(self, relative_url: URLTypes) -> "URL":225 """226 Return an absolute URL, using given this URL as the base.
981 unmodified lines
modified:tests/client/test_redirects.py+14 added
54 unmodified lines
55 elif request.url.path == "/relative_redirect":56 headers = {"location": "/"}57 return Response(codes.SEE_OTHER, headers=headers, request=request)5859 elif request.url.path == "/malformed_redirect":60 headers = {"location": "https://:443/"}61 return Response(codes.SEE_OTHER, headers=headers, request=request)6263 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 lines
180 assert len(response.history) == 1181182183@pytest.mark.usefixtures("async_environment")184async 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) == 1191192193@pytest.mark.usefixtures("async_environment")194async 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.OK
209 unmodified lines