Skip to content
Myr

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

6 files+32 added−9 removed

modified:httpx/__init__.py+2 added-2 removed
30 unmodified lines
31 WriteTimeout,32)33from .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 lines
modified:httpx/client.py+2 added-1 removed
18 unmodified lines
19from .dispatch.connection_pool import ConnectionPool20from .dispatch.threaded import ThreadedDispatcher21from .dispatch.wsgi import WSGIDispatch22from .exceptions import (23 HTTPError,24 InvalidURL,25 RedirectBodyUnavailable,26 RedirectLoop,27 TooManyRedirects,27 HTTPError,28)29from .interfaces import AsyncDispatcher, ConcurrencyBackend, Dispatcher30from .models import (31 URL,
279 unmodified lines
311 """312 headers = Headers(request.headers)313 if url.origin != request.url.origin:314 del headers["Authorization"]315 del headers["host"]316 return headers317318 def redirect_content(319 self, request: AsyncRequest, method: str, response: AsyncResponse
651 unmodified lines
modified:httpx/exceptions.py+3 added-1 removed
7 unmodified lines
8 """9 Base class for Httpx exception10 """1112 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 lines
modified:httpx/models.py+1 added-1 removed
31 unmodified lines
32 guess_json_utf,33 is_known_encoding,34 normalize_header_key,35 normalize_header_value,36 str_query_param36 str_query_param,37)3839PrimitiveData = typing.Union[str, int, float, bool, type(None)]40
1132 unmodified lines
modified:tests/client/test_headers.py+5 added-4 removed
1#!/usr/bin/env python323import json45from 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)151617class MockDispatch(AsyncDispatcher):
107 unmodified lines
modified:tests/client/test_redirects.py+19 added
84 unmodified lines
85 content = await request.read()86 body = json.dumps({"body": content.decode()}).encode()87 return AsyncResponse(codes.OK, content=body, request=request)8889 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 )99100 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)101102103@pytest.mark.asyncio
156 unmodified lines
260 yield b"Example request body"261262 with pytest.raises(RedirectBodyUnavailable):263 await client.post(url, data=streaming_body())264265266@pytest.mark.asyncio267async 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")