Ejemplo n.º 1
0
    def _echo_request_line(self, flow):
        if flow.request.stickycookie:
            stickycookie = click.style("[stickycookie] ", fg="yellow", bold=True)
        else:
            stickycookie = ""

        if flow.client_conn:
            client = click.style(bytes_to_escaped_str(flow.client_conn.address.host), bold=True)
        else:
            client = click.style("[replay]", fg="yellow", bold=True)

        method = flow.request.method
        method_color = dict(
            GET="green",
            DELETE="red"
        ).get(method.upper(), "magenta")
        method = click.style(bytes_to_escaped_str(method), fg=method_color, bold=True)
        if self.showhost:
            url = flow.request.pretty_url
        else:
            url = flow.request.url
        url = click.style(bytes_to_escaped_str(url), bold=True)

        httpversion = ""
        if flow.request.http_version not in ("HTTP/1.1", "HTTP/1.0"):
            httpversion = " " + flow.request.http_version  # We hide "normal" HTTP 1.

        line = "{stickycookie}{client} {method} {url}{httpversion}".format(
            stickycookie=stickycookie,
            client=client,
            method=method,
            url=url,
            httpversion=httpversion
        )
        self.echo(line)
Ejemplo n.º 2
0
    def _echo_message(self, message):
        if self.o.flow_detail >= 2:
            headers = "\r\n".join(
                "{}: {}".format(
                    click.style(bytes_to_escaped_str(k), fg="blue", bold=True),
                    click.style(bytes_to_escaped_str(v), fg="blue"))
                for k, v in message.headers.fields
            )
            self.echo(headers, indent=4)
        if self.o.flow_detail >= 3:
            if message.content is None:
                self.echo("(content missing)", indent=4)
            elif message.content:
                self.echo("")

                try:
                    type, lines = contentviews.get_content_view(
                        contentviews.get("Auto"),
                        message.content,
                        headers=message.headers
                    )
                except ContentViewException:
                    s = "Content viewer failed: \n" + traceback.format_exc()
                    self.add_event(s, "debug")
                    type, lines = contentviews.get_content_view(
                        contentviews.get("Raw"),
                        message.content,
                        headers=message.headers
                    )

                styles = dict(
                    highlight=dict(bold=True),
                    offset=dict(fg="blue"),
                    header=dict(fg="green", bold=True),
                    text=dict(fg="green")
                )

                def colorful(line):
                    yield u"    "  # we can already indent here
                    for (style, text) in line:
                        yield click.style(text, **styles.get(style, {}))

                if self.o.flow_detail == 3:
                    lines_to_echo = itertools.islice(lines, 70)
                else:
                    lines_to_echo = lines

                lines_to_echo = list(lines_to_echo)

                content = u"\r\n".join(
                    u"".join(colorful(line)) for line in lines_to_echo
                )

                self.echo(content)
                if next(lines, None):
                    self.echo("(cut off)", indent=4, dim=True)

        if self.o.flow_detail >= 2:
            self.echo("")
Ejemplo n.º 3
0
    def _echo_message(self, message):
        if self.o.flow_detail >= 2:
            headers = "\r\n".join("{}: {}".format(
                click.style(bytes_to_escaped_str(k), fg="blue", bold=True),
                click.style(bytes_to_escaped_str(v), fg="blue"))
                                  for k, v in message.headers.fields)
            self.echo(headers, indent=4)
        if self.o.flow_detail >= 3:
            if message.content is None:
                self.echo("(content missing)", indent=4)
            elif message.content:
                self.echo("")

                try:
                    type, lines = contentviews.get_content_view(
                        contentviews.get("Auto"),
                        message.content,
                        headers=message.headers)
                except ContentViewException:
                    s = "Content viewer failed: \n" + traceback.format_exc()
                    self.add_event(s, "debug")
                    type, lines = contentviews.get_content_view(
                        contentviews.get("Raw"),
                        message.content,
                        headers=message.headers)

                styles = dict(highlight=dict(bold=True),
                              offset=dict(fg="blue"),
                              header=dict(fg="green", bold=True),
                              text=dict(fg="green"))

                def colorful(line):
                    yield u"    "  # we can already indent here
                    for (style, text) in line:
                        yield click.style(text, **styles.get(style, {}))

                if self.o.flow_detail == 3:
                    lines_to_echo = itertools.islice(lines, 70)
                else:
                    lines_to_echo = lines

                lines_to_echo = list(lines_to_echo)

                content = u"\r\n".join(u"".join(colorful(line))
                                       for line in lines_to_echo)

                self.echo(content)
                if next(lines, None):
                    self.echo("(cut off)", indent=4, dim=True)

        if self.o.flow_detail >= 2:
            self.echo("")
Ejemplo n.º 4
0
    def _echo_response_line(self, flow):
        if flow.response.is_replay:
            replay = click.style("[replay] ", fg="yellow", bold=True)
        else:
            replay = ""

        code = flow.response.status_code
        code_color = None
        if 200 <= code < 300:
            code_color = "green"
        elif 300 <= code < 400:
            code_color = "magenta"
        elif 400 <= code < 600:
            code_color = "red"
        code = click.style(str(code), fg=code_color, bold=True, blink=(code == 418))
        reason = click.style(bytes_to_escaped_str(flow.response.reason), fg=code_color, bold=True)

        if flow.response.content is None:
            size = "(content missing)"
        else:
            size = pretty_size(len(flow.response.content))
        size = click.style(size, bold=True)

        arrows = click.style("<<", bold=True)

        line = "{replay} {arrows} {code} {reason} {size}".format(
            replay=replay,
            arrows=arrows,
            code=code,
            reason=reason,
            size=size
        )
        self.echo(line)
Ejemplo n.º 5
0
def escape_unprintables(s):
    """
        Like inner_repr, but preserves line breaks.
    """
    s = s.replace(b"\r\n", b"PATHOD_MARKER_RN")
    s = s.replace(b"\n", b"PATHOD_MARKER_N")
    s = bytes_to_escaped_str(s)
    s = s.replace("PATHOD_MARKER_RN", "\n")
    s = s.replace("PATHOD_MARKER_N", "\n")
    return s
Ejemplo n.º 6
0
def escape_unprintables(s):
    """
        Like inner_repr, but preserves line breaks.
    """
    s = s.replace(b"\r\n", b"PATHOD_MARKER_RN")
    s = s.replace(b"\n", b"PATHOD_MARKER_N")
    s = bytes_to_escaped_str(s)
    s = s.replace("PATHOD_MARKER_RN", "\n")
    s = s.replace("PATHOD_MARKER_N", "\n")
    return s
Ejemplo n.º 7
0
def test_bytes_to_escaped_str():
    assert utils.bytes_to_escaped_str(b"foo") == "foo"
    assert utils.bytes_to_escaped_str(b"\b") == r"\x08"
    assert utils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)"
    assert utils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc"
    assert utils.bytes_to_escaped_str(b"'") == r"\'"
    assert utils.bytes_to_escaped_str(b'"') == r'"'
Ejemplo n.º 8
0
def test_bytes_to_escaped_str():
    assert utils.bytes_to_escaped_str(b"foo") == "foo"
    assert utils.bytes_to_escaped_str(b"\b") == r"\x08"
    assert utils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)"
    assert utils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc"
    assert utils.bytes_to_escaped_str(b"'") == r"\'"
    assert utils.bytes_to_escaped_str(b'"') == r'"'
Ejemplo n.º 9
0
    def _echo_request_line(self, flow):
        if flow.request.stickycookie:
            stickycookie = click.style("[stickycookie] ",
                                       fg="yellow",
                                       bold=True)
        else:
            stickycookie = ""

        if flow.client_conn:
            client = click.style(bytes_to_escaped_str(
                flow.client_conn.address.host),
                                 bold=True)
        else:
            client = click.style("[replay]", fg="yellow", bold=True)

        method = flow.request.method
        method_color = dict(GET="green",
                            DELETE="red").get(method.upper(), "magenta")
        method = click.style(bytes_to_escaped_str(method),
                             fg=method_color,
                             bold=True)
        if self.showhost:
            url = flow.request.pretty_url
        else:
            url = flow.request.url
        url = click.style(bytes_to_escaped_str(url), bold=True)

        httpversion = ""
        if flow.request.http_version not in ("HTTP/1.1", "HTTP/1.0"):
            httpversion = " " + flow.request.http_version  # We hide "normal" HTTP 1.

        line = "{stickycookie}{client} {method} {url}{httpversion}".format(
            stickycookie=stickycookie,
            client=client,
            method=method,
            url=url,
            httpversion=httpversion)
        self.echo(line)
Ejemplo n.º 10
0
    def _echo_response_line(self, flow):
        if flow.response.is_replay:
            replay = click.style("[replay] ", fg="yellow", bold=True)
        else:
            replay = ""

        code = flow.response.status_code
        code_color = None
        if 200 <= code < 300:
            code_color = "green"
        elif 300 <= code < 400:
            code_color = "magenta"
        elif 400 <= code < 600:
            code_color = "red"
        code = click.style(str(code),
                           fg=code_color,
                           bold=True,
                           blink=(code == 418))
        reason = click.style(bytes_to_escaped_str(flow.response.reason),
                             fg=code_color,
                             bold=True)

        if flow.response.content is None:
            size = "(content missing)"
        else:
            size = pretty_size(len(flow.response.content))
        size = click.style(size, bold=True)

        arrows = click.style("<<", bold=True)

        line = "{replay} {arrows} {code} {reason} {size}".format(replay=replay,
                                                                 arrows=arrows,
                                                                 code=code,
                                                                 reason=reason,
                                                                 size=size)
        self.echo(line)
Ejemplo n.º 11
0
 def freeze(self, settings):
     f = self.parsed.freeze(settings).spec()
     return self.__class__(TokValueLiteral(bytes_to_escaped_str(f)))
Ejemplo n.º 12
0
 def spec(self):
     return "<'%s'" % bytes_to_escaped_str(self.path)
Ejemplo n.º 13
0
 def freeze(self, settings):
     g = self.get_generator(settings)
     return TokValueLiteral(bytes_to_escaped_str(g[:]))
Ejemplo n.º 14
0
 def spec(self):
     return bytes_to_escaped_str(self.val)
Ejemplo n.º 15
0
 def spec(self):
     inner = bytes_to_escaped_str(self.val)
     inner = inner.replace(r"\'", r"\x27")
     return "'" + inner + "'"