Skip to content
Myr

Merge pull request #1128 from mvid/master

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

Updating urllib3 to current master

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 47aa968, 6e53290 · Browse files · 4 files+81 added−26 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+81 added−26 removed

  • other:+81 added−26 removed

4 files+81 added−26 removed

modified:requests/packages/urllib3/connectionpool.py+11 added-15 removed
7 unmodified lines
8import socket9import errno1011from socket import error as SocketError, timeout as SocketTimeout12from .util import resolve_cert_reqs, resolve_ssl_version1314try: # Python 315 from http.client import HTTPConnection, HTTPException16 from http.client import HTTP_PORT, HTTPS_PORT
63 unmodified lines
80 ca_certs = None81 ssl_version = None8283 def set_cert(self, key_file=None, cert_file=None,83 cert_reqs='CERT_NONE', ca_certs=None):84 ssl_req_scheme = {85 'CERT_NONE': ssl.CERT_NONE,86 'CERT_OPTIONAL': ssl.CERT_OPTIONAL,87 'CERT_REQUIRED': ssl.CERT_REQUIRED88 }84 cert_reqs=None, ca_certs=None):8586 self.key_file = key_file87 self.cert_file = cert_file92 self.cert_reqs = ssl_req_scheme.get(cert_reqs) or ssl.CERT_NONE88 self.cert_reqs = cert_reqs89 self.ca_certs = ca_certs9091 def connect(self):92 # Add certificate verification93 sock = socket.create_connection((self.host, self.port), self.timeout)9495 resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)96 resolved_ssl_version = resolve_ssl_version(self.ssl_version)9798 # Wrap socket using verification with the root certs in99 # trusted_root_certs100 self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file,102 cert_reqs=self.cert_reqs,101 cert_reqs=resolved_cert_reqs,102 ca_certs=self.ca_certs,103 server_hostname=self.host,105 ssl_version=self.ssl_version)104 ssl_version=resolved_ssl_version)105107 if self.ca_certs:106 if resolved_cert_reqs != ssl.CERT_NONE:107 match_hostname(self.sock.getpeercert(), self.host)108109110## Pool objects
401 unmodified lines
512 def __init__(self, host, port=None,513 strict=False, timeout=None, maxsize=1,514 block=False, headers=None,515 key_file=None, cert_file=None,517 cert_reqs='CERT_NONE', ca_certs=None, ssl_version=None):516 cert_reqs=None, ca_certs=None, ssl_version=None):517518 HTTPConnectionPool.__init__(self, host, port,519 strict, timeout, maxsize,520 block, headers)
25 unmodified lines
546 strict=self.strict)547 connection.set_cert(key_file=self.key_file, cert_file=self.cert_file,548 cert_reqs=self.cert_reqs, ca_certs=self.ca_certs)549551 if self.ssl_version is None:552 connection.ssl_version = ssl.PROTOCOL_SSLv23553 else:554 connection.ssl_version = self.ssl_version550 connection.ssl_version = self.ssl_version551552 return connection553554
25 unmodified lines
modified:requests/packages/urllib3/poolmanager.py+13 added-4 removed
56 unmodified lines
57 self.connection_pool_kw = connection_pool_kw58 self.pools = RecentlyUsedContainer(num_pools,59 dispose_func=lambda p: p.close())6061 def _new_pool(self, scheme, host, port):62 """63 Create a new :class:`ConnectionPool` based on host, port and scheme.6465 This method is used to actually create the connection pools handed out66 by :meth:`connection_from_url` and companion methods. It is intended67 to be overridden for customization.68 """69 pool_cls = pool_classes_by_scheme[scheme]70 return pool_cls(host, port, **self.connection_pool_kw)7172 def clear(self):73 """74 Empty our store of pools and direct them all to close.75
8 unmodified lines
8485 If ``port`` isn't given, it will be derived from the ``scheme`` using86 ``urllib3.connectionpool.port_by_scheme``.87 """88 scheme = scheme or 'http'89 port = port or port_by_scheme.get(scheme, 80)9091 pool_key = (scheme, host, port)92
3 unmodified lines
96 if pool:97 return pool9899 # Make a fresh ConnectionPool of the desired type88 pool_cls = pool_classes_by_scheme[scheme]89 pool = pool_cls(host, port, **self.connection_pool_kw)90100 pool = self._new_pool(scheme, host, port)101 self.pools[pool_key] = pool92102 return pool103104 def connection_from_url(self, url):105 """
67 unmodified lines
modified:requests/packages/urllib3/response.py+11 added-1 removed
143 unmodified lines
144 if amt is None:145 # cStringIO doesn't like amt=None146 data = self._fp.read()147 else:148 return self._fp.read(amt)148 data = self._fp.read(amt)149 if amt != 0 and not data: # Platform-specific: Buggy versions of Python.150 # Close the connection when no data is returned151 #152 # This is redundant to what httplib/http.client _should_153 # already do. However, versions of python released before154 # December 15, 2012 (http://bugs.python.org/issue16298) do not155 # properly close the connection in all cases. There is no harm156 # in redundantly calling close.157 self._fp.close()158 return data159160 try:161 if decode_content and decoder:162 data = decoder(data)
52 unmodified lines
modified:requests/packages/urllib3/util.py+46 added-6 removed
20 unmodified lines
21try: # Test for SSL features22 SSLContext = None23 HAS_SNI = False2425 import ssl26 from ssl import wrap_socket, CERT_NONE, SSLError, PROTOCOL_SSLv2327 from ssl import SSLContext # Modern SSL?28 from ssl import HAS_SNI # Has SNI?29except ImportError:
233 unmodified lines
263 # Either data is buffered (bad), or the connection is dropped.264 return True265266267def resolve_cert_reqs(candidate):268 """269 Resolves the argument to a numeric constant, which can be passed to270 the wrap_socket function/method from the ssl module.271 Defaults to :data:`ssl.CERT_NONE`.272 If given a string it is assumed to be the name of the constant in the273 :mod:`ssl` module or its abbrevation.274 (So you can specify `REQUIRED` instead of `CERT_REQUIRED`.275 If it's neither `None` nor a string we assume it is already the numeric276 constant which can directly be passed to wrap_socket.277 """278 if candidate is None:279 return CERT_NONE280281 if isinstance(candidate, str):282 res = getattr(ssl, candidate, None)283 if res is None:284 res = getattr(ssl, 'CERT_' + candidate)285 return res286287 return candidate288289290def resolve_ssl_version(candidate):291 """292 like resolve_cert_reqs293 """294 if candidate is None:295 return PROTOCOL_SSLv23296297 if isinstance(candidate, str):298 res = getattr(ssl, candidate, None)299 if res is None:300 res = getattr(ssl, 'PROTOCOL_' + candidate)301 return res302303 return candidate304305if SSLContext is not None: # Python 3.2+267 def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=CERT_NONE,306 def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,307 ca_certs=None, server_hostname=None,269 ssl_version=PROTOCOL_SSLv23):308 ssl_version=None):309 """310 All arguments except `server_hostname` have the same meaning as for311 :func:`ssl.wrap_socket`312
4 unmodified lines
317 context.verify_mode = cert_reqs318 if ca_certs:319 try:320 context.load_verify_locations(ca_certs)282 except TypeError as e: # Reraise as SSLError283 # FIXME: This block needs a test.321 # Py32 raises IOError322 # Py33 raises FileNotFoundError323 except Exception as e: # Reraise as SSLError324 raise SSLError(e)325 if certfile:326 # FIXME: This block needs a test.327 context.load_cert_chain(certfile, keyfile)
1 unmodified line
329 return context.wrap_socket(sock, server_hostname=server_hostname)330 return context.wrap_socket(sock)331332else: # Python 3.1 and earlier293 def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=CERT_NONE,333 def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,334 ca_certs=None, server_hostname=None,295 ssl_version=PROTOCOL_SSLv23):335 ssl_version=None):336 return wrap_socket(sock, keyfile=keyfile, certfile=certfile,337 ca_certs=ca_certs, cert_reqs=cert_reqs,338 ssl_version=ssl_version)