diff options
author | Ian Cordasco <graffatcolmingov@gmail.com> | 2014-09-22 21:01:36 -0500 |
---|---|---|
committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2014-09-22 21:01:36 -0500 |
commit | ac44b482971d93cf5e7d4985a0b3fff52b3d2059 (patch) | |
tree | 1e4665afe5996b8fe0ef344b2017d2eec0d02966 | |
parent | d28d45af915adcc964f1621e8115b40b2db0cbba (diff) | |
download | python-requests-ac44b482971d93cf5e7d4985a0b3fff52b3d2059.tar.gz |
Avoid getting stuck in a loop
This prevents a case where we make a request to URL A, which 301s to B which
would then 301 back to A. Alternatively, for less simple schemes, this will
also prevent us from getting stuck in a loop, e.g., it will prevent the
following from causing an endless loop:
A -> B -> C -> D -> E -> F --
^ \
| /
---<------------<----------<-
-rw-r--r-- | requests/sessions.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/requests/sessions.py b/requests/sessions.py index 023edfbd..f9873064 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -535,8 +535,13 @@ class Session(SessionRedirectMixin): if not isinstance(request, PreparedRequest): raise ValueError('You can only send PreparedRequests.') + checked_urls = set() while request.url in self.redirect_cache: - request.url = self.redirect_cache.get(request.url) + checked_urls.add(request.url) + new_url = self.redirect_cache.get(request.url) + if new_url in checked_urls: + break + request.url = new_url # Set up variables needed for resolve_redirects and dispatching of hooks allow_redirects = kwargs.pop('allow_redirects', True) |