Have the layer PUT method calculate the uncompressed size in realtime, as trusting the JSON is fraught with complications

This commit is contained in:
Joseph Schorr 2014-09-29 17:00:47 -04:00
parent 7fd3c7d31b
commit 474add0fb1
3 changed files with 48 additions and 5 deletions

25
util/gzipstream.py Normal file
View file

@ -0,0 +1,25 @@
"""
Defines utility methods for working with gzip streams.
"""
import zlib
# Window size for decompressing GZIP streams.
# This results in ZLIB automatically detecting the GZIP headers.
# http://stackoverflow.com/questions/3122145/zlib-error-error-3-while-decompressing-incorrect-header-check/22310760#22310760
ZLIB_GZIP_WINDOW = zlib.MAX_WBITS | 32
def calculate_size_handler():
""" Returns an object and a SocketReader handler. The handler will gunzip the data it receives,
adding the size found to the object.
"""
uncompressed_size_info = {
'size': 0
}
decompressor = zlib.decompressobj(ZLIB_GZIP_WINDOW)
def fn(buf):
uncompressed_size_info['size'] += len(decompressor.decompress(buf))
return uncompressed_size_info, fn