Coverage for requests.packages.urllib3.connection : 61%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# urllib3/connection.py # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt) # # This module is part of urllib3 and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php
"Used to detect a failed ConnectionCls import."
except (ImportError, AttributeError): # Platform-specific: No SSL. pass
ConnectTimeoutError, ) assert_fingerprint, resolve_cert_reqs, resolve_ssl_version, ssl_wrap_socket, )
'http': 80, 'https': 443, }
""" Based on httplib.HTTPConnection but provides an extra constructor backwards-compatibility layer between older and newer Pythons. """
# By default, disable Nagle's Algorithm.
kw.pop('strict', None) kw.pop('source_address', None)
# Pre-set source_address in case we have an older Python like 2.6.
# Superclass also sets self.source_address in Python 2.7+.
""" Establish a socket connection and set nodelay settings on it.
:return: a new socket connection """ extra_args.append(self.source_address)
(self.host, self.port), self.timeout, *extra_args) socket.IPPROTO_TCP, socket.TCP_NODELAY, self.tcp_nodelay)
# the _tunnel_host attribute was added in python 2.6.3 (via # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do # not have them. # TODO: Fix tunnel so it doesn't depend on self.sock state. self._tunnel()
strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, **kw):
HTTPConnection.__init__(self, host, port, strict=strict, timeout=timeout, **kw)
self.key_file = key_file self.cert_file = cert_file
# Required property for Google AppEngine 1.9.0 which otherwise causes # HTTPS requests to go out as HTTP. (See Issue #356) self._protocol = 'https'
conn = self._new_conn() self._prepare_conn(conn) self.sock = ssl.wrap_socket(conn, self.key_file, self.cert_file)
""" Based on httplib.HTTPSConnection but wraps the socket with SSL certification. """
cert_reqs=None, ca_certs=None, assert_hostname=None, assert_fingerprint=None):
self.key_file = key_file self.cert_file = cert_file self.cert_reqs = cert_reqs self.ca_certs = ca_certs self.assert_hostname = assert_hostname self.assert_fingerprint = assert_fingerprint
# Add certificate verification
try: sock = socket.create_connection( address=(self.host, self.port), timeout=self.timeout, **self.conn_kw) except SocketTimeout: raise ConnectTimeoutError( self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout))
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, self.tcp_nodelay)
resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) resolved_ssl_version = resolve_ssl_version(self.ssl_version)
hostname = self.host if getattr(self, '_tunnel_host', None): # _tunnel_host was added in Python 2.6.3 # (See: http://hg.python.org/cpython/rev/0f57b30a152f)
self.sock = sock # Calls self._set_hostport(), so self.host is # self._tunnel_host below. self._tunnel()
# Override the host with the one we're requesting data from. hostname = self._tunnel_host
# Wrap socket using verification with the root certs in # trusted_root_certs self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file, cert_reqs=resolved_cert_reqs, ca_certs=self.ca_certs, server_hostname=hostname, ssl_version=resolved_ssl_version)
if resolved_cert_reqs != ssl.CERT_NONE: if self.assert_fingerprint: assert_fingerprint(self.sock.getpeercert(binary_form=True), self.assert_fingerprint) elif self.assert_hostname is not False: match_hostname(self.sock.getpeercert(), self.assert_hostname or hostname)
# Make a copy for testing. |