Skip to content

implement allow_redirects argument #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ def request(
headers: Optional[Dict[str, str]] = None,
stream: bool = False,
timeout: float = 60,
allow_redirects: bool = True,
) -> Response:
"""Perform an HTTP request to the given url which we will parse to determine
whether to use SSL ('https://') or not. We can also send some provided 'data'
Expand Down Expand Up @@ -697,28 +698,29 @@ def request(
raise OutOfRetries("Repeated socket failures") from last_exc

resp = Response(socket, self) # our response
if "location" in resp.headers and 300 <= resp.status_code <= 399:
# a naive handler for redirects
redirect = resp.headers["location"]

if redirect.startswith("http"):
# absolute URL
url = redirect
elif redirect[0] == "/":
# relative URL, absolute path
url = "/".join([proto, dummy, host, redirect[1:]])
else:
# relative URL, relative path
path = path.rsplit("/", 1)[0]

while redirect.startswith("../"):
if allow_redirects:
if "location" in resp.headers and 300 <= resp.status_code <= 399:
# a naive handler for redirects
redirect = resp.headers["location"]

if redirect.startswith("http"):
# absolute URL
url = redirect
elif redirect[0] == "/":
# relative URL, absolute path
url = "/".join([proto, dummy, host, redirect[1:]])
else:
# relative URL, relative path
path = path.rsplit("/", 1)[0]
redirect = redirect.split("../", 1)[1]

url = "/".join([proto, dummy, host, path, redirect])
while redirect.startswith("../"):
path = path.rsplit("/", 1)[0]
redirect = redirect.split("../", 1)[1]

url = "/".join([proto, dummy, host, path, redirect])

self._last_response = resp
resp = self.request(method, url, data, json, headers, stream, timeout)
self._last_response = resp
resp = self.request(method, url, data, json, headers, stream, timeout)

self._last_response = resp
return resp
Expand Down