Merge pull request #3060 from benweatherman/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Consolidate logic for changing method during redirects
authored by Ian Cordasco · committed by Ian Cordasco · parents c9f8414, f24d56a · Browse files · 2 files·+66 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+66 added−17 removed
- tests:+43 added−0 removed
- other:+23 added−17 removed
Provenance
- Commit
3acf3a7- Tree
f3caee9- Author
Ian Cordasco <sigmavirus24@users.noreply.github.com>- Committer
Ian Cordasco <sigmavirus24@users.noreply.github.com>- Parent 1
c9f8414- Parent 2
f24d56a
2 files changed+66 added−17 removed
modified:requests/sessions.py+23 added-17 removed
114 unmodified lines115 # Release the connection back into the pool.116 resp.close()117 118 url = resp.headers['location']119- method = req.method119 120 # Handle redirection without scheme (see: RFC 1808 Section 4)121 if url.startswith('//'):122 parsed_rurl = urlparse(resp.url)15 unmodified lines138 # Cache the url, unless it redirects to itself.139 if resp.is_permanent_redirect and req.url != prepared_request.url:140 self.redirect_cache[req.url] = prepared_request.url141 143- # http://tools.ietf.org/html/rfc7231#section-6.4.4144- if (resp.status_code == codes.see_other and145- method != 'HEAD'):146- method = 'GET'142+ self.rebuild_method(prepared_request, resp)143 148- # Do what the browsers do, despite standards...149- # First, turn 302s into GETs.150- if resp.status_code == codes.found and method != 'HEAD':151- method = 'GET'152-153- # Second, if a POST is responded to with a 301, turn it into a GET.154- # This bizarre behaviour is explained in Issue 1704.155- if resp.status_code == codes.moved and method == 'POST':156- method = 'GET'157-158- prepared_request.method = method159-144 # https://github.com/kennethreitz/requests/issues/1084145 if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect):146 if 'Content-Length' in prepared_request.headers:147 del prepared_request.headers['Content-Length']97 unmodified lines245 headers['Proxy-Authorization'] = _basic_auth_str(username, password)246 247 return new_proxies248 249+ def rebuild_method(self, prepared_request, response):250+ """When being redirected we may want to change the method of the request251+ based on certain specs or browser behavior.252+ """253+ method = prepared_request.method254 255+ # http://tools.ietf.org/html/rfc7231#section-6.4.4256+ if response.status_code == codes.see_other and method != 'HEAD':257+ method = 'GET'258+259+ # Do what the browsers do, despite standards...260+ # First, turn 302s into GETs.261+ if response.status_code == codes.found and method != 'HEAD':262+ method = 'GET'263+264+ # Second, if a POST is responded to with a 301, turn it into a GET.265+ # This bizarre behaviour is explained in Issue 1704.266+ if response.status_code == codes.moved and method == 'POST':267+ method = 'GET'268+269+ prepared_request.method = method270+271+272 class Session(SessionRedirectMixin):273 """A Requests session.274 275 Provides cookie persistence, connection-pooling, and configuration.413 unmodified linesmodified:tests/test_requests.py+43 added
160 unmodified lines161 assert len(e.response.history) == 5162 else:163 pytest.fail('Expected custom max number of redirects to be respected but was not')164 165+ def test_http_301_changes_post_to_get(self, httpbin):166+ r = requests.post(httpbin('status', '301'))167+ assert r.status_code == 200168+ assert r.request.method == 'GET'169+ assert r.history[0].status_code == 301170+ assert r.history[0].is_redirect171+172+ def test_http_301_doesnt_change_head_to_get(self, httpbin):173+ r = requests.head(httpbin('status', '301'), allow_redirects=True)174+ print(r.content)175+ assert r.status_code == 200176+ assert r.request.method == 'HEAD'177+ assert r.history[0].status_code == 301178+ assert r.history[0].is_redirect179+180+ def test_http_302_changes_post_to_get(self, httpbin):181+ r = requests.post(httpbin('status', '302'))182+ assert r.status_code == 200183+ assert r.request.method == 'GET'184+ assert r.history[0].status_code == 302185+ assert r.history[0].is_redirect186+187+ def test_http_302_doesnt_change_head_to_get(self, httpbin):188+ r = requests.head(httpbin('status', '302'), allow_redirects=True)189+ assert r.status_code == 200190+ assert r.request.method == 'HEAD'191+ assert r.history[0].status_code == 302192+ assert r.history[0].is_redirect193+194+ def test_http_303_changes_post_to_get(self, httpbin):195+ r = requests.post(httpbin('status', '303'))196+ assert r.status_code == 200197+ assert r.request.method == 'GET'198+ assert r.history[0].status_code == 303199+ assert r.history[0].is_redirect200+201+ def test_http_303_doesnt_change_head_to_get(self, httpbin):202+ r = requests.head(httpbin('status', '303'), allow_redirects=True)203+ assert r.status_code == 200204+ assert r.request.method == 'HEAD'205+ assert r.history[0].status_code == 303206+ assert r.history[0].is_redirect207+208 # def test_HTTP_302_ALLOW_REDIRECT_POST(self):209 # r = requests.post(httpbin('status', '302'), data={'some': 'data'})210 # self.assertEqual(r.status_code, 200)211 1318 unmodified lines