Skip to content

Commit c40cb25

Browse files
authored
Merge pull request #132 from FoamyGuy/allow_redirects
implement allow_redirects argument
2 parents 59b1aca + 7158a1f commit c40cb25

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

adafruit_requests.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ def request(
631631
headers: Optional[Dict[str, str]] = None,
632632
stream: bool = False,
633633
timeout: float = 60,
634+
allow_redirects: bool = True,
634635
) -> Response:
635636
"""Perform an HTTP request to the given url which we will parse to determine
636637
whether to use SSL ('https://') or not. We can also send some provided 'data'
@@ -697,28 +698,29 @@ def request(
697698
raise OutOfRetries("Repeated socket failures") from last_exc
698699

699700
resp = Response(socket, self) # our response
700-
if "location" in resp.headers and 300 <= resp.status_code <= 399:
701-
# a naive handler for redirects
702-
redirect = resp.headers["location"]
703-
704-
if redirect.startswith("http"):
705-
# absolute URL
706-
url = redirect
707-
elif redirect[0] == "/":
708-
# relative URL, absolute path
709-
url = "/".join([proto, dummy, host, redirect[1:]])
710-
else:
711-
# relative URL, relative path
712-
path = path.rsplit("/", 1)[0]
713-
714-
while redirect.startswith("../"):
701+
if allow_redirects:
702+
if "location" in resp.headers and 300 <= resp.status_code <= 399:
703+
# a naive handler for redirects
704+
redirect = resp.headers["location"]
705+
706+
if redirect.startswith("http"):
707+
# absolute URL
708+
url = redirect
709+
elif redirect[0] == "/":
710+
# relative URL, absolute path
711+
url = "/".join([proto, dummy, host, redirect[1:]])
712+
else:
713+
# relative URL, relative path
715714
path = path.rsplit("/", 1)[0]
716-
redirect = redirect.split("../", 1)[1]
717715

718-
url = "/".join([proto, dummy, host, path, redirect])
716+
while redirect.startswith("../"):
717+
path = path.rsplit("/", 1)[0]
718+
redirect = redirect.split("../", 1)[1]
719+
720+
url = "/".join([proto, dummy, host, path, redirect])
719721

720-
self._last_response = resp
721-
resp = self.request(method, url, data, json, headers, stream, timeout)
722+
self._last_response = resp
723+
resp = self.request(method, url, data, json, headers, stream, timeout)
722724

723725
self._last_response = resp
724726
return resp

0 commit comments

Comments
 (0)