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