kopia lustrzana https://github.com/micropython/micropython-lib
urequests: Add support for redirects.
rodzic
b17e9aaf93
commit
b29cffb3e3
|
@ -36,6 +36,7 @@ class Response:
|
||||||
def request(
|
def request(
|
||||||
method, url, data=None, json=None, headers={}, stream=None, parse_headers=True, auth=None
|
method, url, data=None, json=None, headers={}, stream=None, parse_headers=True, auth=None
|
||||||
):
|
):
|
||||||
|
redirect = None # redirection url, None means no redirection
|
||||||
chunked_data = data and getattr(data, "__iter__", None) and not getattr(data, "__len__", None)
|
chunked_data = data and getattr(data, "__iter__", None) and not getattr(data, "__len__", None)
|
||||||
|
|
||||||
if auth is not None:
|
if auth is not None:
|
||||||
|
@ -123,7 +124,10 @@ def request(
|
||||||
if b"chunked" in l:
|
if b"chunked" in l:
|
||||||
raise ValueError("Unsupported " + str(l, "utf-8"))
|
raise ValueError("Unsupported " + str(l, "utf-8"))
|
||||||
elif l.startswith(b"Location:") and not 200 <= status <= 299:
|
elif l.startswith(b"Location:") and not 200 <= status <= 299:
|
||||||
raise NotImplementedError("Redirects not yet supported")
|
if status in [301, 302, 303, 307, 308]:
|
||||||
|
redirect = str(l[10:-2], "utf-8")
|
||||||
|
else:
|
||||||
|
raise NotImplementedError("Redirect %d not yet supported" % status)
|
||||||
if parse_headers is False:
|
if parse_headers is False:
|
||||||
pass
|
pass
|
||||||
elif parse_headers is True:
|
elif parse_headers is True:
|
||||||
|
@ -136,12 +140,19 @@ def request(
|
||||||
s.close()
|
s.close()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
resp = Response(s)
|
if redirect:
|
||||||
resp.status_code = status
|
s.close()
|
||||||
resp.reason = reason
|
if status in [301, 302, 303]:
|
||||||
if resp_d is not None:
|
return request("GET", redirect, None, None, headers, stream)
|
||||||
resp.headers = resp_d
|
else:
|
||||||
return resp
|
return request(method, redirect, data, json, headers, stream)
|
||||||
|
else:
|
||||||
|
resp = Response(s)
|
||||||
|
resp.status_code = status
|
||||||
|
resp.reason = reason
|
||||||
|
if resp_d is not None:
|
||||||
|
resp.headers = resp_d
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
def head(url, **kw):
|
def head(url, **kw):
|
||||||
|
|
Ładowanie…
Reference in New Issue