Custom Contentviews
Contentviews pretty-print binary message data (e.g. HTTP response bodies) that would otherwise be hard to understand for humans. Some contentviews are also interactive, i.e. the pretty-printed representation can be edited and mitmproxy will re-encode it into a binary message.
Simple Example
All contentviews implement the Contentview base class:
from mitmproxy import contentviews
class SwapCase(contentviews.Contentview):
def prettify(self, data: bytes, metadata: contentviews.Metadata) -> str:
return data.swapcase().decode()
def render_priority(self, data: bytes, metadata: contentviews.Metadata) -> float:
if metadata.content_type and metadata.content_type.startswith("text/example"):
return 2 # return a value > 1 to make sure the custom view is automatically selected
else:
return 0
contentviews.add(SwapCase)
To use this contentview, load it as a regular addon:
mitmproxy -s examples/addons/contentview.py
Like all other mitmproxy addons, contentviews are hot-reloaded when their file contents change. mitmproxy (but not mitmweb) will automatically re-render the contentview as well.
For more details, see the mitmproxy.contentviews
API documentation.
Syntax Highlighting
Contentviews always return an unstyled str
, but they can declare that their output matches one of the
predefined SyntaxHighlight
formats. In particular, binary formats may prettify to YAML (or JSON) and
use the YAML highlighter.
The list of supported formats is currently limited, but the implementation is based on tree-sitter
and easy to extend (see the mitmproxy-highlight
crate).
Interactive Contentviews
The following example implements an interactive contentview that allows users to perform edits on the prettified representation:
from mitmproxy import contentviews
class InteractiveSwapCase(contentviews.InteractiveContentview):
def prettify(
self,
data: bytes,
metadata: contentviews.Metadata,
) -> str:
return data.swapcase().decode()
def reencode(
self,
prettified: str,
metadata: contentviews.Metadata,
) -> bytes:
return prettified.encode().swapcase()
contentviews.add(InteractiveSwapCase)