Merge pull request #1173 from sigmavirus24/use_send_in_resolve_redirects
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use send in resolve redirects
authored by Kenneth Reitz · committed by Kenneth Reitz · parents f2be5fa, 9c8660d · Browse files · 1 file·+29 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+29 added−18 removed
- other:+29 added−18 removed
Provenance
- Commit
12d66cf- Tree
68bf495- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
f2be5fa- Parent 2
9c8660d
1 file changed+29 added−18 removed
modified:requests/sessions.py+29 added-18 removed
11 unmodified lines12 from datetime import datetime13 14 from .compat import cookielib15 from .cookies import cookiejar_from_dict16-from .models import Request16+from .models import Request, PreparedRequest17 from .hooks import default_hooks, dispatch_hook18 from .utils import from_key_val_list, default_headers19 from .exceptions import TooManyRedirects, InvalidSchema20 52 unmodified lines73 return kwargs74 75 76 class SessionRedirectMixin(object):77-77 def resolve_redirects(self, resp, req, stream=False, timeout=None,78 verify=True, cert=None, proxies=None):79 """Receives a Response. Returns a generator of Responses."""80 81 i = 082+ prepared_request = PreparedRequest()83+ prepared_request.body = req.body84+ prepared_request.headers = req.headers.copy()85+ prepared_request.hooks = req.hooks86+ prepared_request.method = req.method87+ prepared_request.url = req.url88 89 # ((resp.status_code is codes.see_other))90 while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):91 5 unmodified lines97 # Release the connection back into the pool.98 resp.close()99 100 url = resp.headers['location']96- method = req.method101+ method = prepared_request.method102 103 # Handle redirection without scheme (see: RFC 1808 Section 4)104 if url.startswith('//'):105 parsed_rurl = urlparse(resp.url)4 unmodified lines110 if not urlparse(url).netloc:111 # Compliant with RFC3986, we percent encode the url.112 url = urljoin(resp.url, requote_uri(url))113 114+ prepared_request.url = url115+116 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4110- if resp.status_code == codes.see_other and req.method != 'HEAD':117+ if (resp.status_code == codes.see_other and118+ prepared_request.method != 'HEAD'):119 method = 'GET'120 121 # Do what the browsers do, despite standards...114- if resp.status_code in (codes.moved, codes.found) and req.method == 'POST':122+ if (resp.status_code in (codes.moved, codes.found) and123+ prepared_request.method == 'POST'):124 method = 'GET'125 117- # Remove the cookie headers that were sent.118- headers = req.headers126+ prepared_request.method = method127+128+ if resp.status_code is not codes.temporary:129+ if 'Content-Length' in prepared_request.headers:130+ del prepared_request.headers['Content-Length']131+132+ prepared_request.body = None133+134 try:120- del headers['Cookie']135+ del prepared_request.headers['Cookie']136 except KeyError:137 pass138 124- resp = self.request(125- url=url,126- method=method,127- headers=headers,128- auth=req.auth,129- cookies=req.cookies,130- allow_redirects=False,139+ resp = self.send(140+ prepared_request,141 stream=stream,142 timeout=timeout,143 verify=verify,144 cert=cert,145 proxies=proxies,136- hooks=req.hooks,146+ allow_redirects=False,147 )148 149 i += 1150 yield resp250 unmodified lines401 # Total elapsed time of the request (approximately)402 r.elapsed = datetime.utcnow() - start403 404 # Redirect resolving generator.395- gen = self.resolve_redirects(r, req, stream=stream, timeout=timeout,396- verify=verify, cert=cert, proxies=proxies)405+ gen = self.resolve_redirects(r, request, stream=stream,406+ timeout=timeout, verify=verify, cert=cert,407+ proxies=proxies)408 409 # Resolve redirects if allowed.410 history = [resp for resp in gen] if allow_redirects else []411 44 unmodified lines