Skip to content

Commit

Permalink
Show reports on individual flows on the Details tab in mitmproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
vfaronov committed Mar 6, 2017
1 parent 28996de commit f35c5fc
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions mitmproxy_httpolice.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def response(self, flow):
exch.silence(self.silence)
httpolice.check_exchange(exch)
self.exchanges.append(exch)
attach_report(exch, flow)
log_exchange(exch, flow)

def done(self):
Expand Down Expand Up @@ -91,6 +92,22 @@ def extract_message_basics(msg):
return version, headers, body


def attach_report(exch, flow):
buf = io.BytesIO()
httpolice.text_report([exch], buf)
if buf.getvalue():
report = buf.getvalue().decode('utf-8')
# It would be nicer to split this into separate metadata entries
# for request and response, but since `flow.metadata` is a plain dict,
# their order is random under Python 3.5 and sometimes response comes
# after request. Also wrap in ``try...except`` because `flow.metadata`
# is not public API yet.
try:
flow.metadata['HTTPolice report'] = ReprString(report)
except Exception: # pragma: no cover
pass


def log_exchange(exch, flow):
severities = collections.Counter(notice.severity
for msg in [exch.request] + exch.responses
Expand Down Expand Up @@ -124,6 +141,15 @@ def ellipsize(s, max_length=40):
return s


class ReprString(str):

# Currently mitmproxy displays ``repr()`` in details view, not ``str()``.
# See also https://discourse.mitmproxy.org/t/extending-the-ui/359/5

def __repr__(self):
return str(self)


if __name__ == '__main__':
# Print the path to this script,
# for substitution into the mitmproxy command.
Expand Down

0 comments on commit f35c5fc

Please sign in to comment.