mirror of
https://github.com/nicolargo/glances.git
synced 2026-03-13 11:28:12 -04:00
Correct Gzip compression issue on Python 3.5
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user