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

response.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import absolute_import
  2. from ..packages.six.moves import http_client as httplib
  3. from ..exceptions import HeaderParsingError
  4. def is_fp_closed(obj):
  5. """
  6. Checks whether a given file-like object is closed.
  7. :param obj:
  8. The file-like object to check.
  9. """
  10. try:
  11. # Check via the official file-like-object way.
  12. return obj.closed
  13. except AttributeError:
  14. pass
  15. try:
  16. # Check if the object is a container for another file-like object that
  17. # gets released on exhaustion (e.g. HTTPResponse).
  18. return obj.fp is None
  19. except AttributeError:
  20. pass
  21. raise ValueError("Unable to determine whether fp is closed.")
  22. def assert_header_parsing(headers):
  23. """
  24. Asserts whether all headers have been successfully parsed.
  25. Extracts encountered errors from the result of parsing headers.
  26. Only works on Python 3.
  27. :param headers: Headers to verify.
  28. :type headers: `httplib.HTTPMessage`.
  29. :raises urllib3.exceptions.HeaderParsingError:
  30. If parsing errors are found.
  31. """
  32. # This will fail silently if we pass in the wrong kind of parameter.
  33. # To make debugging easier add an explicit check.
  34. if not isinstance(headers, httplib.HTTPMessage):
  35. raise TypeError('expected httplib.Message, got {0}.'.format(
  36. type(headers)))
  37. defects = getattr(headers, 'defects', None)
  38. get_payload = getattr(headers, 'get_payload', None)
  39. unparsed_data = None
  40. if get_payload: # Platform-specific: Python 3.
  41. unparsed_data = get_payload()
  42. if defects or unparsed_data:
  43. raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
  44. def is_response_to_head(response):
  45. """
  46. Checks whether the request of a response has been a HEAD-request.
  47. Handles the quirks of AppEngine.
  48. :param conn:
  49. :type conn: :class:`httplib.HTTPResponse`
  50. """
  51. # FIXME: Can we do this somehow without accessing private httplib _method?
  52. method = response._method
  53. if isinstance(method, int): # Platform-specific: Appengine
  54. return method == 3
  55. return method.upper() == 'HEAD'