Esempio n. 1
0
def get_content_view(viewmode, data, **metadata):
    """
        Args:
            viewmode: the view to use.
            data, **metadata: arguments passed to View instance.

        Returns:
            A (description, content generator) tuple.
            In contrast to calling the views directly, text is always safe-to-print unicode.

        Raises:
            ContentViewException, if the content view threw an error.
    """
    msg = []

    headers = metadata.get("headers", {})
    enc = headers.get("content-encoding")
    if enc and enc != "identity":
        decoded = encoding.decode(enc, data)
        if decoded:
            data = decoded
            msg.append("[decoded %s]" % enc)
    try:
        ret = viewmode(data, **metadata)
    # Third-party viewers can fail in unexpected ways...
    except Exception as e:
        six.reraise(exceptions.ContentViewException,
                    exceptions.ContentViewException(str(e)),
                    sys.exc_info()[2])
    if not ret:
        ret = get("Raw")(data, **metadata)
        msg.append("Couldn't parse: falling back to Raw")
    else:
        msg.append(ret[0])
    return " ".join(msg), safe_to_print(ret[1])
Esempio n. 2
0
def get_content_view(viewmode, data, **metadata):
    """
        Args:
            viewmode: the view to use.
            data, **metadata: arguments passed to View instance.

        Returns:
            A (description, content generator) tuple.
            In contrast to calling the views directly, text is always safe-to-print unicode.

        Raises:
            ContentViewException, if the content view threw an error.
    """
    try:
        ret = viewmode(data, **metadata)
    # Third-party viewers can fail in unexpected ways...
    except Exception as e:
        six.reraise(
            exceptions.ContentViewException,
            exceptions.ContentViewException(str(e)),
            sys.exc_info()[2]
        )
    if not ret:
        desc = "Couldn't parse: falling back to Raw"
        _, content = get("Raw")(data, **metadata)
    else:
        desc, content = ret
    return desc, safe_to_print(content)
Esempio n. 3
0
 def test_contentview(self, view_auto):
     view_auto.side_effect = exceptions.ContentViewException("")
     sio = io.StringIO()
     d = dumper.Dumper(sio)
     with taddons.context(options=dump.Options()) as ctx:
         ctx.configure(d, flow_detail=4, verbosity=3)
         d.response(tflow.tflow())
         assert "Content viewer failed" in ctx.master.event_log[0][1]
Esempio n. 4
0
 def test_contentview(self, view_auto):
     view_auto.side_effect = exceptions.ContentViewException("")
     sio = io.StringIO()
     d = dumper.Dumper(sio)
     with taddons.context(d) as ctx:
         ctx.configure(d, flow_detail=4, verbosity='debug')
         d.response(tflow.tflow())
         assert ctx.master.has_log("content viewer failed")
Esempio n. 5
0
def add(view: View) -> None:
    # TODO: auto-select a different name (append an integer?)
    for i in views:
        if i.name == view.name:
            raise exceptions.ContentViewException("Duplicate view: " + view.name)

    # TODO: the UI should auto-prompt for a replacement shortcut
    for prompt in view_prompts:
        if prompt[1] == view.prompt[1]:
            raise exceptions.ContentViewException("Duplicate view shortcut: " + view.prompt[1])

    views.append(view)

    for ct in view.content_types:
        l = content_types_map.setdefault(ct, [])
        l.append(view)

    view_prompts.append(view.prompt)
 async def test_contentview(self):
     with mock.patch("mitmproxy.contentviews.auto.ViewAuto.__call__") as va:
         va.side_effect = exceptions.ContentViewException("")
         sio = io.StringIO()
         d = dumper.Dumper(sio)
         with taddons.context(d) as ctx:
             ctx.configure(d, flow_detail=4)
             d.response(tflow.tflow())
             assert await ctx.master.await_log("content viewer failed")
Esempio n. 7
0
def add(view: View) -> None:
    # TODO: auto-select a different name (append an integer?)
    for i in views:
        if i.name == view.name:
            raise exceptions.ContentViewException("Duplicate view: " + view.name)

    views.append(view)

    for ct in view.content_types:
        l = content_types_map.setdefault(ct, [])
        l.append(view)
Esempio n. 8
0
    def test_contentview(self, view_auto):
        view_auto.side_effect = exceptions.ContentViewException("")

        sio = io.StringIO()
        o = dump.Options(
            flow_detail=4,
            verbosity=3,
            tfile=sio,
        )
        m = mastertest.RecordingMaster(o, proxy.DummyServer())
        d = dumper.Dumper()
        m.addons.add(d)
        m.response(tflow.tflow())
        assert "Content viewer failed" in m.event_log[0][1]
Esempio n. 9
0
    def test_contentview(self, view_auto):
        view_auto.side_effect = exceptions.ContentViewException("")

        s = state.State()
        sio = StringIO()
        o = dump.Options(
            flow_detail=4,
            verbosity=3,
            tfile=sio,
        )
        m = mastertest.RecordingMaster(o, None, s)
        d = dumper.Dumper()
        m.addons.add(o, d)
        self.invoke(m, "response", tutils.tflow())
        assert "Content viewer failed" in m.event_log[0][1]