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/protobuf.py | |
parent | 3ee5227cca9e67df50310447efb6bc7c557d34bb (diff) | |
download | mitmproxy-36352c95397351f4e69d857fcd8110fe125388f7.tar.gz mitmproxy-36352c95397351f4e69d857fcd8110fe125388f7.tar.bz2 mitmproxy-36352c95397351f4e69d857fcd8110fe125388f7.zip |
protobuf: coverage++
Diffstat (limited to 'mitmproxy/contentviews/protobuf.py')
-rw-r--r-- | mitmproxy/contentviews/protobuf.py | 21 |
1 files changed, 9 insertions, 12 deletions
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) |