connection.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import datetime
  2. import sys
  3. import socket
  4. from socket import timeout as SocketTimeout
  5. import warnings
  6. from .packages import six
  7. try: # Python 3
  8. from http.client import HTTPConnection as _HTTPConnection, HTTPException
  9. except ImportError:
  10. from httplib import HTTPConnection as _HTTPConnection, HTTPException
  11. class DummyConnection(object):
  12. "Used to detect a failed ConnectionCls import."
  13. pass
  14. try: # Compiled with SSL?
  15. HTTPSConnection = DummyConnection
  16. import ssl
  17. BaseSSLError = ssl.SSLError
  18. except (ImportError, AttributeError): # Platform-specific: No SSL.
  19. ssl = None
  20. class BaseSSLError(BaseException):
  21. pass
  22. try: # Python 3:
  23. # Not a no-op, we're adding this to the namespace so it can be imported.
  24. ConnectionError = ConnectionError
  25. except NameError: # Python 2:
  26. class ConnectionError(Exception):
  27. pass
  28. from .exceptions import (
  29. ConnectTimeoutError,
  30. SystemTimeWarning,
  31. SecurityWarning,
  32. )
  33. from .packages.ssl_match_hostname import match_hostname
  34. from .util.ssl_ import (
  35. resolve_cert_reqs,
  36. resolve_ssl_version,
  37. ssl_wrap_socket,
  38. assert_fingerprint,
  39. )
  40. from .util import connection
  41. port_by_scheme = {
  42. 'http': 80,
  43. 'https': 443,
  44. }
  45. RECENT_DATE = datetime.date(2014, 1, 1)
  46. class HTTPConnection(_HTTPConnection, object):
  47. """
  48. Based on httplib.HTTPConnection but provides an extra constructor
  49. backwards-compatibility layer between older and newer Pythons.
  50. Additional keyword parameters are used to configure attributes of the connection.
  51. Accepted parameters include:
  52. - ``strict``: See the documentation on :class:`urllib3.connectionpool.HTTPConnectionPool`
  53. - ``source_address``: Set the source address for the current connection.
  54. .. note:: This is ignored for Python 2.6. It is only applied for 2.7 and 3.x
  55. - ``socket_options``: Set specific options on the underlying socket. If not specified, then
  56. defaults are loaded from ``HTTPConnection.default_socket_options`` which includes disabling
  57. Nagle's algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy.
  58. For example, if you wish to enable TCP Keep Alive in addition to the defaults,
  59. you might pass::
  60. HTTPConnection.default_socket_options + [
  61. (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
  62. ]
  63. Or you may want to disable the defaults by passing an empty list (e.g., ``[]``).
  64. """
  65. default_port = port_by_scheme['http']
  66. #: Disable Nagle's algorithm by default.
  67. #: ``[(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]``
  68. default_socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
  69. #: Whether this connection verifies the host's certificate.
  70. is_verified = False
  71. def __init__(self, *args, **kw):
  72. if six.PY3: # Python 3
  73. kw.pop('strict', None)
  74. # Pre-set source_address in case we have an older Python like 2.6.
  75. self.source_address = kw.get('source_address')
  76. if sys.version_info < (2, 7): # Python 2.6
  77. # _HTTPConnection on Python 2.6 will balk at this keyword arg, but
  78. # not newer versions. We can still use it when creating a
  79. # connection though, so we pop it *after* we have saved it as
  80. # self.source_address.
  81. kw.pop('source_address', None)
  82. #: The socket options provided by the user. If no options are
  83. #: provided, we use the default options.
  84. self.socket_options = kw.pop('socket_options', self.default_socket_options)
  85. # Superclass also sets self.source_address in Python 2.7+.
  86. _HTTPConnection.__init__(self, *args, **kw)
  87. def _new_conn(self):
  88. """ Establish a socket connection and set nodelay settings on it.
  89. :return: New socket connection.
  90. """
  91. extra_kw = {}
  92. if self.source_address:
  93. extra_kw['source_address'] = self.source_address
  94. if self.socket_options:
  95. extra_kw['socket_options'] = self.socket_options
  96. try:
  97. conn = connection.create_connection(
  98. (self.host, self.port), self.timeout, **extra_kw)
  99. except SocketTimeout:
  100. raise ConnectTimeoutError(
  101. self, "Connection to %s timed out. (connect timeout=%s)" %
  102. (self.host, self.timeout))
  103. return conn
  104. def _prepare_conn(self, conn):
  105. self.sock = conn
  106. # the _tunnel_host attribute was added in python 2.6.3 (via
  107. # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do
  108. # not have them.
  109. if getattr(self, '_tunnel_host', None):
  110. # TODO: Fix tunnel so it doesn't depend on self.sock state.
  111. self._tunnel()
  112. # Mark this connection as not reusable
  113. self.auto_open = 0
  114. def connect(self):
  115. conn = self._new_conn()
  116. self._prepare_conn(conn)
  117. class HTTPSConnection(HTTPConnection):
  118. default_port = port_by_scheme['https']
  119. def __init__(self, host, port=None, key_file=None, cert_file=None,
  120. strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, **kw):
  121. HTTPConnection.__init__(self, host, port, strict=strict,
  122. timeout=timeout, **kw)
  123. self.key_file = key_file
  124. self.cert_file = cert_file
  125. # Required property for Google AppEngine 1.9.0 which otherwise causes
  126. # HTTPS requests to go out as HTTP. (See Issue #356)
  127. self._protocol = 'https'
  128. def connect(self):
  129. conn = self._new_conn()
  130. self._prepare_conn(conn)
  131. self.sock = ssl.wrap_socket(conn, self.key_file, self.cert_file)
  132. class VerifiedHTTPSConnection(HTTPSConnection):
  133. """
  134. Based on httplib.HTTPSConnection but wraps the socket with
  135. SSL certification.
  136. """
  137. cert_reqs = None
  138. ca_certs = None
  139. ssl_version = None
  140. assert_fingerprint = None
  141. def set_cert(self, key_file=None, cert_file=None,
  142. cert_reqs=None, ca_certs=None,
  143. assert_hostname=None, assert_fingerprint=None):
  144. self.key_file = key_file
  145. self.cert_file = cert_file
  146. self.cert_reqs = cert_reqs
  147. self.ca_certs = ca_certs
  148. self.assert_hostname = assert_hostname
  149. self.assert_fingerprint = assert_fingerprint
  150. def connect(self):
  151. # Add certificate verification
  152. conn = self._new_conn()
  153. resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
  154. resolved_ssl_version = resolve_ssl_version(self.ssl_version)
  155. hostname = self.host
  156. if getattr(self, '_tunnel_host', None):
  157. # _tunnel_host was added in Python 2.6.3
  158. # (See: http://hg.python.org/cpython/rev/0f57b30a152f)
  159. self.sock = conn
  160. # Calls self._set_hostport(), so self.host is
  161. # self._tunnel_host below.
  162. self._tunnel()
  163. # Mark this connection as not reusable
  164. self.auto_open = 0
  165. # Override the host with the one we're requesting data from.
  166. hostname = self._tunnel_host
  167. is_time_off = datetime.date.today() < RECENT_DATE
  168. if is_time_off:
  169. warnings.warn((
  170. 'System time is way off (before {0}). This will probably '
  171. 'lead to SSL verification errors').format(RECENT_DATE),
  172. SystemTimeWarning
  173. )
  174. # Wrap socket using verification with the root certs in
  175. # trusted_root_certs
  176. self.sock = ssl_wrap_socket(conn, self.key_file, self.cert_file,
  177. cert_reqs=resolved_cert_reqs,
  178. ca_certs=self.ca_certs,
  179. server_hostname=hostname,
  180. ssl_version=resolved_ssl_version)
  181. if self.assert_fingerprint:
  182. assert_fingerprint(self.sock.getpeercert(binary_form=True),
  183. self.assert_fingerprint)
  184. elif resolved_cert_reqs != ssl.CERT_NONE \
  185. and self.assert_hostname is not False:
  186. cert = self.sock.getpeercert()
  187. if not cert.get('subjectAltName', ()):
  188. warnings.warn((
  189. 'Certificate has no `subjectAltName`, falling back to check for a `commonName` for now. '
  190. 'This feature is being removed by major browsers and deprecated by RFC 2818. '
  191. '(See https://github.com/shazow/urllib3/issues/497 for details.)'),
  192. SecurityWarning
  193. )
  194. match_hostname(cert, self.assert_hostname or hostname)
  195. self.is_verified = (resolved_cert_reqs == ssl.CERT_REQUIRED
  196. or self.assert_fingerprint is not None)
  197. if ssl:
  198. # Make a copy for testing.
  199. UnverifiedHTTPSConnection = HTTPSConnection
  200. HTTPSConnection = VerifiedHTTPSConnection
  201. else:
  202. HTTPSConnection = DummyConnection