diff options
author | Sviatoslav Sydorenko <wk@sydorenko.org.ua> | 2020-11-15 01:21:55 +0100 |
---|---|---|
committer | Sviatoslav Sydorenko <wk@sydorenko.org.ua> | 2020-11-15 02:58:16 +0100 |
commit | b4eb20e5803ccf11d5a84044ada8ebc6dc411ab1 (patch) | |
tree | 586686824ddbd5d52f3b22546cb414659f376ca1 /cherrypy/lib/encoding.py | |
parent | 05ee11a96940817e471cce3675ac1fb1810f11e8 (diff) | |
download | cherrypy-git-bugfixes/1849-gzip-compression-header-cpython-bug39389.tar.gz |
Fix compression level in meta header in gzip toolbugfixes/1849-gzip-compression-header-cpython-bug39389
* https://bugs.python.org/issue39389
* https://github.com/python/cpython/pull/18077
* https://github.com/python/cpython/pull/18100
* https://github.com/python/cpython/pull/18101
Fixes #1849
Diffstat (limited to 'cherrypy/lib/encoding.py')
-rw-r--r-- | cherrypy/lib/encoding.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py index 54a7a8a8..c2c478a5 100644 --- a/cherrypy/lib/encoding.py +++ b/cherrypy/lib/encoding.py @@ -9,6 +9,10 @@ from cherrypy.lib import is_closable_iterator from cherrypy.lib import set_vary_header +_COMPRESSION_LEVEL_FAST = 1 +_COMPRESSION_LEVEL_BEST = 9 + + def decode(encoding=None, default_encoding='utf-8'): """Replace or extend the list of charsets used to decode a request entity. @@ -285,13 +289,29 @@ def compress(body, compress_level): """Compress 'body' at the given compress_level.""" import zlib - # See http://www.gzip.org/zlib/rfc-gzip.html + # See https://tools.ietf.org/html/rfc1952 yield b'\x1f\x8b' # ID1 and ID2: gzip marker yield b'\x08' # CM: compression method yield b'\x00' # FLG: none set # MTIME: 4 bytes yield struct.pack('<L', int(time.time()) & int('FFFFFFFF', 16)) - yield b'\x02' # XFL: max compression, slowest algo + + # RFC 1952, section 2.3.1: + # + # XFL (eXtra FLags) + # These flags are available for use by specific compression + # methods. The "deflate" method (CM = 8) sets these flags as + # follows: + # + # XFL = 2 - compressor used maximum compression, + # slowest algorithm + # XFL = 4 - compressor used fastest algorithm + if compress_level == _COMPRESSION_LEVEL_BEST: + yield b'\x02' # XFL: max compression, slowest algo + elif compress_level == _COMPRESSION_LEVEL_FAST: + yield b'\x04' # XFL: min compression, fastest algo + else: + yield b'\x00' # XFL: compression unset/tradeoff yield b'\xff' # OS: unknown crc = zlib.crc32(b'') |