Skip to content

Commit

Permalink
Add simple support for redirects
Browse files Browse the repository at this point in the history
Follow 3XX status redirects with a new request. Parse the 'location'
header for an absolute url, absolute path, or relative path. Replace or
amend the url from the original request and place a new request.

Expand the requests_simpletest_cpython.py example with new requests
utilizing the three supported types of redirects.

Note: We're using the httpbingo.org domain for redirects until
the following issue on httpbin.org is resolved:
postmanlabs/httpbin#617

This has been tested on CPython and an ESP32-S2 based MagTag board.
  • Loading branch information
mogenson committed Jul 8, 2021
1 parent 74b0a5a commit 49f58f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
23 changes: 22 additions & 1 deletion adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,28 @@ 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()

0 comments on commit 49f58f4

Please sign in to comment.