Plex plugin to to play various online streams (mostly Latvian).

connection.py 11KB

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