From c418c4c4aa4e8491db814e5139b836d8b6b42811 Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Sat, 16 Jul 2016 10:37:13 -0400 Subject: [PATCH] moving implementation details into util func --- requests/sessions.py | 4 ++-- requests/utils.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index d51897fa..a5083e4d 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -29,7 +29,7 @@ from .adapters import HTTPAdapter from .utils import ( requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies, - get_auth_from_url + get_auth_from_url, is_valid_location ) from .status_codes import codes @@ -99,7 +99,7 @@ class SessionRedirectMixin(object): request = response.request while response.is_redirect: - if len(response.raw.headers.getlist('location')) > 1: + if not is_valid_location(response): raise InvalidHeader('Response contains multiple Location headers. ' 'Unable to perform redirect.') diff --git a/requests/utils.py b/requests/utils.py index f9629112..17e5b428 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -665,6 +665,17 @@ def parse_header_links(value): return links +def is_valid_location(response): + """Verify that multiple Location headers weren't + returned from the last response. + """ + headers = getattr(response.raw, 'headers', None) + if headers is not None: + getlist = getattr(headers, 'getlist', None) + if getlist is not None: + return len(getlist('location')) <= 1 + # If response.raw isn't urllib3-like we can't reliably check this + return True # Null bytes; no need to recreate these on each call to guess_json_utf _null = '\x00'.encode('ascii') # encoding to ASCII for Python 3