Skip to content

Add simple support for redirects #77

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
Jul 20, 2021
Merged
Show file tree
Hide file tree
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
36 changes: 25 additions & 11 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,12 @@ def _parse_headers(self):

content = self._readto(b"\r\n")
if title and content:
title = str(title, "utf-8")
# enforce that all headers are lowercase
title = str(title, "utf-8").lower()
content = str(content, "utf-8")
# Check len first so we can skip the .lower allocation most of the time.
if (
len(title) == len("content-length")
and title.lower() == "content-length"
):
if title == "content-length":
self._remaining = int(content)
if (
len(title) == len("transfer-encoding")
and title.lower() == "transfer-encoding"
):
if title == "transfer-encoding":
self._chunked = content.strip().lower() == "chunked"
self._headers[title] = content

Expand Down Expand Up @@ -587,7 +581,27 @@ def request(

resp = Response(socket, self) # our response
if "location" in resp.headers and 300 <= resp.status_code <= 399:
raise NotImplementedError("Redirects not yet supported")
# 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("../"):
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
return resp
Expand Down
26 changes: 26 additions & 0 deletions examples/requests_simpletest_cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "http://httpbin.org/get"
JSON_POST_URL = "http://httpbin.org/post"
REDIRECT_URL = "http://httpbingo.org/redirect/1"
RELATIVE_REDIRECT_URL = "http://httpbingo.org/relative-redirect/1"
ABSOLUTE_REDIRECT_URL = "http://httpbingo.org/absolute-redirect/1"

print("Fetching text from %s" % TEXT_URL)
response = http.get(TEXT_URL)
Expand Down Expand Up @@ -45,3 +48,26 @@
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp["json"])
print("-" * 40)

print("Fetching JSON data from redirect url %s" % REDIRECT_URL)
response = http.get(REDIRECT_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)

print("Fetching JSON data from relative redirect url %s" % RELATIVE_REDIRECT_URL)
response = http.get(RELATIVE_REDIRECT_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)

print("Fetching JSON data from aboslute redirect url %s" % ABSOLUTE_REDIRECT_URL)
response = http.get(ABSOLUTE_REDIRECT_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)

response.close()