Skip to content

Commit 03f5442

Browse files
authored
Merge pull request #77 from mogenson/redirect-support
Add simple support for redirects
2 parents 74b0a5a + 522a61a commit 03f5442

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

adafruit_requests.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,12 @@ def _parse_headers(self):
281281

282282
content = self._readto(b"\r\n")
283283
if title and content:
284-
title = str(title, "utf-8")
284+
# enforce that all headers are lowercase
285+
title = str(title, "utf-8").lower()
285286
content = str(content, "utf-8")
286-
# Check len first so we can skip the .lower allocation most of the time.
287-
if (
288-
len(title) == len("content-length")
289-
and title.lower() == "content-length"
290-
):
287+
if title == "content-length":
291288
self._remaining = int(content)
292-
if (
293-
len(title) == len("transfer-encoding")
294-
and title.lower() == "transfer-encoding"
295-
):
289+
if title == "transfer-encoding":
296290
self._chunked = content.strip().lower() == "chunked"
297291
self._headers[title] = content
298292

@@ -587,7 +581,27 @@ def request(
587581

588582
resp = Response(socket, self) # our response
589583
if "location" in resp.headers and 300 <= resp.status_code <= 399:
590-
raise NotImplementedError("Redirects not yet supported")
584+
# a naive handler for redirects
585+
redirect = resp.headers["location"]
586+
587+
if redirect.startswith("http"):
588+
# absolute URL
589+
url = redirect
590+
elif redirect[0] == "/":
591+
# relative URL, absolute path
592+
url = "/".join([proto, dummy, host, redirect[1:]])
593+
else:
594+
# relative URL, relative path
595+
path = path.rsplit("/", 1)[0]
596+
597+
while redirect.startswith("../"):
598+
path = path.rsplit("/", 1)[0]
599+
redirect = redirect.split("../", 1)[1]
600+
601+
url = "/".join([proto, dummy, host, path, redirect])
602+
603+
self._last_response = resp
604+
resp = self.request(method, url, data, json, headers, stream, timeout)
591605

592606
self._last_response = resp
593607
return resp

examples/requests_simpletest_cpython.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
1111
JSON_GET_URL = "http://httpbin.org/get"
1212
JSON_POST_URL = "http://httpbin.org/post"
13+
REDIRECT_URL = "http://httpbingo.org/redirect/1"
14+
RELATIVE_REDIRECT_URL = "http://httpbingo.org/relative-redirect/1"
15+
ABSOLUTE_REDIRECT_URL = "http://httpbingo.org/absolute-redirect/1"
1316

1417
print("Fetching text from %s" % TEXT_URL)
1518
response = http.get(TEXT_URL)
@@ -45,3 +48,26 @@
4548
# Parse out the 'json' key from json_resp dict.
4649
print("JSON Data received from server:", json_resp["json"])
4750
print("-" * 40)
51+
52+
print("Fetching JSON data from redirect url %s" % REDIRECT_URL)
53+
response = http.get(REDIRECT_URL)
54+
print("-" * 40)
55+
56+
print("JSON Response: ", response.json())
57+
print("-" * 40)
58+
59+
print("Fetching JSON data from relative redirect url %s" % RELATIVE_REDIRECT_URL)
60+
response = http.get(RELATIVE_REDIRECT_URL)
61+
print("-" * 40)
62+
63+
print("JSON Response: ", response.json())
64+
print("-" * 40)
65+
66+
print("Fetching JSON data from aboslute redirect url %s" % ABSOLUTE_REDIRECT_URL)
67+
response = http.get(ABSOLUTE_REDIRECT_URL)
68+
print("-" * 40)
69+
70+
print("JSON Response: ", response.json())
71+
print("-" * 40)
72+
73+
response.close()

0 commit comments

Comments
 (0)