def write_raw(fp, lines, timestamp=True): if fp: if timestamp: fp.write(human.format_timestamp(time.time())) for i in lines: fp.write(i) fp.write("\n") fp.flush()
def format_left_indicators(*, focused: bool, intercepted: bool, timestamp: float): indicators: typing.List[typing.Union[str, typing.Tuple[str, str]]] = [] if focused: indicators.append(("focus", ">>")) else: indicators.append(" ") pretty_timestamp = human.format_timestamp(timestamp)[-8:] if intercepted: indicators.append(("intercept", pretty_timestamp)) else: indicators.append(("text", pretty_timestamp)) return "fixed", 10, urwid.Text(indicators)
def raw_format_flow(f, flow): f = dict(f) pile = [] req = [] if f["extended"]: req.append( fcol( human.format_timestamp(f["req_timestamp"]), "highlight" ) ) else: req.append(fcol(">>" if f["focus"] else " ", "focus")) if f["marked"]: req.append(fcol(SYMBOL_MARK, "mark")) if f["req_is_replay"]: req.append(fcol(SYMBOL_REPLAY, "replay")) pushed = ' PUSH_PROMISE' if 'h2-pushed-stream' in flow.metadata else '' req.append(fcol(f["req_method"] + pushed, "method")) preamble = sum(i[1] for i in req) + len(req) - 1 if f["intercepted"] and not f["acked"]: uc = "intercept" elif "resp_code" in f or "err_msg" in f: uc = "text" else: uc = "title" url = f["req_url"] if f["max_url_len"] and len(url) > f["max_url_len"]: url = url[:f["max_url_len"]] + "…" if f["req_http_version"] not in ("HTTP/1.0", "HTTP/1.1"): url += " " + f["req_http_version"] req.append( urwid.Text([(uc, url)]) ) pile.append(urwid.Columns(req, dividechars=1)) resp = [] resp.append( ("fixed", preamble, urwid.Text("")) ) if "resp_code" in f: codes = { 2: "code_200", 3: "code_300", 4: "code_400", 5: "code_500", } ccol = codes.get(f["resp_code"] // 100, "code_other") resp.append(fcol(SYMBOL_RETURN, ccol)) if f["resp_is_replay"]: resp.append(fcol(SYMBOL_REPLAY, "replay")) resp.append(fcol(f["resp_code"], ccol)) if f["extended"]: resp.append(fcol(f["resp_reason"], ccol)) if f["intercepted"] and f["resp_code"] and not f["acked"]: rc = "intercept" else: rc = "text" if f["resp_ctype"]: resp.append(fcol(f["resp_ctype"], rc)) resp.append(fcol(f["resp_clen"], rc)) resp.append(fcol(f["roundtrip"], rc)) elif f["err_msg"]: resp.append(fcol(SYMBOL_RETURN, "error")) resp.append( urwid.Text([ ( "error", f["err_msg"] ) ]) ) pile.append(urwid.Columns(resp, dividechars=1)) return urwid.Pile(pile)
def format_http_flow_list( *, render_mode: RenderMode, focused: bool, marked: bool, request_method: str, request_scheme: str, request_host: str, request_path: str, request_url: str, request_http_version: str, request_timestamp: float, request_is_push_promise: bool, request_is_replay: bool, intercepted: bool, response_code: typing.Optional[int], response_reason: typing.Optional[str], response_content_length: typing.Optional[int], response_content_type: typing.Optional[str], response_is_replay: bool, duration: typing.Optional[float], error_message: typing.Optional[str], ) -> urwid.Widget: req = [] if render_mode is RenderMode.DETAILVIEW: req.append(fcol(human.format_timestamp(request_timestamp), "highlight")) else: if focused: req.append(fcol(">>", "focus")) else: req.append(fcol(" ", "focus")) method_style = HTTP_REQUEST_METHOD_STYLES.get(request_method, "method_other") req.append(fcol(request_method, method_style)) if request_is_push_promise: req.append(fcol('PUSH_PROMISE', 'method_http2_push')) preamble_len = sum(x[1] for x in req) + len(req) - 1 if request_http_version not in ("HTTP/1.0", "HTTP/1.1"): request_url += " " + request_http_version if intercepted and not response_code: url_style = "intercept" elif response_code or error_message: url_style = "text" else: url_style = "title" if render_mode is RenderMode.DETAILVIEW: req.append(urwid.Text([(url_style, request_url)])) else: req.append(truncated_plain(request_url, url_style)) req.append( format_right_indicators(replay=request_is_replay or response_is_replay, marked=marked)) resp = [("fixed", preamble_len, urwid.Text(""))] if response_code: if intercepted: style = "intercept" else: style = "" status_style = style or HTTP_RESPONSE_CODE_STYLE.get( response_code // 100, "code_other") resp.append(fcol(SYMBOL_RETURN, status_style)) if response_is_replay: resp.append(fcol(SYMBOL_REPLAY, "replay")) resp.append(fcol(str(response_code), status_style)) if response_reason and render_mode is RenderMode.DETAILVIEW: resp.append(fcol(response_reason, status_style)) if response_content_type: ct, ct_style = format_http_content_type(response_content_type) resp.append(fcol(ct, style or ct_style)) if response_content_length: size, size_style = format_size(response_content_length) elif response_content_length == 0: size = "[no content]" size_style = "text" else: size = "[content missing]" size_style = "text" resp.append(fcol(size, style or size_style)) if duration: dur, dur_style = format_duration(duration) resp.append(fcol(dur, style or dur_style)) elif error_message: resp.append(fcol(SYMBOL_RETURN, "error")) resp.append(urwid.Text([("error", error_message)])) return urwid.Pile([ urwid.Columns(req, dividechars=1), urwid.Columns(resp, dividechars=1) ])
def raw_format_flow(f, flow): f = dict(f) pile = [] req = [] if f["extended"]: req.append( fcol(human.format_timestamp(f["req_timestamp"]), "highlight")) else: req.append(fcol(">>" if f["focus"] else " ", "focus")) if f["marked"]: req.append(fcol(SYMBOL_MARK, "mark")) if f["req_is_replay"]: req.append(fcol(SYMBOL_REPLAY, "replay")) pushed = ' PUSH_PROMISE' if 'h2-pushed-stream' in flow.metadata else '' req.append(fcol(f["req_method"] + pushed, "method")) preamble = sum(i[1] for i in req) + len(req) - 1 if f["intercepted"] and not f["acked"]: uc = "intercept" elif "resp_code" in f or "err_msg" in f: uc = "text" else: uc = "title" url = f["req_url"] if f["max_url_len"] and len(url) > f["max_url_len"]: url = url[:f["max_url_len"]] + "…" if f["req_http_version"] not in ("HTTP/1.0", "HTTP/1.1"): url += " " + f["req_http_version"] req.append(urwid.Text([(uc, url)])) pile.append(urwid.Columns(req, dividechars=1)) resp = [] resp.append(("fixed", preamble, urwid.Text(""))) if "resp_code" in f: codes = { 2: "code_200", 3: "code_300", 4: "code_400", 5: "code_500", } ccol = codes.get(f["resp_code"] // 100, "code_other") resp.append(fcol(SYMBOL_RETURN, ccol)) if f["resp_is_replay"]: resp.append(fcol(SYMBOL_REPLAY, "replay")) resp.append(fcol(f["resp_code"], ccol)) if f["extended"]: resp.append(fcol(f["resp_reason"], ccol)) if f["intercepted"] and f["resp_code"] and not f["acked"]: rc = "intercept" else: rc = "text" if f["resp_ctype"]: resp.append(fcol(f["resp_ctype"], rc)) resp.append(fcol(f["resp_clen"], rc)) resp.append(fcol(f["roundtrip"], rc)) elif f["err_msg"]: resp.append(fcol(SYMBOL_RETURN, "error")) resp.append(urwid.Text([("error", f["err_msg"])])) pile.append(urwid.Columns(resp, dividechars=1)) return urwid.Pile(pile)
def raw_format_table(f): f = dict(f) pile = [] req = [] cursor = [' ', 'focus'] if f['focus']: cursor[0] = '>' req.append(fcol(*cursor)) if f.get('resp_is_replay', False) or f.get('req_is_replay', False): req.append(fcol(SYMBOL_REPLAY, 'replay')) if f['marked']: req.append(fcol(SYMBOL_MARK, 'mark')) if f["two_line"]: req.append( TruncatedText(f["req_url"], colorize_url(f["req_url"]), 'left')) pile.append(urwid.Columns(req, dividechars=1)) req = [] req.append(fcol(' ', 'text')) if f["intercepted"] and not f["acked"]: uc = "intercept" elif "resp_code" in f or f["err_msg"] is not None: uc = "highlight" else: uc = "title" if f["extended"]: s = human.format_timestamp(f["req_timestamp"]) else: s = datetime.datetime.fromtimestamp( time.mktime(time.localtime( f["req_timestamp"]))).strftime("%H:%M:%S") req.append(fcol(s, uc)) methods = { 'GET': 'method_get', 'POST': 'method_post', 'DELETE': 'method_delete', 'HEAD': 'method_head', 'PUT': 'method_put' } uc = methods.get(f["req_method"], "method_other") if f['extended']: req.append(fcol(f["req_method"], uc)) if f["req_promise"]: req.append(fcol('PUSH_PROMISE', 'method_http2_push')) else: if f["req_promise"]: uc = 'method_http2_push' req.append(("fixed", 4, truncated_plain(f["req_method"], uc))) if f["two_line"]: req.append(fcol(f["req_http_version"], 'text')) else: schemes = { 'http': 'scheme_http', 'https': 'scheme_https', } req.append( fcol(fixlen(f["req_scheme"].upper(), 5), schemes.get(f["req_scheme"], "scheme_other"))) req.append(('weight', 0.25, TruncatedText(f["req_host"], colorize_host(f["req_host"]), 'right'))) req.append(('weight', 1.0, TruncatedText(f["req_path"], colorize_req(f["req_path"]), 'left'))) ret = (' ' * len(SYMBOL_RETURN), 'text') status = ('', 'text') content = ('', 'text') size = ('', 'text') duration = ('', 'text') if "resp_code" in f: codes = { 2: "code_200", 3: "code_300", 4: "code_400", 5: "code_500", } ccol = codes.get(f["resp_code"] // 100, "code_other") ret = (SYMBOL_RETURN, ccol) status = (str(f["resp_code"]), ccol) if f["resp_len"] < 0: if f["intercepted"] and f["resp_code"] and not f["acked"]: rc = "intercept" else: rc = "content_none" if f["resp_len"] == -1: contentdesc = "[content missing]" else: contentdesc = "[no content]" content = (contentdesc, rc) else: if f["resp_ctype"]: ctype = f["resp_ctype"].split(";")[0] if ctype.endswith('/javascript'): rc = 'content_script' elif ctype.startswith('text/'): rc = 'content_text' elif (ctype.startswith('image/') or ctype.startswith('video/') or ctype.startswith('font/') or "/x-font-" in ctype): rc = 'content_media' elif ctype.endswith('/json') or ctype.endswith('/xml'): rc = 'content_data' elif ctype.startswith('application/'): rc = 'content_raw' else: rc = 'content_other' content = (ctype, rc) rc = 'gradient_%02d' % int( 99 - 100 * min(math.log2(1 + f["resp_len"]) / 20, 0.99)) size_str = human.pretty_size(f["resp_len"]) if not f['extended']: # shorten to 5 chars max if len(size_str) > 5: size_str = size_str[0:4].rstrip('.') + size_str[-1:] size = (size_str, rc) if f['duration'] is not None: rc = 'gradient_%02d' % int( 99 - 100 * min(math.log2(1 + 1000 * f['duration']) / 12, 0.99)) duration = (human.pretty_duration(f['duration']), rc) elif f["err_msg"]: status = ('Err', 'error') content = f["err_msg"], 'error' if f["two_line"]: req.append(fcol(*ret)) req.append(fcol(fixlen(status[0], 3), status[1])) req.append(('weight', 0.15, truncated_plain(content[0], content[1], 'right'))) if f['extended']: req.append(fcol(*size)) else: req.append(fcol(fixlen_r(size[0], 5), size[1])) req.append(fcol(fixlen_r(duration[0], 5), duration[1])) pile.append(urwid.Columns(req, dividechars=1, min_width=15)) return urwid.Pile(pile)
def test_format_timestamp(): assert human.format_timestamp(time.time())