Skip to content
Myr

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

1 file+29 added−18 removed

modified:requests/sessions.py+29 added-18 removed
11 unmodified lines
12from datetime import datetime1314from .compat import cookielib15from .cookies import cookiejar_from_dict16from .models import Request16from .models import Request, PreparedRequest17from .hooks import default_hooks, dispatch_hook18from .utils import from_key_val_list, default_headers19from .exceptions import TooManyRedirects, InvalidSchema20
52 unmodified lines
73 return kwargs747576class SessionRedirectMixin(object):7777 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."""8081 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.url8889 # ((resp.status_code is codes.see_other))90 while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):91
5 unmodified lines
97 # Release the connection back into the pool.98 resp.close()99100 url = resp.headers['location']96 method = req.method101 method = prepared_request.method102103 # Handle redirection without scheme (see: RFC 1808 Section 4)104 if url.startswith('//'):105 parsed_rurl = urlparse(resp.url)
4 unmodified lines
110 if not urlparse(url).netloc:111 # Compliant with RFC3986, we percent encode the url.112 url = urljoin(resp.url, requote_uri(url))113114 prepared_request.url = url115116 # 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'120121 # 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'125117 # Remove the cookie headers that were sent.118 headers = req.headers126 prepared_request.method = method127128 if resp.status_code is not codes.temporary:129 if 'Content-Length' in prepared_request.headers:130 del prepared_request.headers['Content-Length']131132 prepared_request.body = None133134 try:120 del headers['Cookie']135 del prepared_request.headers['Cookie']136 except KeyError:137 pass138124 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 )148149 i += 1150 yield resp
250 unmodified lines
401 # Total elapsed time of the request (approximately)402 r.elapsed = datetime.utcnow() - start403404 # 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)408409 # Resolve redirects if allowed.410 history = [resp for resp in gen] if allow_redirects else []411
44 unmodified lines