From ac44b482971d93cf5e7d4985a0b3fff52b3d2059 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 22 Sep 2014 21:01:36 -0500 Subject: [PATCH] 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 -- ^ \ | / ---<------------<----------<- --- requests/sessions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/requests/sessions.py b/requests/sessions.py index 023edfbd31..f987306497 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -535,8 +535,13 @@ def send(self, request, **kwargs): 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)