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

Improve redirect handling #157

Merged
merged 2 commits into from
Oct 9, 2014
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ var/*
.noseids
docs/_build/
nosetests.xml
cover
cover
.idea
pyvenv
9 changes: 8 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def request(method, url, *,

"""
redirects = 0
method = method.upper()
if loop is None:
loop = asyncio.get_event_loop()
if request_class is None:
Expand Down Expand Up @@ -120,12 +121,18 @@ def request(method, url, *,
raise aiohttp.OsConnectionError(exc)

# redirects
if resp.status in (301, 302) and allow_redirects:
if resp.status in (301, 302, 303, 307) and allow_redirects:
redirects += 1
if max_redirects and redirects >= max_redirects:
resp.close(force=True)
break

# For 301 and 302, mimic IE behaviour, now changed in RFC. Details: https://github.com/kennethreitz/requests/pull/269
if resp.status != 307:
method = 'GET'
data = None
cookies = resp.cookies

r_url = resp.headers.get('LOCATION') or resp.headers.get('URI')

scheme = urllib.parse.urlsplit(r_url)[0]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ def test_HTTP_302_REDIRECT_POST(self):
content = self.loop.run_until_complete(r.content.read())
content = content.decode()

self.assertEqual(r.status, 200)
self.assertIn('"method": "GET"', content)
self.assertEqual(2, httpd['redirects'])
r.close()

def test_HTTP_307_REDIRECT_POST(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
r = self.loop.run_until_complete(
client.request('post', httpd.url('redirect_307', 2),
data={'some': 'data'}, loop=self.loop))
content = self.loop.run_until_complete(r.content.read())
content = content.decode()

self.assertEqual(r.status, 200)
self.assertIn('"method": "POST"', content)
self.assertEqual(2, httpd['redirects'])
Expand Down Expand Up @@ -933,6 +946,20 @@ def redirect(self, match):
self._start_response(302),
headers={'Location': self._path})

@test_utils.Router.define('/redirect_307/([0-9]+)$')
def redirect_307(self, match):
no = int(match.group(1).upper())
rno = self._props['redirects'] = self._props.get('redirects', 0) + 1

if rno >= no:
self._response(
self._start_response(307),
headers={'Location': '/method/%s' % self._method.lower()})
else:
self._response(
self._start_response(307),
headers={'Location': self._path})

@test_utils.Router.define('/encoding/(gzip|deflate)$')
def encoding(self, match):
mode = match.group(1)
Expand Down