Skip to content
Myr

urllib3 update

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 638532d · Browse files · 4 files+62 added−37 removed
This commit

State

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

Diff stats

4 files+62 added−37 removed

  • other:+62 added−37 removed

4 files+62 added−37 removed

modified:requests/packages/urllib3/connectionpool.py+9 added-13 removed
304 unmodified lines
305 if headers is None:306 headers = self.headers307308 if retries < 0:309 raise MaxRetryError("Max retries exceeded for url: %s" % url)309 raise MaxRetryError(url)310311 if timeout is _Default:312 timeout = self.timeout313
5 unmodified lines
319 host = "%s://%s" % (self.scheme, self.host)320 if self.port:321 host = "%s:%d" % (host, self.port)322323 raise HostChangedError("Connection pool with host '%s' tried to "324 "open a foreign host: %s" % (host, url))323 raise HostChangedError(host, url, retries - 1)324325 conn = None326327 try:
39 unmodified lines
367368 except (CertificateError), e:369 # Name mismatch370 raise SSLError(e)372 371372 except (HTTPException, SocketError), e:373 # Connection broken, discard. It will be replaced next _get_conn().374 conn = None375
7 unmodified lines
383 "broken by '%r': %s" % (retries, e, url))384 return self.urlopen(method, url, body, headers, retries - 1,385 redirect, assert_same_host) # Try again386388 # Handle redirection389 if (redirect and390 response.status in [301, 302, 303, 307] and391 'location' in response.headers): # Redirect, retry392 log.info("Redirecting %s -> %s" %393 (url, response.headers.get('location')))394 return self.urlopen(method, response.headers.get('location'), body,395 headers, retries - 1, redirect,396 assert_same_host)387 # Handle redirect?388 redirect_location = redirect and response.get_redirect_location()389 if redirect_location:390 log.info("Redirecting %s -> %s" % (url, redirect_location))391 return self.urlopen(method, redirect_location, body, headers,392 retries - 1, redirect, assert_same_host)393394 return response395396
152 unmodified lines
modified:requests/packages/urllib3/exceptions.py+12 added-2 removed
16 unmodified lines
171819class MaxRetryError(HTTPError):20 "Raised when the maximum number of retries is exceeded."21 pass21 def __init__(self, url):22 HTTPError.__init__(self, "Max retries exceeded for url: %s" % url)23 self.url = url242526class TimeoutError(HTTPError):27 "Raised when a socket timeout occurs."
1 unmodified line
293031class HostChangedError(HTTPError):32 "Raised when an existing pool gets a request for a foreign host."31 pass33 def __init__(self, original_host, new_url, retries=3):34 HTTPError.__init__(self,35 "Connection pool with host '%s' tried to open a foreign host: %s" %36 (original_host, new_url))3738 self.original_host = original_host39 self.new_url = new_url40 self.retries = retries414243class EmptyPoolError(HTTPError):44 "Raised when a pool runs out of connections and no more are allowed."45 pass
modified:requests/packages/urllib3/poolmanager.py+14 added-9 removed
2 unmodified lines
3#4# This module is part of urllib3 and is released under5# the MIT License: http://www.opensource.org/licenses/mit-license.php67import logging89from ._collections import RecentlyUsedContainer8from .connectionpool import (9 HTTPConnectionPool, HTTPSConnectionPool,10 get_host, connection_from_url,11)10from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool11from .connectionpool import get_host, connection_from_url12from .exceptions import HostChangedError13from .request import RequestMethods141516__all__ = ['PoolManager', 'ProxyManager', 'proxy_from_url']171817from .request import RequestMethods18from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool192019pool_classes_by_scheme = {20 'http': HTTPConnectionPool,21 'https': HTTPSConnectionPool,22}
2 unmodified lines
25 'http': 80,26 'https': 443,27}2829log = logging.getLogger(__name__)303132class PoolManager(RequestMethods):33 """34 Allows for arbitrary requests while transparently keeping track of35 necessary connection pools for you.
68 unmodified lines
104 ``url`` must be absolute, such that an appropriate105 :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.106 """107 conn = self.connection_from_url(url)108 try:108 return conn.urlopen(method, url, assert_same_host=False, **kw)109 return conn.urlopen(method, url, **kw)110111 except HostChangedError, e:112 kw['retries'] = e.retries # Persist retries countdown113 return self.urlopen(method, e.new_url, **kw)114115116class ProxyManager(RequestMethods):117 """118 Given a ConnectionPool to a proxy, the ProxyManager's ``urlopen`` method119 will make requests to any url through the defined proxy.
14 unmodified lines
modified:requests/packages/urllib3/response.py+27 added-13 removed
82 unmodified lines
8384 if preload_content:85 self._body = self.read(decode_content=decode_content)8687 def get_redirect_location(self):88 """89 Should we redirect and where to?9091 :returns: Truthy redirect location string if we got a redirect status92 code and valid location. ``None`` if redirect status and no93 location. ``False`` if not a redirect status code.94 """95 if self.status in [301, 302, 303, 307]:96 return self.headers.get('location')9798 return False99100 def release_conn(self):101 if not self._pool or not self._connection:102 return103
6 unmodified lines
110 if self._body:111 return self._body112113 if self._fp:101 return self.read(decode_content=self._decode_content,102 cache_content=True)114 return self.read(cache_content=True)115104 def read(self, amt=None, decode_content=True, cache_content=False):116 def read(self, amt=None, decode_content=None, cache_content=False):117 """118 Similar to :meth:`httplib.HTTPResponse.read`, but with two additional119 parameters: ``decode_content`` and ``cache_content``.120
14 unmodified lines
135 set.)136 """137 content_encoding = self.headers.get('content-encoding')138 decoder = self.CONTENT_DECODERS.get(content_encoding)139 if decode_content is None:140 decode_content = self._decode_content141142 data = self._fp and self._fp.read(amt)143144 try:
22 unmodified lines
167168 if self._original_response and self._original_response.isclosed():169 self.release_conn()170157 @staticmethod158 def from_httplib(r, **response_kw):171 @classmethod172 def from_httplib(ResponseCls, r, **response_kw):173 """174 Given an :class:`httplib.HTTPResponse` instance ``r``, return a175 corresponding :class:`urllib3.response.HTTPResponse` object.176177 Remaining parameters are passed to the HTTPResponse constructor, along178 with ``original_response=r``.179 """180167 return HTTPResponse(body=r,168 headers=dict(r.getheaders()),169 status=r.status,170 version=r.version,171 reason=r.reason,172 strict=r.strict,173 original_response=r,174 **response_kw)181 return ResponseCls(body=r,182 headers=dict(r.getheaders()),183 status=r.status,184 version=r.version,185 reason=r.reason,186 strict=r.strict,187 original_response=r,188 **response_kw)189190 # Backwards-compatibility methods for httplib.HTTPResponse191 def getheaders(self):192 return self.headers
3 unmodified lines