Skip to content

Commit

Permalink
Merge pull request #157 from Eyepea/improve_redirect_handling
Browse files Browse the repository at this point in the history
Improve redirect handling
  • Loading branch information
fafhrd91 committed Oct 9, 2014
2 parents 0289ce8 + f49eef1 commit e494364
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
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

0 comments on commit e494364

Please sign in to comment.