How to use the mitmproxy.utils.strutils function in mitmproxy

To help you get started, we’ve selected a few mitmproxy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mitmproxy / mitmproxy / examples / custom_contentviews.py View on Github external
def __call__(self, data, **metadata):
        if strutils.is_xml(data):
            parser = lxml.etree.HTMLParser(
                strip_cdata=True,
                remove_blank_text=True
            )
            d = lxml.html.fromstring(data, parser=parser)
            docinfo = d.getroottree().docinfo

            def piglify(src):
                words = src.split()
                ret = ''
                for word in words:
                    idx = -1
                    while word[idx] in string.punctuation and (idx * -1) != len(word):
                        idx -= 1
                    if word[0].lower() in 'aeiou':
                        if idx == -1:
github mitmproxy / mitmproxy / pathod / log.py View on Github external
def dump(self, data, hexdump):
        if hexdump:
            for line in strutils.hexdump(data):
                self("\t%s %s %s" % line)
        else:
            data = strutils.always_str(
                strutils.escape_control_characters(
                    data
                        .decode("ascii", "replace")
                        .replace(u"\ufffd", u".")
                )
            )
            for i in data.split("\n"):
                self("\t%s" % i)
github mitmproxy / mitmproxy / mitmproxy / flowfilter.py View on Github external
def __init__(self, expr):
        self.expr = expr
        if self.is_binary:
            expr = strutils.escaped_str_to_bytes(expr)
        try:
            self.re = re.compile(expr, self.flags)
        except:
            raise ValueError("Cannot compile expression.")
github mitmproxy / mitmproxy / mitmproxy / io / compat.py View on Github external
def _convert_dict_keys(o: Any) -> Any:
    if isinstance(o, dict):
        return {strutils.always_str(k): _convert_dict_keys(v) for k, v in o.items()}
    else:
        return o
github mitmproxy / mitmproxy / mitmproxy / net / http / headers.py View on Github external
def _always_bytes(x):
    return strutils.always_bytes(x, "utf-8", "surrogateescape")
github mitmproxy / mitmproxy / mitmproxy / contentviews / auto.py View on Github external
def __call__(self, data, **metadata):
        headers = metadata.get("headers", {})
        ctype = headers.get("content-type")
        if data and ctype:
            ct = http.parse_content_type(ctype) if ctype else None
            ct = "%s/%s" % (ct[0], ct[1])
            if ct in contentviews.content_types_map:
                return contentviews.content_types_map[ct][0](data, **metadata)
            elif strutils.is_xml(data):
                return contentviews.get("XML/HTML")(data, **metadata)
            elif ct.startswith("image/"):
                return contentviews.get("Image")(data, **metadata)
        if metadata.get("query"):
            return contentviews.get("Query")(data, **metadata)
        if data and strutils.is_mostly_bin(data):
            return contentviews.get("Hex")(data)
        if not data:
            return "No content", []
        return contentviews.get("Raw")(data)
github mitmproxy / mitmproxy / mitmproxy / contentviews / css.py View on Github external
from mitmproxy.contentviews import base
from mitmproxy.utils import strutils

"""
A custom CSS prettifier. Compared to other prettifiers, its main features are:

- Implemented in pure Python.
- Modifies whitespace only.
- Works with any input.
- Considerably faster than e.g. cssutils.
"""

CSS_SPECIAL_AREAS = (
    "'" + strutils.SINGLELINE_CONTENT + strutils.NO_ESCAPE + "'",
    '"' + strutils.SINGLELINE_CONTENT + strutils.NO_ESCAPE + '"',
    r"/\*" + strutils.MULTILINE_CONTENT + r"\*/",
    "//" + strutils.SINGLELINE_CONTENT + "$"
)
CSS_SPECIAL_CHARS = "{};:"


def beautify(data: str, indent: str = "    "):
    """Beautify a string containing CSS code"""
    data = strutils.escape_special_areas(
        data.strip(),
        CSS_SPECIAL_AREAS,
        CSS_SPECIAL_CHARS,
    )

    # Add newlines
    data = re.sub(r"\s*;\s*", ";\n", data)
    data = re.sub(r"\s*{\s*", " {\n", data)
github mitmproxy / mitmproxy / mitmproxy / proxy / protocol / websocket.py View on Github external
def _handle_pong_received(self, event, source_conn, other_conn, is_server):
        self.log(
            "Pong Received from {}".format("server" if is_server else "client"),
            "info",
            [strutils.bytes_to_escaped_str(bytes(event.payload))]
        )
        return True
github mitmproxy / mitmproxy / mitmproxy / contentviews.py View on Github external
def __call__(self, data, **metadata):
        if strutils.is_xml(data):
            parser = lxml.etree.HTMLParser(
                strip_cdata=True,
                remove_blank_text=True
            )
            d = lxml.html.fromstring(data, parser=parser)
            docinfo = d.getroottree().docinfo
            s = lxml.etree.tostring(
                d,
                pretty_print=True,
                doctype=docinfo.doctype,
                encoding='utf8'
            )
            return "HTML", format_text(s)
github mitmproxy / mitmproxy / mitmproxy / addons / upstream_auth.py View on Github external
def parse_upstream_auth(auth):
    pattern = re.compile(".+:")
    if pattern.search(auth) is None:
        raise exceptions.OptionsError(
            "Invalid upstream auth specification: %s" % auth
        )
    return b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))