@@ -631,6 +631,7 @@ def request(
631
631
headers : Optional [Dict [str , str ]] = None ,
632
632
stream : bool = False ,
633
633
timeout : float = 60 ,
634
+ allow_redirects : bool = True ,
634
635
) -> Response :
635
636
"""Perform an HTTP request to the given url which we will parse to determine
636
637
whether to use SSL ('https://') or not. We can also send some provided 'data'
@@ -697,28 +698,29 @@ def request(
697
698
raise OutOfRetries ("Repeated socket failures" ) from last_exc
698
699
699
700
resp = Response (socket , self ) # our response
700
- if "location" in resp .headers and 300 <= resp .status_code <= 399 :
701
- # a naive handler for redirects
702
- redirect = resp .headers ["location" ]
703
-
704
- if redirect .startswith ("http" ):
705
- # absolute URL
706
- url = redirect
707
- elif redirect [0 ] == "/" :
708
- # relative URL, absolute path
709
- url = "/" .join ([proto , dummy , host , redirect [1 :]])
710
- else :
711
- # relative URL, relative path
712
- path = path .rsplit ("/" , 1 )[0 ]
713
-
714
- while redirect .startswith ("../" ):
701
+ if allow_redirects :
702
+ if "location" in resp .headers and 300 <= resp .status_code <= 399 :
703
+ # a naive handler for redirects
704
+ redirect = resp .headers ["location" ]
705
+
706
+ if redirect .startswith ("http" ):
707
+ # absolute URL
708
+ url = redirect
709
+ elif redirect [0 ] == "/" :
710
+ # relative URL, absolute path
711
+ url = "/" .join ([proto , dummy , host , redirect [1 :]])
712
+ else :
713
+ # relative URL, relative path
715
714
path = path .rsplit ("/" , 1 )[0 ]
716
- redirect = redirect .split ("../" , 1 )[1 ]
717
715
718
- url = "/" .join ([proto , dummy , host , path , redirect ])
716
+ while redirect .startswith ("../" ):
717
+ path = path .rsplit ("/" , 1 )[0 ]
718
+ redirect = redirect .split ("../" , 1 )[1 ]
719
+
720
+ url = "/" .join ([proto , dummy , host , path , redirect ])
719
721
720
- self ._last_response = resp
721
- resp = self .request (method , url , data , json , headers , stream , timeout )
722
+ self ._last_response = resp
723
+ resp = self .request (method , url , data , json , headers , stream , timeout )
722
724
723
725
self ._last_response = resp
724
726
return resp
0 commit comments