From 8e07dae0fda85f102976a7e9b55d612cbaaf9cf0 Mon Sep 17 00:00:00 2001 From: jonathan vanasco Date: Wed, 22 Feb 2017 13:23:21 -0500 Subject: [PATCH] altered internal loops of how ``SessionRedirectMixin.resolve_redirects`` and ``Session.send`` handle redirect history. 3.0.0 branch --- HISTORY.rst | 5 +++++ requests/sessions.py | 21 ++++++--------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c26036c1..020cc1d3 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -12,6 +12,11 @@ Release History querying ``Response.is_redirect`` and ``Response.headers['location']``. Advanced users will be able to process malformed redirects more easily. +- Altered how ``SessionRedirectMixin.resolve_redirects`` and ``Session.send`` + process redirect history. Developers who subclass ``resolve_redirects`` will + find a different ``.history`` attribute - the first element now contains the + original response, and the last element now contains the active response. + 2.13.0 (2017-01-24) +++++++++++++++++++ diff --git a/requests/sessions.py b/requests/sessions.py index 6b962587..7f813141 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -103,19 +103,13 @@ class SessionRedirectMixin(object): reached. """ - redirect_count = 0 - history = [] # keep track of history + history = [response] # keep track of history; seed it with the original response url = self.get_redirect_target(response) while url: prepared_request = request.copy() - # Update history and keep track of redirects. - # response.history must ignore the original request in this loop - history.append(response) - response.history = history[1:] - try: response.content # Consume socket so it can be released except (ChunkedEncodingError, ConnectionError, ContentDecodingError, RuntimeError): @@ -209,6 +203,10 @@ class SessionRedirectMixin(object): allow_redirects=False, **adapter_kwargs ) + # copy our history tracker into the response + response.history = history[:] + # append the new response to the history tracker for the next iteration + history.append(response) extract_cookies_to_jar(self.cookies, prepared_request, response.raw) @@ -657,17 +655,10 @@ class Session(SessionRedirectMixin): # Resolve redirects, if allowed. history = [resp for resp in gen] if allow_redirects else [] - # Shuffle things around if there's redirection history. + # If there is a history, replace ``r`` with the last response if history: - # Insert the first (original) Response at the start. - history.insert(0, r) - - # Remove the final response from history, use it as our Response. r = history.pop() - # Save redirection history to final Response object. - r.history = history - # Automatically download response body, if not in streaming mode. if not stream: r.content