Merge pull request #310 from encode/drop-content-length-on-get-redirects
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Drop Content-Length headers on GET redirects
authored by Tom Christie · committed by GitHub · parents cbe7616, 5961bd8 · Browse files · 2 files·+39 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+39 added−7 removed
- tests:+25 added−2 removed
- other:+14 added−5 removed
Provenance
- Commit
33c5fe0- Tree
19f9fbb- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent 1
cbe7616- Parent 2
5961bd8
2 files changed+39 added−7 removed
modified:httpx/middleware.py+14 added-5 removed
86 unmodified lines87 self, request: AsyncRequest, response: AsyncResponse88 ) -> AsyncRequest:89 method = self.redirect_method(request, response)90 url = self.redirect_url(request, response)91- headers = self.redirect_headers(request, url) # TODO: merge headers?91+ headers = self.redirect_headers(request, url, method) # TODO: merge headers?92 content = self.redirect_content(request, method)93 cookies = Cookies(self.cookies)94 cookies.update(request.cookies)95 return AsyncRequest(41 unmodified lines137 url = url.copy_with(fragment=request.url.fragment)138 139 return url140 141- def redirect_headers(self, request: AsyncRequest, url: URL) -> Headers:141+ def redirect_headers(self, request: AsyncRequest, url: URL, method: str) -> Headers:142 """143- Strip Authorization headers when responses are redirected away from144- the origin.143+ Return the headers that should be used for the redirect request.144 """145 headers = Headers(request.headers)146+147 if url.origin != request.url.origin:148+ # Strip Authorization headers when responses are redirected away from149+ # the origin.150 del headers["Authorization"]149- del headers["host"]151+ del headers["Host"]152+153+ if method != request.method and method == "GET":154+ # If we've switch to a 'GET' request, then strip any headers which155+ # are only relevant to the request body.156+ del headers["Content-Length"]157+ del headers["Transfer-Encoding"]158+159 return headers160 161 def redirect_content(self, request: AsyncRequest, method: str) -> bytes:162 """7 unmodified linesmodified:tests/client/test_redirects.py+25 added-2 removed
83 unmodified lines84 return AsyncResponse(85 codes.PERMANENT_REDIRECT, headers=headers, request=request86 )87 88+ elif request.url.path == "/redirect_no_body":89+ await request.read()90+ headers = {"location": "/redirect_body_target"}91+ return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)92+93 elif request.url.path == "/redirect_body_target":94 content = await request.read()95+ headers = dict(request.headers.items())90- body = json.dumps({"body": content.decode()}).encode()96+ body = json.dumps({"body": content.decode(), "headers": headers}).encode()97 return AsyncResponse(codes.OK, content=body, request=request)98 99 elif request.url.path == "/cross_subdomain":100 if request.headers["host"] != "www.example.org":147 unmodified lines248 assert response.json()["headers"]["authorization"] == "abc"249 250 251 async def test_body_redirect(backend):252+ """253+ A 308 redirect should preserve the request body.254+ """255 client = AsyncClient(dispatch=MockDispatch(), backend=backend)256 url = "https://example.org/redirect_body"257 data = b"Example request body"258 response = await client.post(url, data=data)259 assert response.url == URL("https://example.org/redirect_body_target")251- assert response.json() == {"body": "Example request body"}260+ assert response.json()["body"] == "Example request body"261+ assert "content-length" in response.json()["headers"]262 263 264+async def test_no_body_redirect(backend):265+ """266+ A 303 redirect should remove the request body.267+ """268+ client = AsyncClient(dispatch=MockDispatch(), backend=backend)269+ url = "https://example.org/redirect_no_body"270+ data = b"Example request body"271+ response = await client.post(url, data=data)272+ assert response.url == URL("https://example.org/redirect_body_target")273+ assert response.json()["body"] == ""274+ assert "content-length" not in response.json()["headers"]275+276+277 async def test_cannot_redirect_streaming_body(backend):278 client = AsyncClient(dispatch=MockDispatch(), backend=backend)279 url = "https://example.org/redirect_body"280 12 unmodified lines