From e7e395549f8b60777f00c244dabd2bd9f89271b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81re=CC=81my=20Bethmont?= Date: Tue, 9 Aug 2011 17:28:58 +0200 Subject: [PATCH] Handle too many redirects. --- requests/exceptions.py | 3 +++ requests/models.py | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/requests/exceptions.py b/requests/exceptions.py index eff7512a..c08c6148 100644 --- a/requests/exceptions.py +++ b/requests/exceptions.py @@ -21,3 +21,6 @@ class URLRequired(RequestException): class InvalidMethod(RequestException): """An inappropriate method was attempted.""" + +class TooManyRedirects(RequestException): + """Too many redirects.""" diff --git a/requests/models.py b/requests/models.py index ca72802f..e08bce25 100644 --- a/requests/models.py +++ b/requests/models.py @@ -20,7 +20,7 @@ from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicA from .structures import CaseInsensitiveDict from .packages.poster.encode import multipart_encode from .packages.poster.streaminghttp import register_openers, get_handlers -from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod +from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects REDIRECT_STATI = (301, 302, 303, 307) @@ -192,6 +192,9 @@ class Request(object): (self.allow_redirects)) ): + if not len(history) < 30: + raise TooManyRedirects() + history.append(r) url = r.headers['location']