Correct Gzip compression issue on Python 3.5

pull/1243/head
nicolargo 2018-02-25 10:06:39 +01:00
parent 15bbf22339
commit f5fbd65996
2 changed files with 12 additions and 24 deletions

View File

@ -48,6 +48,7 @@ if PY3:
text_type = str
binary_type = bytes
bool_type = bool
long = int
viewkeys = operator.methodcaller('keys')
viewvalues = operator.methodcaller('values')
@ -119,6 +120,7 @@ else:
text_type = unicode
binary_type = str
bool_type = types.BooleanType
long = long
viewkeys = operator.methodcaller('viewkeys')
viewvalues = operator.methodcaller('viewvalues')

View File

@ -29,6 +29,7 @@ import zlib
import struct
import time
from glances.compat import nativestr, u, b, long
from glances.timer import Timer
from glances.logger import logger
@ -56,32 +57,17 @@ def gzip_compress(func):
response.headers['Content-Encoding'] = 'identity'
return ret
def gzip_header():
header = '\037\213'
header += '\010'
header += '\0'
header += struct.pack("<L", long(time.time()))
header += '\002'
header += '\377'
return header
def gzip_trailer(crc, size):
footer = struct.pack("<l", crc)
footer += struct.pack("<L", size & 0xFFFFFFFF)
return footer
def compress(data, compress_level=6):
# Compress page
yield gzip_header()
crc = zlib.crc32('')
"""Compress given data using the DEFLATE algorithm"""
# Init compression
zobj = zlib.compressobj(compress_level,
zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0)
size = len(data)
crc = zlib.crc32(data, crc)
yield zobj.compress(data)
yield zobj.flush()
yield gzip_trailer(crc, size)
zlib.DEFLATED,
zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL,
zlib.Z_DEFAULT_STRATEGY)
# Return compressed object
return zobj.compress(b(data)) + zobj.flush()
return wrapper