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

[Feature Request] add save_redirect_histories #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions python2/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,9 @@ def __init__(self, cache=None, timeout=None,

# If set to False then no redirects are followed, even safe ones.
self.follow_redirects = True
self.save_redirect_histories = False

self.redirect_histories = []

# Which HTTP methods do we apply optimistic concurrency to, i.e.
# which methods get an "if-match:" etag header added to them.
Expand Down Expand Up @@ -1256,6 +1259,9 @@ def add_certificate(self, key, cert, domain):
any time a request requires authentication."""
self.certificates.add(key, cert, domain)

def clear_redirects_list(self):
self.redirect_histories = []

def clear_credentials(self):
"""Remove all the names and passwords
that are used for authentication"""
Expand Down Expand Up @@ -1394,6 +1400,7 @@ def _request(self, conn, host, absolute_uri, request_uri, method, body, headers,
if not old_response.has_key('content-location'):
old_response['content-location'] = absolute_uri
redirect_method = method

if response.status in [302, 303]:
redirect_method = "GET"
body = None
Expand All @@ -1402,6 +1409,9 @@ def _request(self, conn, host, absolute_uri, request_uri, method, body, headers,
body=body, headers=headers,
redirections=redirections - 1)
response.previous = old_response

if self.save_redirect_histories:
self.redirect_histories.insert(0, location)
else:
raise RedirectLimit("Redirected more times than rediection_limit allows.", response, content)
elif response.status in [200, 203] and method in ["GET", "HEAD"]:
Expand Down
9 changes: 9 additions & 0 deletions python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,9 @@ def __init__(self, cache=None, timeout=None,

# If set to False then no redirects are followed, even safe ones.
self.follow_redirects = True
self.save_redirect_histories = True

self.redirect_histories = []

# Which HTTP methods do we apply optimistic concurrency to, i.e.
# which methods get an "if-match:" etag header added to them.
Expand Down Expand Up @@ -966,6 +969,9 @@ def add_certificate(self, key, cert, domain):
any time a request requires authentication."""
self.certificates.add(key, cert, domain)

def clear_redirects_list(self):
self.redirect_histories = []

def clear_credentials(self):
"""Remove all the names and passwords
that are used for authentication"""
Expand Down Expand Up @@ -1109,6 +1115,9 @@ def _request(self, conn, host, absolute_uri, request_uri, method, body, headers,
location, method=redirect_method, body=body,
headers=headers, redirections=redirections - 1)
response.previous = old_response

if self.save_redirect_histories:
self.redirect_histories.insert(0, location)
else:
raise RedirectLimit("Redirected more times than redirection_limit allows.", response, content)
elif response.status in [200, 203] and method in ["GET", "HEAD"]:
Expand Down