Remove "Host" header on redirects if URL origin has changed (#199)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #198 * Reinstate accidentally removed return statement
authored by Yeray Diaz Diaz · committed by Seth Michael Larson · parent ebbc003 · Browse files · 6 files·+32 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+32 added−9 removed
- tests:+24 added−4 removed
- other:+8 added−5 removed
Provenance
- Commit
95dad24- Tree
61c6f36- Author
Yeray Diaz Diaz <yeraydiazdiaz@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
ebbc003
6 files changed+32 added−9 removed
modified:httpx/__init__.py+2 added-2 removed
30 unmodified lines31 WriteTimeout,32 )33 from .interfaces import (34 AsyncDispatcher,35- BaseReader,36- BaseWriter,35 BaseBackgroundManager,36 BasePoolSemaphore,37+ BaseReader,38+ BaseWriter,39 ConcurrencyBackend,40 Dispatcher,41 Protocol,42 )91 unmodified linesmodified:httpx/client.py+2 added-1 removed
18 unmodified lines19 from .dispatch.connection_pool import ConnectionPool20 from .dispatch.threaded import ThreadedDispatcher21 from .dispatch.wsgi import WSGIDispatch22 from .exceptions import (23+ HTTPError,24 InvalidURL,25 RedirectBodyUnavailable,26 RedirectLoop,27 TooManyRedirects,27- HTTPError,28 )29 from .interfaces import AsyncDispatcher, ConcurrencyBackend, Dispatcher30 from .models import (31 URL,279 unmodified lines311 """312 headers = Headers(request.headers)313 if url.origin != request.url.origin:314 del headers["Authorization"]315+ del headers["host"]316 return headers317 318 def redirect_content(319 self, request: AsyncRequest, method: str, response: AsyncResponse651 unmodified linesmodified:httpx/exceptions.py+3 added-1 removed
7 unmodified lines8 """9 Base class for Httpx exception10 """11 12- def __init__(self, request: 'BaseRequest' = None, response: 'BaseResponse' = None, *args) -> None:12+ def __init__(13+ self, request: "BaseRequest" = None, response: "BaseResponse" = None, *args14+ ) -> None:15 self.response = response16 self.request = request or getattr(self.response, "request", None)17 super().__init__(*args)18 123 unmodified linesmodified:httpx/models.py+1 added-1 removed
31 unmodified lines32 guess_json_utf,33 is_known_encoding,34 normalize_header_key,35 normalize_header_value,36- str_query_param36+ str_query_param,37 )38 39 PrimitiveData = typing.Union[str, int, float, bool, type(None)]40 1132 unmodified linesmodified:tests/client/test_headers.py+5 added-4 removed
1 #!/usr/bin/env python32 3 import json4+5 from httpx import (5- __version__,6- Client,6+ AsyncDispatcher,7 AsyncRequest,8 AsyncResponse,9- VerifyTypes,9 CertTypes,10+ Client,11 TimeoutTypes,12- AsyncDispatcher,12+ VerifyTypes,13+ __version__,14 )15 16 17 class MockDispatch(AsyncDispatcher):107 unmodified linesmodified:tests/client/test_redirects.py+19 added
84 unmodified lines85 content = await request.read()86 body = json.dumps({"body": content.decode()}).encode()87 return AsyncResponse(codes.OK, content=body, request=request)88 89+ elif request.url.path == "/cross_subdomain":90+ if request.headers["host"] != "www.example.org":91+ headers = {"location": "https://www.example.org/cross_subdomain"}92+ return AsyncResponse(93+ codes.PERMANENT_REDIRECT, headers=headers, request=request94+ )95+ else:96+ return AsyncResponse(97+ codes.OK, content=b"Hello, world!", request=request98+ )99+100 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)101 102 103 @pytest.mark.asyncio156 unmodified lines260 yield b"Example request body"261 262 with pytest.raises(RedirectBodyUnavailable):263 await client.post(url, data=streaming_body())264+265+266+@pytest.mark.asyncio267+async def test_cross_dubdomain_redirect():268+ client = AsyncClient(dispatch=MockDispatch())269+ url = "https://example.com/cross_subdomain"270+ response = await client.get(url)271+ assert response.url == URL("https://www.example.org/cross_subdomain")