Skip to content
Myr

Merge pull request #1151 from sigmavirus24/fix1133

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

Move redirect handling from Session.request to Session.send

authored by Kenneth Reitz · committed by Kenneth Reitz · parents ae55676, ba48591 · Browse files · 2 files+49 added−15 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+49 added−15 removed

  • other:+49 added−15 removed

2 files+49 added−15 removed

modified:requests/sessions.py+44 added-14 removed
73 unmodified lines
747576class SessionRedirectMixin(object):7778 def resolve_redirects(self, resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None):78 def resolve_redirects(self, resp, req, stream=False, timeout=None,79 verify=True, cert=None, proxies=None):80 """Receives a Response. Returns a generator of Responses."""8182 i = 083
192 unmodified lines
276 # Prepare the Request.277 prep = req.prepare()278279 # Send the request.279 resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)280 send_kwargs = {281 'stream': stream,282 'timeout': timeout,283 'verify': verify,284 'cert': cert,285 'proxies': proxies,286 'allow_redirects': allow_redirects,287 'req': req,288 }289 resp = self.send(prep, **send_kwargs)290291 # Persist cookies.292 for cookie in resp.cookies:293 self.cookies.set_cookie(cookie)294285 # Redirect resolving generator.286 gen = self.resolve_redirects(resp, req, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)287288 # Resolve redirects if allowed.289 history = [r for r in gen] if allow_redirects else []290291 # Shuffle things around if there's history.292 if history:293 history.insert(0, resp)294 resp = history.pop()295 resp.history = tuple(history)296295 return resp296297 def get(self, url, **kwargs):298 """Sends a GET request. Returns :class:`Response` object.
65 unmodified lines
364 return self.request('DELETE', url, **kwargs)365366 def send(self, request, **kwargs):367 """Send a given PreparedRequest."""368 # Set up variables needed for resolve_redirects and dispatching of369 # hooks370 allow_redirects = kwargs.pop('allow_redirects', True)371 req = kwargs.pop('req', None)372 stream = kwargs.get('stream', False)373 timeout = kwargs.get('timeout')374 verify = kwargs.get('verify')375 cert = kwargs.get('cert')376 proxies = kwargs.get('proxies')377 hooks = request.hooks378379 # Get the appropriate adapter to use380 adapter = self.get_adapter(url=request.url)381382 # Start time (approximately) of the request383 start = datetime.utcnow()384 # Send the request385 r = adapter.send(request, **kwargs)386 # Total elapsed time of the request (approximately)387 r.elapsed = datetime.utcnow() - start388389 # Redirect resolving generator.390 gen = self.resolve_redirects(r, req, stream=stream, timeout=timeout,391 verify=verify, cert=cert, proxies=proxies)392393 # Resolve redirects if allowed.394 history = [resp for resp in gen] if allow_redirects else []395396 # Shuffle things around if there's history.397 if history:398 # Insert the first (original) request at the start399 history.insert(0, r)400 # Get the last request made401 r = history.pop()402 r.history = tuple(history)403404 # Response manipulation hooks405 r = dispatch_hook('response', hooks, r)406407 return r408409 def get_adapter(self, url):410 """Returns the appropriate connnection adapter for the given URL."""
29 unmodified lines
modified:test_requests.py+5 added-1 removed
1 unmodified line
2# -*- coding: utf-8 -*-34"""Tests for Requests."""56from __future__ import division7import json8import os9import unittest10
312 unmodified lines
323 self.assertEqual(cookie._rest['HttpOnly'], rest['HttpOnly'])324325 def test_time_elapsed_blank(self):326 r = requests.get(httpbin('get'))326 self.assertTrue(r.elapsed.total_seconds() > 0.0)327 td = r.elapsed328 total_seconds = ((td.microseconds + (td.seconds + td.days * 24 * 3600)329 * 10**6) / 10**6)330 self.assertTrue(total_seconds > 0.0)331332 def test_response_is_iterable(self):333 r = requests.Response()334 io = StringIO.StringIO('abc')
6 unmodified lines