Skip to content
Myr

Consolidate logic for changing method during redirects

Before adoption: this commit came in with the history the project adopted, so no landing records it.

I only moved the code into a function, there was no actual change to the code. I added a few tests to ensure we're doing things correctly. The real point of me doing this is to make it easier to bring back `strict_mode` functionality. For you requests youngsters in the crowd, `strict_mode` followed the spec for redirects meaning the method wouldn't change to a GET. The current code follows the browser convention of changing the method to a GET when doing a 302 redirect. However, lots of servers want you to follow the standards (the nerve!) so I'd like to override the logic. Now that the method changing logic is in `rebuild_method`, I can simply override that function instead of overriding the entire `resolve_redirects` function as suggested by kennethreitz/requests#1325

authored by Ben · committed by Ben · parent c9f8414 · 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

2 files+66 added−17 removed

modified:requests/sessions.py+23 added-17 removed
114 unmodified lines
115 # Release the connection back into the pool.116 resp.close()117118 url = resp.headers['location']119 method = req.method119120 # Handle redirection without scheme (see: RFC 1808 Section 4)121 if url.startswith('//'):122 parsed_rurl = urlparse(resp.url)
15 unmodified lines
138 # 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.url141143 # 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)143148 # 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'152153 # 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'157158 prepared_request.method = method159144 # 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 lines
245 headers['Proxy-Authorization'] = _basic_auth_str(username, password)246247 return new_proxies248249 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.method254255 # http://tools.ietf.org/html/rfc7231#section-6.4.4256 if response.status_code == codes.see_other and method != 'HEAD':257 method = 'GET'258259 # 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'263264 # 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'268269 prepared_request.method = method270271272class Session(SessionRedirectMixin):273 """A Requests session.274275 Provides cookie persistence, connection-pooling, and configuration.
413 unmodified lines
modified:tests/test_requests.py+43 added
160 unmodified lines
161 assert len(e.response.history) == 5162 else:163 pytest.fail('Expected custom max number of redirects to be respected but was not')164165 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_redirect171172 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_redirect179180 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_redirect186187 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_redirect193194 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_redirect200201 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_redirect207208 # 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