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

altered internal loops of how ``SessionRedirectMixin.resolve_redirect… #3871

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ 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.

2.13.0 (2017-01-24)
+++++++++++++++++++

Expand Down
17 changes: 6 additions & 11 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,12 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,
verify=True, cert=None, proxies=None, **adapter_kwargs):
"""Receives a Response. Returns a generator of Responses."""

hist = [] # keep track of history
hist = [resp, ] # keep track of history; seed it with the original response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the trailing comma in the list.


url = self.get_redirect_target(resp)
while url:
prepared_request = req.copy()

# Update history and keep track of redirects.
# resp.history must ignore the original request in this loop
hist.append(resp)
resp.history = hist[1:]

try:
resp.content # Consume socket so it can be released
except (ChunkedEncodingError, ContentDecodingError, RuntimeError):
Expand Down Expand Up @@ -193,6 +188,10 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,
allow_redirects=False,
**adapter_kwargs
)
# copy our history tracker into the response
resp.history = hist[:]
# append the new response to the history tracker for the next iteration
hist.append(resp)

extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)

Expand Down Expand Up @@ -637,13 +636,9 @@ 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 history.
# if there is a history, replace ``r`` with the last response
if history:
# Insert the first (original) request at the start
history.insert(0, r)
# Get the last request made
r = history.pop()
r.history = history

if not stream:
r.content
Expand Down