mitmdump: improve request printing

This commit is contained in:
Maximilian Hils 2015-03-15 03:08:45 +01:00
parent 5b4d54c829
commit 7b58fe5142
1 changed files with 14 additions and 9 deletions

View File

@ -1,4 +1,5 @@
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import json
import sys import sys
import os import os
import netlib.utils import netlib.utils
@ -159,26 +160,33 @@ class DumpMaster(flow.FlowMaster):
print(e, file=self.outfile) print(e, file=self.outfile)
self.outfile.flush() self.outfile.flush()
def indent(self, n, t): @staticmethod
l = str(t).strip().split("\n") def indent(n, t):
return "\n".join(" "*n + i for i in l) l = str(t).strip().splitlines()
pad = " " * n
return "\n".join(pad + i for i in l)
def _print_message(self, message): def _print_message(self, message):
if self.o.flow_detail >= 2: if self.o.flow_detail >= 2:
print(self.indent(4, message.headers), file=self.outfile) print(self.indent(4, message.headers), file=self.outfile)
if self.o.flow_detail >= 3: if self.o.flow_detail >= 3:
if message.content == http.CONTENT_MISSING: if message.content == http.CONTENT_MISSING:
print("", file=self.outfile)
print(self.indent(4, "(content missing)"), file=self.outfile) print(self.indent(4, "(content missing)"), file=self.outfile)
elif message.content: elif message.content:
print("", file=self.outfile) print("", file=self.outfile)
content = message.get_decoded_content() content = message.get_decoded_content()
if not utils.isBin(content): if not utils.isBin(content):
try:
jsn = json.loads(content)
print(self.indent(4, json.dumps(jsn, indent=2)), file=self.outfile)
except ValueError:
print(self.indent(4, content), file=self.outfile) print(self.indent(4, content), file=self.outfile)
else: else:
d = netlib.utils.hexdump(content) d = netlib.utils.hexdump(content)
d = "\n".join("%s\t%s %s"%i for i in d) d = "\n".join("%s\t%s %s"%i for i in d)
print(self.indent(4, d), file=self.outfile) print(self.indent(4, d), file=self.outfile)
if self.o.flow_detail >= 2:
print("", file=self.outfile)
def _process_flow(self, f): def _process_flow(self, f):
self.state.delete_flow(f) self.state.delete_flow(f)
@ -192,9 +200,6 @@ class DumpMaster(flow.FlowMaster):
print(str_request(f, self.showhost), file=self.outfile) print(str_request(f, self.showhost), file=self.outfile)
self._print_message(f.request) self._print_message(f.request)
if self.o.flow_detail >= 2:
print("", file=self.outfile)
if f.response: if f.response:
if f.response.content == http.CONTENT_MISSING: if f.response.content == http.CONTENT_MISSING:
sz = "(content missing)" sz = "(content missing)"