Skip to content

Commit f49eef1

Browse files
committed
Add 307 handling (#155) + for 301 and 302, mimic IE behaviour, now changed in RFC. Details: https://github.com/kennethreitz/requests/pull/269
1 parent a06d076 commit f49eef1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

aiohttp/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def request(method, url, *,
9090
9191
"""
9292
redirects = 0
93+
method = method.upper()
9394
if loop is None:
9495
loop = asyncio.get_event_loop()
9596
if request_class is None:
@@ -120,12 +121,18 @@ def request(method, url, *,
120121
raise aiohttp.OsConnectionError(exc)
121122

122123
# redirects
123-
if resp.status in (301, 302) and allow_redirects:
124+
if resp.status in (301, 302, 303, 307) and allow_redirects:
124125
redirects += 1
125126
if max_redirects and redirects >= max_redirects:
126127
resp.close(force=True)
127128
break
128129

130+
# For 301 and 302, mimic IE behaviour, now changed in RFC. Details: https://github.com/kennethreitz/requests/pull/269
131+
if resp.status != 307:
132+
method = 'GET'
133+
data = None
134+
cookies = resp.cookies
135+
129136
r_url = resp.headers.get('LOCATION') or resp.headers.get('URI')
130137

131138
scheme = urllib.parse.urlsplit(r_url)[0]

tests/test_client_functional.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ def test_HTTP_302_REDIRECT_POST(self):
202202
content = self.loop.run_until_complete(r.content.read())
203203
content = content.decode()
204204

205+
self.assertEqual(r.status, 200)
206+
self.assertIn('"method": "GET"', content)
207+
self.assertEqual(2, httpd['redirects'])
208+
r.close()
209+
210+
def test_HTTP_307_REDIRECT_POST(self):
211+
with test_utils.run_server(self.loop, router=Functional) as httpd:
212+
r = self.loop.run_until_complete(
213+
client.request('post', httpd.url('redirect_307', 2),
214+
data={'some': 'data'}, loop=self.loop))
215+
content = self.loop.run_until_complete(r.content.read())
216+
content = content.decode()
217+
205218
self.assertEqual(r.status, 200)
206219
self.assertIn('"method": "POST"', content)
207220
self.assertEqual(2, httpd['redirects'])
@@ -933,6 +946,20 @@ def redirect(self, match):
933946
self._start_response(302),
934947
headers={'Location': self._path})
935948

949+
@test_utils.Router.define('/redirect_307/([0-9]+)$')
950+
def redirect_307(self, match):
951+
no = int(match.group(1).upper())
952+
rno = self._props['redirects'] = self._props.get('redirects', 0) + 1
953+
954+
if rno >= no:
955+
self._response(
956+
self._start_response(307),
957+
headers={'Location': '/method/%s' % self._method.lower()})
958+
else:
959+
self._response(
960+
self._start_response(307),
961+
headers={'Location': self._path})
962+
936963
@test_utils.Router.define('/encoding/(gzip|deflate)$')
937964
def encoding(self, match):
938965
mode = match.group(1)

0 commit comments

Comments
 (0)