@@ -36,6 +36,7 @@ def json(self):
36
36
def request (
37
37
method , url , data = None , json = None , headers = {}, stream = None , parse_headers = True , auth = None
38
38
):
39
+ redirect = None # redirection url, None means no redirection
39
40
chunked_data = data and getattr (data , "__iter__" , None ) and not getattr (data , "__len__" , None )
40
41
41
42
if auth is not None :
@@ -123,7 +124,10 @@ def request(
123
124
if b"chunked" in l :
124
125
raise ValueError ("Unsupported " + str (l , "utf-8" ))
125
126
elif l .startswith (b"Location:" ) and not 200 <= status <= 299 :
126
- raise NotImplementedError ("Redirects not yet supported" )
127
+ if status in [301 , 302 , 303 , 307 , 308 ]:
128
+ redirect = str (l [10 :- 2 ], "utf-8" )
129
+ else :
130
+ raise NotImplementedError ("Redirect %d not yet supported" % status )
127
131
if parse_headers is False :
128
132
pass
129
133
elif parse_headers is True :
@@ -136,12 +140,19 @@ def request(
136
140
s .close ()
137
141
raise
138
142
139
- resp = Response (s )
140
- resp .status_code = status
141
- resp .reason = reason
142
- if resp_d is not None :
143
- resp .headers = resp_d
144
- return resp
143
+ if redirect :
144
+ s .close ()
145
+ if status in [301 , 302 , 303 ]:
146
+ return request ("GET" , redirect , None , None , headers , stream )
147
+ else :
148
+ return request (method , redirect , data , json , headers , stream )
149
+ else :
150
+ resp = Response (s )
151
+ resp .status_code = status
152
+ resp .reason = reason
153
+ if resp_d is not None :
154
+ resp .headers = resp_d
155
+ return resp
145
156
146
157
147
158
def head (url , ** kw ):
0 commit comments