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