@@ -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 and is_chunked_data (data )
40
41
41
42
if auth is not None :
@@ -119,7 +120,10 @@ def request(
119
120
if b"chunked" in l :
120
121
raise ValueError ("Unsupported " + l .decode ())
121
122
elif l .startswith (b"Location:" ) and not 200 <= status <= 299 :
122
- raise NotImplementedError ("Redirects not yet supported" )
123
+ if status in [301 , 302 , 303 , 307 , 308 ]:
124
+ redirect = l [10 :- 2 ].decode ()
125
+ else :
126
+ raise NotImplementedError ("Redirect %d not yet supported" % status )
123
127
if parse_headers is False :
124
128
pass
125
129
elif parse_headers is True :
@@ -132,12 +136,19 @@ def request(
132
136
s .close ()
133
137
raise
134
138
135
- resp = Response (s )
136
- resp .status_code = status
137
- resp .reason = reason
138
- if resp_d is not None :
139
- resp .headers = resp_d
140
- return resp
139
+ if redirect :
140
+ s .close ()
141
+ if status in [301 , 302 , 303 ]:
142
+ return request ("GET" , redirect , None , None , headers , stream )
143
+ else :
144
+ return request (method , redirect , data , json , headers , stream )
145
+ else :
146
+ resp = Response (s )
147
+ resp .status_code = status
148
+ resp .reason = reason
149
+ if resp_d is not None :
150
+ resp .headers = resp_d
151
+ return resp
141
152
142
153
143
154
def head (url , ** kw ):
0 commit comments