Skip to content
Myr

Merge pull request #1315 from reclosedev/fix-redirects

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

Don't reuse the same prepared request for all redirects

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 8028103, 716b627 · Browse files · 2 files+14 added−10 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+14 added−10 removed

  • other:+14 added−10 removed

2 files+14 added−10 removed

modified:requests/sessions.py+7 added-9 removed
69 unmodified lines
70 verify=True, cert=None, proxies=None):71 """Receives a Response. Returns a generator of Responses."""7273 i = 074 prepared_request = PreparedRequest()75 prepared_request.body = req.body76 prepared_request.headers = req.headers.copy()77 prepared_request.hooks = req.hooks78 prepared_request.method = req.method79 prepared_request.url = req.url7475 # ((resp.status_code is codes.see_other))76 while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):77 prepared_request = PreparedRequest()78 prepared_request.body = req.body79 prepared_request.headers = req.headers.copy()80 prepared_request.hooks = req.hooks8182 resp.content # Consume socket so it can be released8384 if i >= self.max_redirects:
2 unmodified lines
87 # Release the connection back into the pool.88 resp.close()8990 url = resp.headers['location']93 method = prepared_request.method91 method = req.method9293 # Handle redirection without scheme (see: RFC 1808 Section 4)94 if url.startswith('//'):95 parsed_rurl = urlparse(resp.url)
15 unmodified lines
111 prepared_request.url = url112113 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4114 if (resp.status_code == codes.see_other and117 prepared_request.method != 'HEAD'):115 method != 'HEAD'):116 method = 'GET'117118 # Do what the browsers do, despite standards...119 if (resp.status_code in (codes.moved, codes.found) and122 prepared_request.method not in ('GET', 'HEAD')):120 method not in ('GET', 'HEAD')):121 method = 'GET'122123 prepared_request.method = method124
380 unmodified lines
modified:test_requests.py+7 added-1 removed
186 unmodified lines
187 # Make sure the cookie was sent188 assert r.json()['cookies']['foo'] == 'bar'189 # Make sure the session cj is still the custom one190 assert s.cookies is cj191191 192 def test_requests_in_history_are_not_overridden(self):193 resp = requests.get(httpbin('redirect/3'))194 urls = [r.url for r in resp.history]195 req_urls = [r.request.url for r in resp.history]196 self.assertEquals(urls, req_urls)197 198 def test_user_agent_transfers(self):199200 heads = {201 'User-agent': 'Mozilla/5.0 (github.com/kennethreitz/requests)'
547 unmodified lines