Skip to content

Commit

Permalink
Merge pull request #3886 from jvanasco/proposed/3.0.0
Browse files Browse the repository at this point in the history
proposed 3.0 - altered internal loops of resolve_redirect
  • Loading branch information
sigmavirus24 authored Feb 25, 2017
2 parents b22833c + 8e07dae commit 60339d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
+++++++++++++++++++

Expand Down
21 changes: 6 additions & 15 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,13 @@ def resolve_redirects(self, response, request, stream=False, timeout=None,
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):
Expand Down Expand Up @@ -209,6 +203,10 @@ def resolve_redirects(self, response, request, stream=False, timeout=None,
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)

Expand Down Expand Up @@ -657,17 +655,10 @@ def send(self, request, **kwargs):
# 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
Expand Down

0 comments on commit 60339d1

Please sign in to comment.