|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +hyper/http20/response |
| 4 | +~~~~~~~~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | +Contains the HTTP/2 equivalent of the HTTPResponse object defined in |
| 7 | +httplib/http.client. |
| 8 | +""" |
| 9 | +import logging |
| 10 | + |
| 11 | +log = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class DeflateDecoder(object): |
| 15 | + """ |
| 16 | + This is a decoding object that wraps ``zlib`` and is used for decoding |
| 17 | + deflated content. |
| 18 | +
|
| 19 | + This rationale for the existence of this object is pretty unpleasant. |
| 20 | + The HTTP RFC specifies that 'deflate' is a valid content encoding. However, |
| 21 | + the spec _meant_ the zlib encoding form. Unfortunately, people who didn't |
| 22 | + read the RFC very carefully actually implemented a different form of |
| 23 | + 'deflate'. Insanely, ``zlib`` handles them using two wbits values. This is |
| 24 | + such a mess it's hard to adequately articulate. |
| 25 | +
|
| 26 | + This class was lovingly borrowed from the excellent urllib3 library under |
| 27 | + license: see NOTICES. If you ever see @shazow, you should probably buy him |
| 28 | + a drink or something. |
| 29 | + """ |
| 30 | + def __init__(self): |
| 31 | + self._first_try = True |
| 32 | + self._data = b'' |
| 33 | + self._obj = zlib.decompressobj(zlib.MAX_WBITS) |
| 34 | + |
| 35 | + def __getattr__(self, name): |
| 36 | + return getattr(self._obj, name) |
| 37 | + |
| 38 | + def decompress(self, data): |
| 39 | + if not self._first_try: |
| 40 | + return self._obj.decompress(data) |
| 41 | + |
| 42 | + self._data += data |
| 43 | + try: |
| 44 | + return self._obj.decompress(data) |
| 45 | + except zlib.error: |
| 46 | + self._first_try = False |
| 47 | + self._obj = zlib.decompressobj(-zlib.MAX_WBITS) |
| 48 | + try: |
| 49 | + return self.decompress(self._data) |
| 50 | + finally: |
| 51 | + self._data = None |
| 52 | + |
| 53 | + |
| 54 | +class HTTP11Response(object): |
| 55 | + """ |
| 56 | + An ``HTTP11Response`` wraps the HTTP/1.1 response from the server. It |
| 57 | + provides access to the response headers and the entity body. The response |
| 58 | + is an iterable object and can be used in a with statement. |
| 59 | + """ |
| 60 | + def __init__(self, headers, sock): |
| 61 | + #: The reason phrase returned by the server. |
| 62 | + self.reason = '' |
| 63 | + |
| 64 | + #: The status code returned by the server. |
| 65 | + self.status = 0 |
| 66 | + |
| 67 | + # The response headers. These are determined upon creation, assigned |
| 68 | + # once, and never assigned again. |
| 69 | + self._headers = headers |
| 70 | + |
| 71 | + # The response trailers. These are always intially ``None``. |
| 72 | + self._trailers = None |
| 73 | + |
| 74 | + # The socket this response is being sent over. |
| 75 | + self._sock = sock |
| 76 | + |
| 77 | + # We always read in one-data-frame increments from the stream, so we |
| 78 | + # may need to buffer some for incomplete reads. |
| 79 | + self._data_buffer = b'' |
| 80 | + |
| 81 | + def read(self, amt=None, decode_content=True): |
| 82 | + """ |
| 83 | + Reads the response body, or up to the next ``amt`` bytes. |
| 84 | +
|
| 85 | + :param amt: (optional) The amount of data to read. If not provided, all |
| 86 | + the data will be read from the response. |
| 87 | + :param decode_content: (optional) If ``True``, will transparently |
| 88 | + decode the response data. |
| 89 | + :returns: The read data. Note that if ``decode_content`` is set to |
| 90 | + ``True``, the actual amount of data returned may be different to |
| 91 | + the amount requested. |
| 92 | + """ |
| 93 | + # For now, just read what we're asked, unless we're not asked: |
| 94 | + # then, read content-length. This obviously doesn't work longer term, |
| 95 | + # we need to do some content-length processing there. |
| 96 | + if amt is None: |
| 97 | + amt = self.headers.get(b'content-length', 0) |
| 98 | + |
| 99 | + # Return early if we've lost our connection. |
| 100 | + if self._sock is None: |
| 101 | + return b'' |
| 102 | + |
| 103 | + data = self._sock.read(amt) |
| 104 | + |
| 105 | + # We may need to decode the body. |
| 106 | + if decode_content and self._decompressobj and data: |
| 107 | + data = self._decompressobj.decompress(data) |
| 108 | + |
| 109 | + # If we're at the end of the request, we have some cleaning up to do. |
| 110 | + # Close the stream, and if necessary flush the buffer. |
| 111 | + if decode_content and self._decompressobj: |
| 112 | + data += self._decompressobj.flush() |
| 113 | + |
| 114 | + # We're at the end. Close the connection. |
| 115 | + if not data: |
| 116 | + self.close() |
| 117 | + |
| 118 | + return data |
| 119 | + |
| 120 | + def fileno(self): |
| 121 | + """ |
| 122 | + Return the ``fileno`` of the underlying socket. This function is |
| 123 | + currently not implemented. |
| 124 | + """ |
| 125 | + raise NotImplementedError("Not currently implemented.") |
| 126 | + |
| 127 | + def close(self): |
| 128 | + """ |
| 129 | + Close the response. In effect this closes the backing HTTP/2 stream. |
| 130 | +
|
| 131 | + :returns: Nothing. |
| 132 | + """ |
| 133 | + self._sock = None |
| 134 | + |
| 135 | + # The following methods implement the context manager protocol. |
| 136 | + def __enter__(self): |
| 137 | + return self |
| 138 | + |
| 139 | + def __exit__(self, *args): |
| 140 | + self.close() |
| 141 | + return False # Never swallow exceptions. |
0 commit comments