aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2019-09-05 22:09:51 +0200
committerGitHub <noreply@github.com>2019-09-05 22:09:51 +0200
commitede2d1b243713e847eff87aa8dc6913ab694b920 (patch)
tree16f4ace19f153f6142fbe716b79b118252e4ee9f /mitmproxy
parentebfff8db761bf101ae46ac9ca0983408897baad2 (diff)
parentdd3589ce345bcabe51563e1e1ac83797353baf8c (diff)
downloadmitmproxy-ede2d1b243713e847eff87aa8dc6913ab694b920.tar.gz
mitmproxy-ede2d1b243713e847eff87aa8dc6913ab694b920.tar.bz2
mitmproxy-ede2d1b243713e847eff87aa8dc6913ab694b920.zip
Merge pull request #3630 from tsaaristo/encode-zstandard
Add support for zstd Content-Encoding
Diffstat (limited to 'mitmproxy')
-rw-r--r--mitmproxy/addons/core.py2
-rw-r--r--mitmproxy/net/http/encoding.py24
-rw-r--r--mitmproxy/net/http/message.py2
-rw-r--r--mitmproxy/net/http/request.py2
4 files changed, 25 insertions, 5 deletions
diff --git a/mitmproxy/addons/core.py b/mitmproxy/addons/core.py
index a908dbb3..5c9bbcd0 100644
--- a/mitmproxy/addons/core.py
+++ b/mitmproxy/addons/core.py
@@ -289,7 +289,7 @@ class Core:
"""
The possible values for an encoding specification.
"""
- return ["gzip", "deflate", "br"]
+ return ["gzip", "deflate", "br", "zstd"]
@command.command("options.load")
def options_load(self, path: mitmproxy.types.Path) -> None:
diff --git a/mitmproxy/net/http/encoding.py b/mitmproxy/net/http/encoding.py
index 8cb96e5c..16d399ca 100644
--- a/mitmproxy/net/http/encoding.py
+++ b/mitmproxy/net/http/encoding.py
@@ -9,6 +9,7 @@ from io import BytesIO
import gzip
import zlib
import brotli
+import zstandard as zstd
from typing import Union, Optional, AnyStr # noqa
@@ -52,7 +53,7 @@ def decode(
decoded = custom_decode[encoding](encoded)
except KeyError:
decoded = codecs.decode(encoded, encoding, errors)
- if encoding in ("gzip", "deflate", "br"):
+ if encoding in ("gzip", "deflate", "br", "zstd"):
_cache = CachedDecode(encoded, encoding, errors, decoded)
return decoded
except TypeError:
@@ -93,7 +94,7 @@ def encode(decoded: Optional[str], encoding: str, errors: str='strict') -> Optio
encoded = custom_encode[encoding](decoded)
except KeyError:
encoded = codecs.encode(decoded, encoding, errors)
- if encoding in ("gzip", "deflate", "br"):
+ if encoding in ("gzip", "deflate", "br", "zstd"):
_cache = CachedDecode(encoded, encoding, errors, decoded)
return encoded
except TypeError:
@@ -140,6 +141,23 @@ def encode_brotli(content: bytes) -> bytes:
return brotli.compress(content)
+def decode_zstd(content: bytes) -> bytes:
+ if not content:
+ return b""
+ zstd_ctx = zstd.ZstdDecompressor()
+ try:
+ return zstd_ctx.decompress(content)
+ except zstd.ZstdError:
+ # If the zstd stream is streamed without a size header,
+ # try decoding with a 10MiB output buffer
+ return zstd_ctx.decompress(content, max_output_size=10 * 2**20)
+
+
+def encode_zstd(content: bytes) -> bytes:
+ zstd_ctx = zstd.ZstdCompressor()
+ return zstd_ctx.compress(content)
+
+
def decode_deflate(content: bytes) -> bytes:
"""
Returns decompressed data for DEFLATE. Some servers may respond with
@@ -170,6 +188,7 @@ custom_decode = {
"gzip": decode_gzip,
"deflate": decode_deflate,
"br": decode_brotli,
+ "zstd": decode_zstd,
}
custom_encode = {
"none": identity,
@@ -177,6 +196,7 @@ custom_encode = {
"gzip": encode_gzip,
"deflate": encode_deflate,
"br": encode_brotli,
+ "zstd": encode_zstd,
}
__all__ = ["encode", "decode"]
diff --git a/mitmproxy/net/http/message.py b/mitmproxy/net/http/message.py
index 86782e8a..6830c6cd 100644
--- a/mitmproxy/net/http/message.py
+++ b/mitmproxy/net/http/message.py
@@ -236,7 +236,7 @@ class Message(serializable.Serializable):
def encode(self, e):
"""
- Encodes body with the encoding e, where e is "gzip", "deflate", "identity", or "br".
+ Encodes body with the encoding e, where e is "gzip", "deflate", "identity", "br", or "zstd".
Any existing content-encodings are overwritten,
the content is not decoded beforehand.
diff --git a/mitmproxy/net/http/request.py b/mitmproxy/net/http/request.py
index 959fdd33..3516e949 100644
--- a/mitmproxy/net/http/request.py
+++ b/mitmproxy/net/http/request.py
@@ -421,7 +421,7 @@ class Request(message.Message):
self.headers["accept-encoding"] = (
', '.join(
e
- for e in {"gzip", "identity", "deflate", "br"}
+ for e in {"gzip", "identity", "deflate", "br", "zstd"}
if e in accept_encoding
)
)