Add flow to ContentView metadata

This commit is contained in:
Yoav Shai 2019-12-06 00:13:20 +02:00
parent 056a9024ec
commit 4ffa1ed7b6
7 changed files with 20 additions and 16 deletions

View File

@ -86,10 +86,11 @@ class Dumper:
)
self.echo(out, ident=4)
def _echo_message(self, message):
def _echo_message(self, message, flow):
_, lines, error = contentviews.get_message_content_view(
ctx.options.dumper_default_contentview,
message
message,
flow
)
if error:
ctx.log.debug(error)
@ -218,14 +219,14 @@ class Dumper:
if ctx.options.flow_detail >= 2:
self._echo_headers(f.request.headers)
if ctx.options.flow_detail >= 3:
self._echo_message(f.request)
self._echo_message(f.request, f)
if f.response:
self._echo_response_line(f)
if ctx.options.flow_detail >= 2:
self._echo_headers(f.response.headers)
if ctx.options.flow_detail >= 3:
self._echo_message(f.response)
self._echo_message(f.response, f)
if f.error:
msg = strutils.escape_control_characters(f.error.msg)
@ -263,7 +264,7 @@ class Dumper:
if ctx.options.flow_detail >= 3:
message = message.from_state(message.get_state())
message.content = message.content.encode() if isinstance(message.content, str) else message.content
self._echo_message(message)
self._echo_message(message, f)
def websocket_end(self, f):
if self.match(f):
@ -291,4 +292,4 @@ class Dumper:
direction=direction,
))
if ctx.options.flow_detail >= 3:
self._echo_message(message)
self._echo_message(message, f)

View File

@ -75,7 +75,7 @@ def safe_to_print(lines, encoding="utf8"):
yield clean_line
def get_message_content_view(viewname, message):
def get_message_content_view(viewname, message, flow):
"""
Like get_content_view, but also handles message encoding.
"""
@ -104,6 +104,7 @@ def get_message_content_view(viewname, message):
if isinstance(message, http.Message):
metadata["headers"] = message.headers
metadata["message"] = message
metadata["flow"] = flow
description, lines, error = get_content_view(
viewmode, content, **metadata

View File

@ -120,7 +120,7 @@ class FlowDetails(tabs.Tabs):
message = self._get_content_view_message
self._get_content_view_message = None
description, lines, error = contentviews.get_message_content_view(
viewmode, message
viewmode, message, self.flow
)
if error:
self.master.log.debug(error)

View File

@ -396,7 +396,7 @@ class FlowContentView(RequestHandler):
message = getattr(self.flow, message)
description, lines, error = contentviews.get_message_content_view(
content_view.replace('_', ' '), message
content_view.replace('_', ' '), message, self.flow
)
# if error:
# add event log

View File

@ -65,7 +65,7 @@ def save_flows_content(path: pathlib.Path, flows: typing.Iterable[flow.Flow]) ->
t = time.time()
if message:
description, lines, error = contentviews.get_message_content_view(
'Auto', message
'Auto', message, f
)
else:
description, lines = 'No content.', []

View File

@ -123,7 +123,7 @@ def test_echo_body():
d = dumper.Dumper(sio, sio_err)
with taddons.context(d) as ctx:
ctx.configure(d, flow_detail=3)
d._echo_message(f.response)
d._echo_message(f.response, f)
t = sio.getvalue()
assert "cut off" in t

View File

@ -5,6 +5,7 @@ from mitmproxy import contentviews
from mitmproxy.exceptions import ContentViewException
from mitmproxy.net.http import Headers
from mitmproxy.test import tutils
from mitmproxy.test import tflow
class TestContentView(contentviews.View):
@ -60,21 +61,22 @@ def test_get_content_view():
def test_get_message_content_view():
f = tflow.tflow()
r = tutils.treq()
desc, lines, err = contentviews.get_message_content_view("raw", r)
desc, lines, err = contentviews.get_message_content_view("raw", r, f)
assert desc == "Raw"
desc, lines, err = contentviews.get_message_content_view("unknown", r)
desc, lines, err = contentviews.get_message_content_view("unknown", r, f)
assert desc == "Raw"
r.encode("gzip")
desc, lines, err = contentviews.get_message_content_view("raw", r)
desc, lines, err = contentviews.get_message_content_view("raw", r, f)
assert desc == "[decoded gzip] Raw"
r.headers["content-encoding"] = "deflate"
desc, lines, err = contentviews.get_message_content_view("raw", r)
desc, lines, err = contentviews.get_message_content_view("raw", r, f)
assert desc == "[cannot decode] Raw"
r.content = None
desc, lines, err = contentviews.get_message_content_view("raw", r)
desc, lines, err = contentviews.get_message_content_view("raw", r, f)
assert list(lines) == [[("error", "content missing")]]