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

response.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. from __future__ import absolute_import
  2. from contextlib import contextmanager
  3. import zlib
  4. import io
  5. from socket import timeout as SocketTimeout
  6. from socket import error as SocketError
  7. from ._collections import HTTPHeaderDict
  8. from .exceptions import (
  9. ProtocolError, DecodeError, ReadTimeoutError, ResponseNotChunked
  10. )
  11. from .packages.six import string_types as basestring, binary_type, PY3
  12. from .packages.six.moves import http_client as httplib
  13. from .connection import HTTPException, BaseSSLError
  14. from .util.response import is_fp_closed, is_response_to_head
  15. class DeflateDecoder(object):
  16. def __init__(self):
  17. self._first_try = True
  18. self._data = binary_type()
  19. self._obj = zlib.decompressobj()
  20. def __getattr__(self, name):
  21. return getattr(self._obj, name)
  22. def decompress(self, data):
  23. if not data:
  24. return data
  25. if not self._first_try:
  26. return self._obj.decompress(data)
  27. self._data += data
  28. try:
  29. return self._obj.decompress(data)
  30. except zlib.error:
  31. self._first_try = False
  32. self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
  33. try:
  34. return self.decompress(self._data)
  35. finally:
  36. self._data = None
  37. class GzipDecoder(object):
  38. def __init__(self):
  39. self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS)
  40. def __getattr__(self, name):
  41. return getattr(self._obj, name)
  42. def decompress(self, data):
  43. if not data:
  44. return data
  45. return self._obj.decompress(data)
  46. def _get_decoder(mode):
  47. if mode == 'gzip':
  48. return GzipDecoder()
  49. return DeflateDecoder()
  50. class HTTPResponse(io.IOBase):
  51. """
  52. HTTP Response container.
  53. Backwards-compatible to httplib's HTTPResponse but the response ``body`` is
  54. loaded and decoded on-demand when the ``data`` property is accessed. This
  55. class is also compatible with the Python standard library's :mod:`io`
  56. module, and can hence be treated as a readable object in the context of that
  57. framework.
  58. Extra parameters for behaviour not present in httplib.HTTPResponse:
  59. :param preload_content:
  60. If True, the response's body will be preloaded during construction.
  61. :param decode_content:
  62. If True, attempts to decode specific content-encoding's based on headers
  63. (like 'gzip' and 'deflate') will be skipped and raw data will be used
  64. instead.
  65. :param original_response:
  66. When this HTTPResponse wrapper is generated from an httplib.HTTPResponse
  67. object, it's convenient to include the original for debug purposes. It's
  68. otherwise unused.
  69. """
  70. CONTENT_DECODERS = ['gzip', 'deflate']
  71. REDIRECT_STATUSES = [301, 302, 303, 307, 308]
  72. def __init__(self, body='', headers=None, status=0, version=0, reason=None,
  73. strict=0, preload_content=True, decode_content=True,
  74. original_response=None, pool=None, connection=None):
  75. if isinstance(headers, HTTPHeaderDict):
  76. self.headers = headers
  77. else:
  78. self.headers = HTTPHeaderDict(headers)
  79. self.status = status
  80. self.version = version
  81. self.reason = reason
  82. self.strict = strict
  83. self.decode_content = decode_content
  84. self._decoder = None
  85. self._body = None
  86. self._fp = None
  87. self._original_response = original_response
  88. self._fp_bytes_read = 0
  89. if body and isinstance(body, (basestring, binary_type)):
  90. self._body = body
  91. self._pool = pool
  92. self._connection = connection
  93. if hasattr(body, 'read'):
  94. self._fp = body
  95. # Are we using the chunked-style of transfer encoding?
  96. self.chunked = False
  97. self.chunk_left = None
  98. tr_enc = self.headers.get('transfer-encoding', '').lower()
  99. # Don't incur the penalty of creating a list and then discarding it
  100. encodings = (enc.strip() for enc in tr_enc.split(","))
  101. if "chunked" in encodings:
  102. self.chunked = True
  103. # If requested, preload the body.
  104. if preload_content and not self._body:
  105. self._body = self.read(decode_content=decode_content)
  106. def get_redirect_location(self):
  107. """
  108. Should we redirect and where to?
  109. :returns: Truthy redirect location string if we got a redirect status
  110. code and valid location. ``None`` if redirect status and no
  111. location. ``False`` if not a redirect status code.
  112. """
  113. if self.status in self.REDIRECT_STATUSES:
  114. return self.headers.get('location')
  115. return False
  116. def release_conn(self):
  117. if not self._pool or not self._connection:
  118. return
  119. self._pool._put_conn(self._connection)
  120. self._connection = None
  121. @property
  122. def data(self):
  123. # For backwords-compat with earlier urllib3 0.4 and earlier.
  124. if self._body:
  125. return self._body
  126. if self._fp:
  127. return self.read(cache_content=True)
  128. @property
  129. def connection(self):
  130. return self._connection
  131. def tell(self):
  132. """
  133. Obtain the number of bytes pulled over the wire so far. May differ from
  134. the amount of content returned by :meth:``HTTPResponse.read`` if bytes
  135. are encoded on the wire (e.g, compressed).
  136. """
  137. return self._fp_bytes_read
  138. def _init_decoder(self):
  139. """
  140. Set-up the _decoder attribute if necessar.
  141. """
  142. # Note: content-encoding value should be case-insensitive, per RFC 7230
  143. # Section 3.2
  144. content_encoding = self.headers.get('content-encoding', '').lower()
  145. if self._decoder is None and content_encoding in self.CONTENT_DECODERS:
  146. self._decoder = _get_decoder(content_encoding)
  147. def _decode(self, data, decode_content, flush_decoder):
  148. """
  149. Decode the data passed in and potentially flush the decoder.
  150. """
  151. try:
  152. if decode_content and self._decoder:
  153. data = self._decoder.decompress(data)
  154. except (IOError, zlib.error) as e:
  155. content_encoding = self.headers.get('content-encoding', '').lower()
  156. raise DecodeError(
  157. "Received response with content-encoding: %s, but "
  158. "failed to decode it." % content_encoding, e)
  159. if flush_decoder and decode_content:
  160. data += self._flush_decoder()
  161. return data
  162. def _flush_decoder(self):
  163. """
  164. Flushes the decoder. Should only be called if the decoder is actually
  165. being used.
  166. """
  167. if self._decoder:
  168. buf = self._decoder.decompress(b'')
  169. return buf + self._decoder.flush()
  170. return b''
  171. @contextmanager
  172. def _error_catcher(self):
  173. """
  174. Catch low-level python exceptions, instead re-raising urllib3
  175. variants, so that low-level exceptions are not leaked in the
  176. high-level api.
  177. On exit, release the connection back to the pool.
  178. """
  179. clean_exit = False
  180. try:
  181. try:
  182. yield
  183. except SocketTimeout:
  184. # FIXME: Ideally we'd like to include the url in the ReadTimeoutError but
  185. # there is yet no clean way to get at it from this context.
  186. raise ReadTimeoutError(self._pool, None, 'Read timed out.')
  187. except BaseSSLError as e:
  188. # FIXME: Is there a better way to differentiate between SSLErrors?
  189. if 'read operation timed out' not in str(e): # Defensive:
  190. # This shouldn't happen but just in case we're missing an edge
  191. # case, let's avoid swallowing SSL errors.
  192. raise
  193. raise ReadTimeoutError(self._pool, None, 'Read timed out.')
  194. except (HTTPException, SocketError) as e:
  195. # This includes IncompleteRead.
  196. raise ProtocolError('Connection broken: %r' % e, e)
  197. # If no exception is thrown, we should avoid cleaning up
  198. # unnecessarily.
  199. clean_exit = True
  200. finally:
  201. # If we didn't terminate cleanly, we need to throw away our
  202. # connection.
  203. if not clean_exit:
  204. # The response may not be closed but we're not going to use it
  205. # anymore so close it now to ensure that the connection is
  206. # released back to the pool.
  207. if self._original_response:
  208. self._original_response.close()
  209. # Closing the response may not actually be sufficient to close
  210. # everything, so if we have a hold of the connection close that
  211. # too.
  212. if self._connection:
  213. self._connection.close()
  214. # If we hold the original response but it's closed now, we should
  215. # return the connection back to the pool.
  216. if self._original_response and self._original_response.isclosed():
  217. self.release_conn()
  218. def read(self, amt=None, decode_content=None, cache_content=False):
  219. """
  220. Similar to :meth:`httplib.HTTPResponse.read`, but with two additional
  221. parameters: ``decode_content`` and ``cache_content``.
  222. :param amt:
  223. How much of the content to read. If specified, caching is skipped
  224. because it doesn't make sense to cache partial content as the full
  225. response.
  226. :param decode_content:
  227. If True, will attempt to decode the body based on the
  228. 'content-encoding' header.
  229. :param cache_content:
  230. If True, will save the returned data such that the same result is
  231. returned despite of the state of the underlying file object. This
  232. is useful if you want the ``.data`` property to continue working
  233. after having ``.read()`` the file object. (Overridden if ``amt`` is
  234. set.)
  235. """
  236. self._init_decoder()
  237. if decode_content is None:
  238. decode_content = self.decode_content
  239. if self._fp is None:
  240. return
  241. flush_decoder = False
  242. data = None
  243. with self._error_catcher():
  244. if amt is None:
  245. # cStringIO doesn't like amt=None
  246. data = self._fp.read()
  247. flush_decoder = True
  248. else:
  249. cache_content = False
  250. data = self._fp.read(amt)
  251. if amt != 0 and not data: # Platform-specific: Buggy versions of Python.
  252. # Close the connection when no data is returned
  253. #
  254. # This is redundant to what httplib/http.client _should_
  255. # already do. However, versions of python released before
  256. # December 15, 2012 (http://bugs.python.org/issue16298) do
  257. # not properly close the connection in all cases. There is
  258. # no harm in redundantly calling close.
  259. self._fp.close()
  260. flush_decoder = True
  261. if data:
  262. self._fp_bytes_read += len(data)
  263. data = self._decode(data, decode_content, flush_decoder)
  264. if cache_content:
  265. self._body = data
  266. return data
  267. def stream(self, amt=2**16, decode_content=None):
  268. """
  269. A generator wrapper for the read() method. A call will block until
  270. ``amt`` bytes have been read from the connection or until the
  271. connection is closed.
  272. :param amt:
  273. How much of the content to read. The generator will return up to
  274. much data per iteration, but may return less. This is particularly
  275. likely when using compressed data. However, the empty string will
  276. never be returned.
  277. :param decode_content:
  278. If True, will attempt to decode the body based on the
  279. 'content-encoding' header.
  280. """
  281. if self.chunked:
  282. for line in self.read_chunked(amt, decode_content=decode_content):
  283. yield line
  284. else:
  285. while not is_fp_closed(self._fp):
  286. data = self.read(amt=amt, decode_content=decode_content)
  287. if data:
  288. yield data
  289. @classmethod
  290. def from_httplib(ResponseCls, r, **response_kw):
  291. """
  292. Given an :class:`httplib.HTTPResponse` instance ``r``, return a
  293. corresponding :class:`urllib3.response.HTTPResponse` object.
  294. Remaining parameters are passed to the HTTPResponse constructor, along
  295. with ``original_response=r``.
  296. """
  297. headers = r.msg
  298. if not isinstance(headers, HTTPHeaderDict):
  299. if PY3: # Python 3
  300. headers = HTTPHeaderDict(headers.items())
  301. else: # Python 2
  302. headers = HTTPHeaderDict.from_httplib(headers)
  303. # HTTPResponse objects in Python 3 don't have a .strict attribute
  304. strict = getattr(r, 'strict', 0)
  305. resp = ResponseCls(body=r,
  306. headers=headers,
  307. status=r.status,
  308. version=r.version,
  309. reason=r.reason,
  310. strict=strict,
  311. original_response=r,
  312. **response_kw)
  313. return resp
  314. # Backwards-compatibility methods for httplib.HTTPResponse
  315. def getheaders(self):
  316. return self.headers
  317. def getheader(self, name, default=None):
  318. return self.headers.get(name, default)
  319. # Overrides from io.IOBase
  320. def close(self):
  321. if not self.closed:
  322. self._fp.close()
  323. if self._connection:
  324. self._connection.close()
  325. @property
  326. def closed(self):
  327. if self._fp is None:
  328. return True
  329. elif hasattr(self._fp, 'closed'):
  330. return self._fp.closed
  331. elif hasattr(self._fp, 'isclosed'): # Python 2
  332. return self._fp.isclosed()
  333. else:
  334. return True
  335. def fileno(self):
  336. if self._fp is None:
  337. raise IOError("HTTPResponse has no file to get a fileno from")
  338. elif hasattr(self._fp, "fileno"):
  339. return self._fp.fileno()
  340. else:
  341. raise IOError("The file-like object this HTTPResponse is wrapped "
  342. "around has no file descriptor")
  343. def flush(self):
  344. if self._fp is not None and hasattr(self._fp, 'flush'):
  345. return self._fp.flush()
  346. def readable(self):
  347. # This method is required for `io` module compatibility.
  348. return True
  349. def readinto(self, b):
  350. # This method is required for `io` module compatibility.
  351. temp = self.read(len(b))
  352. if len(temp) == 0:
  353. return 0
  354. else:
  355. b[:len(temp)] = temp
  356. return len(temp)
  357. def _update_chunk_length(self):
  358. # First, we'll figure out length of a chunk and then
  359. # we'll try to read it from socket.
  360. if self.chunk_left is not None:
  361. return
  362. line = self._fp.fp.readline()
  363. line = line.split(b';', 1)[0]
  364. try:
  365. self.chunk_left = int(line, 16)
  366. except ValueError:
  367. # Invalid chunked protocol response, abort.
  368. self.close()
  369. raise httplib.IncompleteRead(line)
  370. def _handle_chunk(self, amt):
  371. returned_chunk = None
  372. if amt is None:
  373. chunk = self._fp._safe_read(self.chunk_left)
  374. returned_chunk = chunk
  375. self._fp._safe_read(2) # Toss the CRLF at the end of the chunk.
  376. self.chunk_left = None
  377. elif amt < self.chunk_left:
  378. value = self._fp._safe_read(amt)
  379. self.chunk_left = self.chunk_left - amt
  380. returned_chunk = value
  381. elif amt == self.chunk_left:
  382. value = self._fp._safe_read(amt)
  383. self._fp._safe_read(2) # Toss the CRLF at the end of the chunk.
  384. self.chunk_left = None
  385. returned_chunk = value
  386. else: # amt > self.chunk_left
  387. returned_chunk = self._fp._safe_read(self.chunk_left)
  388. self._fp._safe_read(2) # Toss the CRLF at the end of the chunk.
  389. self.chunk_left = None
  390. return returned_chunk
  391. def read_chunked(self, amt=None, decode_content=None):
  392. """
  393. Similar to :meth:`HTTPResponse.read`, but with an additional
  394. parameter: ``decode_content``.
  395. :param decode_content:
  396. If True, will attempt to decode the body based on the
  397. 'content-encoding' header.
  398. """
  399. self._init_decoder()
  400. # FIXME: Rewrite this method and make it a class with a better structured logic.
  401. if not self.chunked:
  402. raise ResponseNotChunked(
  403. "Response is not chunked. "
  404. "Header 'transfer-encoding: chunked' is missing.")
  405. # Don't bother reading the body of a HEAD request.
  406. if self._original_response and is_response_to_head(self._original_response):
  407. self._original_response.close()
  408. return
  409. with self._error_catcher():
  410. while True:
  411. self._update_chunk_length()
  412. if self.chunk_left == 0:
  413. break
  414. chunk = self._handle_chunk(amt)
  415. decoded = self._decode(chunk, decode_content=decode_content,
  416. flush_decoder=False)
  417. if decoded:
  418. yield decoded
  419. if decode_content:
  420. # On CPython and PyPy, we should never need to flush the
  421. # decoder. However, on Jython we *might* need to, so
  422. # lets defensively do it anyway.
  423. decoded = self._flush_decoder()
  424. if decoded: # Platform-specific: Jython.
  425. yield decoded
  426. # Chunk content ends with \r\n: discard it.
  427. while True:
  428. line = self._fp.fp.readline()
  429. if not line:
  430. # Some sites may not end with '\r\n'.
  431. break
  432. if line == b'\r\n':
  433. break
  434. # We read everything; close the "file".
  435. if self._original_response:
  436. self._original_response.close()