Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposed 3.0 - altered internal loops of resolve_redirect #3886

Merged
merged 1 commit into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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