]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Remove "Host" header on redirects if URL origin has changed (#199)
authorYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Tue, 13 Aug 2019 11:44:23 +0000 (12:44 +0100)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Tue, 13 Aug 2019 11:44:23 +0000 (06:44 -0500)
Fixes #198

* Reinstate accidentally removed return statement

httpx/__init__.py
httpx/client.py
httpx/exceptions.py
httpx/models.py
tests/client/test_headers.py
tests/client/test_redirects.py

index fd1744120254d474d773ed1236c3a72b53c4d5c7..39b44aae77fbd2b2a72157690c78098e0ef11a13 100644 (file)
@@ -32,10 +32,10 @@ from .exceptions import (
 )
 from .interfaces import (
     AsyncDispatcher,
-    BaseReader,
-    BaseWriter,
     BaseBackgroundManager,
     BasePoolSemaphore,
+    BaseReader,
+    BaseWriter,
     ConcurrencyBackend,
     Dispatcher,
     Protocol,
index e22d1a9fb92cf2cdcb70e200ae9d17b2cf11371d..6b49797e4af909a7d231334b16f301ad9f301304 100644 (file)
@@ -20,11 +20,11 @@ from .dispatch.connection_pool import ConnectionPool
 from .dispatch.threaded import ThreadedDispatcher
 from .dispatch.wsgi import WSGIDispatch
 from .exceptions import (
+    HTTPError,
     InvalidURL,
     RedirectBodyUnavailable,
     RedirectLoop,
     TooManyRedirects,
-    HTTPError,
 )
 from .interfaces import AsyncDispatcher, ConcurrencyBackend, Dispatcher
 from .models import (
@@ -312,6 +312,7 @@ class BaseClient:
         headers = Headers(request.headers)
         if url.origin != request.url.origin:
             del headers["Authorization"]
+            del headers["host"]
         return headers
 
     def redirect_content(
index 21dc7f4de4b0fd48528f1e46bc36682f946b36a4..ddccbf8c2afb1f85d4ad7d854a94b1d90b497a3f 100644 (file)
@@ -9,7 +9,9 @@ class HTTPError(Exception):
     Base class for Httpx exception
     """
 
-    def __init__(self, request: 'BaseRequest' = None, response: 'BaseResponse' = None, *args) -> None:
+    def __init__(
+        self, request: "BaseRequest" = None, response: "BaseResponse" = None, *args
+    ) -> None:
         self.response = response
         self.request = request or getattr(self.response, "request", None)
         super().__init__(*args)
index e7e7a57ae3a8a5eb4adebf2e3187316e0ce9c976..f397e780a82cba82be0c0fa3109b9715763c6fe3 100644 (file)
@@ -33,7 +33,7 @@ from .utils import (
     is_known_encoding,
     normalize_header_key,
     normalize_header_value,
-    str_query_param
+    str_query_param,
 )
 
 PrimitiveData = typing.Union[str, int, float, bool, type(None)]
index bf82a578d25dfabac95d3292d6653a10db2e928c..2d17c1259e5aa270da010319037c75d84c64d576 100755 (executable)
@@ -1,15 +1,16 @@
 #!/usr/bin/env python3
 
 import json
+
 from httpx import (
-    __version__,
-    Client,
+    AsyncDispatcher,
     AsyncRequest,
     AsyncResponse,
-    VerifyTypes,
     CertTypes,
+    Client,
     TimeoutTypes,
-    AsyncDispatcher,
+    VerifyTypes,
+    __version__,
 )
 
 
index ff7547512b20a4b1582195251af3aff18fa230ce..f9c207575e74b337359daa18cb001b48c5184725 100644 (file)
@@ -86,6 +86,17 @@ class MockDispatch(AsyncDispatcher):
             body = json.dumps({"body": content.decode()}).encode()
             return AsyncResponse(codes.OK, content=body, request=request)
 
+        elif request.url.path == "/cross_subdomain":
+            if request.headers["host"] != "www.example.org":
+                headers = {"location": "https://www.example.org/cross_subdomain"}
+                return AsyncResponse(
+                    codes.PERMANENT_REDIRECT, headers=headers, request=request
+                )
+            else:
+                return AsyncResponse(
+                    codes.OK, content=b"Hello, world!", request=request
+                )
+
         return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)
 
 
@@ -250,3 +261,11 @@ async def test_cannot_redirect_streaming_body():
 
     with pytest.raises(RedirectBodyUnavailable):
         await client.post(url, data=streaming_body())
+
+
+@pytest.mark.asyncio
+async def test_cross_dubdomain_redirect():
+    client = AsyncClient(dispatch=MockDispatch())
+    url = "https://example.com/cross_subdomain"
+    response = await client.get(url)
+    assert response.url == URL("https://www.example.org/cross_subdomain")