diff options
author | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-19 12:37:50 +0100 |
---|---|---|
committer | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-19 14:29:09 +0100 |
commit | 36352c95397351f4e69d857fcd8110fe125388f7 (patch) | |
tree | b12a7a193d27262eb921e161a849dbbf3e974932 /mitmproxy/contentviews | |
parent | 3ee5227cca9e67df50310447efb6bc7c557d34bb (diff) | |
download | mitmproxy-36352c95397351f4e69d857fcd8110fe125388f7.tar.gz mitmproxy-36352c95397351f4e69d857fcd8110fe125388f7.tar.bz2 mitmproxy-36352c95397351f4e69d857fcd8110fe125388f7.zip |
protobuf: coverage++
Diffstat (limited to 'mitmproxy/contentviews')
-rw-r--r-- | mitmproxy/contentviews/__init__.py | 5 | ||||
-rw-r--r-- | mitmproxy/contentviews/protobuf.py | 21 |
2 files changed, 11 insertions, 15 deletions
diff --git a/mitmproxy/contentviews/__init__.py b/mitmproxy/contentviews/__init__.py index 357172e3..c7db6690 100644 --- a/mitmproxy/contentviews/__init__.py +++ b/mitmproxy/contentviews/__init__.py @@ -159,6 +159,7 @@ def get_content_view(viewmode: View, data: bytes, **metadata): return desc, safe_to_print(content), error +# The order in which ContentViews are added is important! add(auto.ViewAuto()) add(raw.ViewRaw()) add(hex.ViewHex()) @@ -172,9 +173,7 @@ add(urlencoded.ViewURLEncoded()) add(multipart.ViewMultipart()) add(image.ViewImage()) add(query.ViewQuery()) - -if protobuf.ViewProtobuf.is_available(): - add(protobuf.ViewProtobuf()) +add(protobuf.ViewProtobuf()) __all__ = [ "View", "VIEW_CUTOFF", "KEY_MAX", "format_text", "format_dict", diff --git a/mitmproxy/contentviews/protobuf.py b/mitmproxy/contentviews/protobuf.py index 620d9444..4bbb1580 100644 --- a/mitmproxy/contentviews/protobuf.py +++ b/mitmproxy/contentviews/protobuf.py @@ -15,31 +15,28 @@ class ViewProtobuf(base.View): "application/x-protobuffer", ] - @staticmethod - def is_available(): + def is_available(self): try: p = subprocess.Popen( ["protoc", "--version"], stdout=subprocess.PIPE ) out, _ = p.communicate() - return out.startswith("libprotoc") + return out.startswith(b"libprotoc") except: return False - def decode_protobuf(self, content): + def __call__(self, data, **metadata): + if not self.is_available(): + raise NotImplementedError("protoc not found. Please make sure 'protoc' is available in $PATH.") + # if Popen raises OSError, it will be caught in # get_content_view and fall back to Raw p = subprocess.Popen(['protoc', '--decode_raw'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = p.communicate(input=content) - if out: - return out - else: - return err - - def __call__(self, data, **metadata): - decoded = self.decode_protobuf(data) + decoded, _ = p.communicate(input=data) + if not decoded: + raise ValueError("Failed to parse input.") return "Protobuf", base.format_text(decoded) |