Merge pull request #1451 from YanchWare/master

Integrated encode/decoder for brotli
This commit is contained in:
Maximilian Hils 2016-07-30 15:56:15 -07:00 committed by GitHub
commit f008bdf590
6 changed files with 30 additions and 4 deletions

View File

@ -713,6 +713,7 @@ class FlowView(tabs.Tabs):
keys = ( keys = (
("gzip", "z"), ("gzip", "z"),
("deflate", "d"), ("deflate", "d"),
("brotli", "b"),
), ),
callback = self.encode_callback, callback = self.encode_callback,
args = (conn,) args = (conn,)
@ -726,6 +727,7 @@ class FlowView(tabs.Tabs):
encoding_map = { encoding_map = {
"z": "gzip", "z": "gzip",
"d": "deflate", "d": "deflate",
"b": "brotli",
} }
conn.encode(encoding_map[key]) conn.encode(encoding_map[key])
signals.flow_change.send(self, flow = self.flow) signals.flow_change.send(self, flow = self.flow)

View File

@ -8,6 +8,7 @@ import collections
from io import BytesIO from io import BytesIO
import gzip import gzip
import zlib import zlib
import brotli
from typing import Union # noqa from typing import Union # noqa
@ -45,7 +46,7 @@ def decode(encoded, encoding, errors='strict'):
decoded = custom_decode[encoding](encoded) decoded = custom_decode[encoding](encoded)
except KeyError: except KeyError:
decoded = codecs.decode(encoded, encoding, errors) decoded = codecs.decode(encoded, encoding, errors)
if encoding in ("gzip", "deflate"): if encoding in ("gzip", "deflate", "br"):
_cache = CachedDecode(encoded, encoding, errors, decoded) _cache = CachedDecode(encoded, encoding, errors, decoded)
return decoded return decoded
except Exception as e: except Exception as e:
@ -81,7 +82,7 @@ def encode(decoded, encoding, errors='strict'):
encoded = custom_encode[encoding](decoded) encoded = custom_encode[encoding](decoded)
except KeyError: except KeyError:
encoded = codecs.encode(decoded, encoding, errors) encoded = codecs.encode(decoded, encoding, errors)
if encoding in ("gzip", "deflate"): if encoding in ("gzip", "deflate", "br"):
_cache = CachedDecode(encoded, encoding, errors, decoded) _cache = CachedDecode(encoded, encoding, errors, decoded)
return encoded return encoded
except Exception as e: except Exception as e:
@ -113,6 +114,14 @@ def encode_gzip(content):
return s.getvalue() return s.getvalue()
def decode_brotli(content):
return brotli.decompress(content)
def encode_brotli(content):
return brotli.compress(content)
def decode_deflate(content): def decode_deflate(content):
""" """
Returns decompressed data for DEFLATE. Some servers may respond with Returns decompressed data for DEFLATE. Some servers may respond with
@ -139,11 +148,13 @@ custom_decode = {
"identity": identity, "identity": identity,
"gzip": decode_gzip, "gzip": decode_gzip,
"deflate": decode_deflate, "deflate": decode_deflate,
"br": decode_brotli,
} }
custom_encode = { custom_encode = {
"identity": identity, "identity": identity,
"gzip": encode_gzip, "gzip": encode_gzip,
"deflate": encode_deflate, "deflate": encode_deflate,
"br": encode_brotli,
} }
__all__ = ["encode", "decode"] __all__ = ["encode", "decode"]

View File

@ -248,7 +248,7 @@ class Message(basetypes.Serializable):
def encode(self, e): def encode(self, e):
""" """
Encodes body with the encoding e, where e is "gzip", "deflate" or "identity". Encodes body with the encoding e, where e is "gzip", "deflate", "identity", or "br".
Any existing content-encodings are overwritten, Any existing content-encodings are overwritten,
the content is not decoded beforehand. the content is not decoded beforehand.

View File

@ -337,7 +337,7 @@ class Request(message.Message):
self.headers["accept-encoding"] = ( self.headers["accept-encoding"] = (
', '.join( ', '.join(
e e
for e in {"gzip", "identity", "deflate"} for e in {"gzip", "identity", "deflate", "br"}
if e in accept_encoding if e in accept_encoding
) )
) )

View File

@ -85,6 +85,7 @@ setup(
"tornado>=4.3, <4.5", "tornado>=4.3, <4.5",
"urwid>=1.3.1, <1.4", "urwid>=1.3.1, <1.4",
"watchdog>=0.8.3, <0.9", "watchdog>=0.8.3, <0.9",
"brotlipy>=0.3.0, <0.4",
], ],
extras_require={ extras_require={
':sys_platform == "win32"': [ ':sys_platform == "win32"': [

View File

@ -21,6 +21,18 @@ def test_gzip():
encoding.decode(b"bogus", "gzip") encoding.decode(b"bogus", "gzip")
def test_brotli():
assert b"string" == encoding.decode(
encoding.encode(
b"string",
"br"
),
"br"
)
with tutils.raises(ValueError):
encoding.decode(b"bogus", "br")
def test_deflate(): def test_deflate():
assert b"string" == encoding.decode( assert b"string" == encoding.decode(
encoding.encode( encoding.encode(