From 822b34039269aea8be5f3703ae327cdf5a8678f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E6=98=95=E6=9A=90?= Date: Fri, 6 Dec 2024 13:43:24 +0800 Subject: [PATCH 1/4] Use the host in the redirect url, not the one in headers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The host in headers extracted from the original url may not be the same as the host in the redirect url. Poping out the host in headers force the code to use the host in the redirect url, otherwise the redirect may fail due to inconsistence of hosts in the original url and the redirect url. Signed-off-by: 黃昕暐 --- python-ecosys/requests/requests/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python-ecosys/requests/requests/__init__.py b/python-ecosys/requests/requests/__init__.py index a9a183619..d9bc86530 100644 --- a/python-ecosys/requests/requests/__init__.py +++ b/python-ecosys/requests/requests/__init__.py @@ -180,6 +180,9 @@ def request( if redirect: s.close() + # use the Host in the redirect URL + if "Host" in headers: + headers.pop("Host") if status in [301, 302, 303]: return request("GET", redirect, None, None, headers, stream) else: From 1e68c60442951b013eaec0dd492deb14aa1cd7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E6=98=95=E6=9A=90?= Date: Fri, 11 Apr 2025 13:41:24 +0800 Subject: [PATCH 2/4] pop the item "Host" in the efficient way MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `headers.pop("Host", None)` instead of a `if` statement. Signed-off-by: 黃昕暐 --- python-ecosys/requests/requests/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python-ecosys/requests/requests/__init__.py b/python-ecosys/requests/requests/__init__.py index d9bc86530..8e0a36f22 100644 --- a/python-ecosys/requests/requests/__init__.py +++ b/python-ecosys/requests/requests/__init__.py @@ -181,8 +181,7 @@ def request( if redirect: s.close() # use the Host in the redirect URL - if "Host" in headers: - headers.pop("Host") + headers.pop("Host", None) if status in [301, 302, 303]: return request("GET", redirect, None, None, headers, stream) else: From 68fb277ef7e8f0b4d7abb855ddb71a5cac370277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E6=98=95=E6=9A=90?= Date: Fri, 11 Apr 2025 14:03:53 +0800 Subject: [PATCH 3/4] python-ecosys/requests/requests/__init__.py: update comment about the "Host" header in the redirect URL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update the comment to explain the reason for poping out "Host" header from headers. Ths host in the redirect URL may not be the same as the original URL, pop out "Host" in headers before redirect would force using the host specified in the redirect URL. Signed-off-by: 黃昕暐 --- python-ecosys/requests/requests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-ecosys/requests/requests/__init__.py b/python-ecosys/requests/requests/__init__.py index 8e0a36f22..92d2c79c9 100644 --- a/python-ecosys/requests/requests/__init__.py +++ b/python-ecosys/requests/requests/__init__.py @@ -180,7 +180,7 @@ def request( if redirect: s.close() - # use the Host in the redirect URL + # Use the host specified in the redirect URL, as it may not be the same as the original URL. headers.pop("Host", None) if status in [301, 302, 303]: return request("GET", redirect, None, None, headers, stream) From 4616dbafae8fb2836ea735463c6c4af303bb6a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E6=98=95=E6=9A=90?= Date: Fri, 11 Apr 2025 14:03:53 +0800 Subject: [PATCH 4/4] requests: update comment about the "Host" header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update the comment to explain the reason for poping out "Host" header from headers. Ths host in the redirect URL may not be the same as the original URL, pop out "Host" in headers before redirect would force using the host specified in the redirect URL. Signed-off-by: 黃昕暐 --- python-ecosys/requests/requests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-ecosys/requests/requests/__init__.py b/python-ecosys/requests/requests/__init__.py index 8e0a36f22..92d2c79c9 100644 --- a/python-ecosys/requests/requests/__init__.py +++ b/python-ecosys/requests/requests/__init__.py @@ -180,7 +180,7 @@ def request( if redirect: s.close() - # use the Host in the redirect URL + # Use the host specified in the redirect URL, as it may not be the same as the original URL. headers.pop("Host", None) if status in [301, 302, 303]: return request("GET", redirect, None, None, headers, stream)