214
214
if num_bytes is None:
215
215
num_bytes = self._content_length
216
assert num_bytes <= self._content_length
216
if self._content_length is not None:
217
assert num_bytes <= self._content_length
217
218
if self._content is None:
218
219
assert self._z_content is not None
219
220
if self._z_content == '':
233
234
# decompressors 'unconsumed_tail'
234
235
self._z_content = None
235
236
# Do we have enough bytes already?
236
if len(self._content) >= num_bytes:
237
if num_bytes is not None and len(self._content) >= num_bytes:
238
239
# If we got this far, and don't have a decompressor, something is wrong
239
240
assert self._z_content_decompressor is not None
240
241
remaining_decomp = self._z_content_decompressor.unconsumed_tail
241
# If we have nothing left to decomp, we ran out of decomp bytes
242
assert remaining_decomp
243
needed_bytes = num_bytes - len(self._content)
244
# We always set max_size to 32kB over the minimum needed, so that zlib
245
# will give us as much as we really want.
246
# TODO: If this isn't good enough, we could make a loop here, that
247
# keeps expanding the request until we get enough
248
self._content += self._z_content_decompressor.decompress(
249
remaining_decomp, needed_bytes + _ZLIB_DECOMP_WINDOW)
250
assert len(self._content) >= num_bytes
251
if not self._z_content_decompressor.unconsumed_tail:
252
# The stream is finished
253
self._z_content_decompressor = None
242
if num_bytes is None:
244
# We don't know how much is left, but we'll decompress it all
245
self._content += self._z_content_decompressor.decompress(
247
# Note: There what I consider a bug in zlib.decompressobj
248
# If you pass back in the entire unconsumed_tail, only
249
# this time you don't pass a max-size, it doesn't
250
# change the unconsumed_tail back to None/''.
251
# However, we know we are done with the whole stream
252
self._z_content_decompressor = None
253
self._content_length = len(self._content)
255
# If we have nothing left to decomp, we ran out of decomp bytes
256
assert remaining_decomp
257
needed_bytes = num_bytes - len(self._content)
258
# We always set max_size to 32kB over the minimum needed, so that zlib
259
# will give us as much as we really want.
260
# TODO: If this isn't good enough, we could make a loop here, that
261
# keeps expanding the request until we get enough
262
self._content += self._z_content_decompressor.decompress(
263
remaining_decomp, needed_bytes + _ZLIB_DECOMP_WINDOW)
264
assert len(self._content) >= num_bytes
265
if not self._z_content_decompressor.unconsumed_tail:
266
# The stream is finished
267
self._z_content_decompressor = None
255
269
def _parse_bytes(self, bytes):
256
270
"""Read the various lengths from the header.