Skip to content

Commit

Permalink
feature: Http().redirect_codes set, works after follow(_all)_redirect…
Browse files Browse the repository at this point in the history
…s check

#156
  • Loading branch information
temoto committed Jan 23, 2020
1 parent a128b94 commit ea0e1f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion python2/httplib2/__init__.py
Expand Up @@ -294,6 +294,9 @@ class NotRunningAppEngineEnvironment(HttpLib2Error):
# https://tools.ietf.org/html/rfc7231#section-8.1.3
SAFE_METHODS = ("GET", "HEAD") # TODO add "OPTIONS", "TRACE"

# To change, assign to `Http().redirect_codes`
REDIRECT_CODES = frozenset((300, 301, 302, 303, 307, 308))


def _get_end2end_headers(response):
hopbyhop = list(HOP_BY_HOP)
Expand Down Expand Up @@ -1664,6 +1667,8 @@ def __init__(
# If set to False then no redirects are followed, even safe ones.
self.follow_redirects = True

self.redirect_codes = REDIRECT_CODES

# Which HTTP methods do we apply optimistic concurrency to, i.e.
# which methods get an "if-match:" etag header added to them.
self.optimistic_concurrency_methods = ["PUT", "PATCH"]
Expand Down Expand Up @@ -1866,7 +1871,7 @@ def _request(
or method in self.safe_methods
or response.status in (303, 308)
):
if self.follow_redirects and response.status in (300, 301, 302, 303, 307, 308):
if self.follow_redirects and response.status in self.redirect_codes:
# Pick out the location header and basically start from the beginning
# remembering first to strip the ETag header and decrement our 'depth'
if redirections:
Expand Down
7 changes: 6 additions & 1 deletion python3/httplib2/__init__.py
Expand Up @@ -164,6 +164,9 @@ class ProxiesUnavailableError(HttpLib2Error):
# https://tools.ietf.org/html/rfc7231#section-8.1.3
SAFE_METHODS = ("GET", "HEAD", "OPTIONS", "TRACE")

# To change, assign to `Http().redirect_codes`
REDIRECT_CODES = frozenset((300, 301, 302, 303, 307, 308))


from httplib2 import certs
CA_CERTS = certs.where()
Expand Down Expand Up @@ -1471,6 +1474,8 @@ def __init__(
# If set to False then no redirects are followed, even safe ones.
self.follow_redirects = True

self.redirect_codes = REDIRECT_CODES

# Which HTTP methods do we apply optimistic concurrency to, i.e.
# which methods get an "if-match:" etag header added to them.
self.optimistic_concurrency_methods = ["PUT", "PATCH"]
Expand Down Expand Up @@ -1672,7 +1677,7 @@ def _request(
or method in self.safe_methods
or response.status in (303, 308)
):
if self.follow_redirects and response.status in (300, 301, 302, 303, 307, 308):
if self.follow_redirects and response.status in self.redirect_codes:
# Pick out the location header and basically start from the beginning
# remembering first to strip the ETag header and decrement our 'depth'
if redirections:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_http.py
Expand Up @@ -694,3 +694,12 @@ def test_get_duplicate_headers():
assert response.status == 200
assert content == b"content"
assert response["link"], "link1, link2"


def test_custom_redirect_codes():
http = httplib2.Http()
http.redirect_codes = set([300])
with tests.server_const_http(status=301, request_count=1) as uri:
response, content = http.request(uri, "GET")
assert response.status == 301
assert response.previous is None

3 comments on commit ea0e1f1

@neilodonuts
Copy link

Choose a reason for hiding this comment

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

I like it. This will give the caller a chance to decide on whether to follow specific types of redirects. 👍

@temoto
Copy link
Member Author

@temoto temoto commented on ea0e1f1 Jan 23, 2020

Choose a reason for hiding this comment

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

Does it work in your setup?

@neilodonuts
Copy link

Choose a reason for hiding this comment

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

Yes! I was able to test it successfully and have added code in anticipation of the next httplib2 release.

Please sign in to comment.