2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2012-02-07 03:39:37 +00:00
|
|
|
import urwid
|
2012-02-22 23:41:01 +00:00
|
|
|
import urwid.util
|
2015-01-15 15:37:26 +00:00
|
|
|
import os
|
2014-03-09 20:59:53 +00:00
|
|
|
from .. import utils
|
|
|
|
from ..protocol.http import CONTENT_MISSING
|
2015-01-15 12:36:59 +00:00
|
|
|
try:
|
|
|
|
import pyperclip
|
|
|
|
except:
|
|
|
|
pyperclip = False
|
2012-03-19 19:35:20 +00:00
|
|
|
|
2013-07-28 06:00:49 +00:00
|
|
|
VIEW_LIST = 0
|
|
|
|
VIEW_FLOW = 1
|
|
|
|
|
2012-03-19 19:35:20 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
VIEW_FLOW_REQUEST = 0
|
|
|
|
VIEW_FLOW_RESPONSE = 1
|
|
|
|
|
|
|
|
|
|
|
|
def highlight_key(s, k):
|
|
|
|
l = []
|
|
|
|
parts = s.split(k, 1)
|
|
|
|
if parts[0]:
|
|
|
|
l.append(("text", parts[0]))
|
|
|
|
l.append(("key", k))
|
|
|
|
if parts[1]:
|
|
|
|
l.append(("text", parts[1]))
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
2012-02-11 05:23:07 +00:00
|
|
|
KEY_MAX = 30
|
|
|
|
def format_keyvals(lst, key="key", val="text", indent=0):
|
2012-02-07 03:39:37 +00:00
|
|
|
"""
|
|
|
|
Format a list of (key, value) tuples.
|
|
|
|
|
|
|
|
If key is None, it's treated specially:
|
|
|
|
- We assume a sub-value, and add an extra indent.
|
|
|
|
- The value is treated as a pre-formatted list of directives.
|
|
|
|
"""
|
|
|
|
ret = []
|
|
|
|
if lst:
|
2012-02-11 05:23:07 +00:00
|
|
|
maxk = min(max(len(i[0]) for i in lst if i and i[0]), KEY_MAX)
|
2012-02-10 22:25:35 +00:00
|
|
|
for i, kv in enumerate(lst):
|
|
|
|
if kv is None:
|
2012-02-17 23:27:59 +00:00
|
|
|
ret.append(urwid.Text(""))
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2012-02-18 10:56:40 +00:00
|
|
|
cols = []
|
|
|
|
# This cumbersome construction process is here for a reason:
|
|
|
|
# Urwid < 1.0 barfs if given a fixed size column of size zero.
|
|
|
|
if indent:
|
|
|
|
cols.append(("fixed", indent, urwid.Text("")))
|
|
|
|
cols.extend([
|
|
|
|
(
|
|
|
|
"fixed",
|
|
|
|
maxk,
|
|
|
|
urwid.Text([(key, kv[0] or "")])
|
|
|
|
),
|
2012-04-02 23:10:25 +00:00
|
|
|
kv[1] if isinstance(kv[1], urwid.Widget) else urwid.Text([(val, kv[1])])
|
|
|
|
])
|
2012-02-18 10:56:40 +00:00
|
|
|
ret.append(urwid.Columns(cols, dividechars = 2))
|
2012-02-07 03:39:37 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def shortcuts(k):
|
|
|
|
if k == " ":
|
|
|
|
k = "page down"
|
|
|
|
elif k == "j":
|
|
|
|
k = "down"
|
|
|
|
elif k == "k":
|
|
|
|
k = "up"
|
|
|
|
return k
|
|
|
|
|
|
|
|
|
2012-02-17 22:11:59 +00:00
|
|
|
def fcol(s, attr):
|
2012-02-17 23:12:01 +00:00
|
|
|
s = unicode(s)
|
2012-02-17 22:11:59 +00:00
|
|
|
return (
|
|
|
|
"fixed",
|
|
|
|
len(s),
|
|
|
|
urwid.Text(
|
|
|
|
[
|
|
|
|
(attr, s)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2012-02-22 23:41:01 +00:00
|
|
|
if urwid.util.detected_encoding:
|
|
|
|
SYMBOL_REPLAY = u"\u21ba"
|
|
|
|
SYMBOL_RETURN = u"\u2190"
|
|
|
|
else:
|
|
|
|
SYMBOL_REPLAY = u"[r]"
|
|
|
|
SYMBOL_RETURN = u"<-"
|
2012-02-17 22:11:59 +00:00
|
|
|
|
2012-02-17 23:12:01 +00:00
|
|
|
|
2012-02-17 22:11:59 +00:00
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
def raw_format_flow(f, focus, extended, padding):
|
|
|
|
f = dict(f)
|
|
|
|
|
|
|
|
pile = []
|
2012-02-17 22:11:59 +00:00
|
|
|
req = []
|
2012-02-07 03:39:37 +00:00
|
|
|
if extended:
|
2012-02-17 22:11:59 +00:00
|
|
|
req.append(
|
|
|
|
fcol(
|
2012-02-26 21:00:44 +00:00
|
|
|
utils.format_timestamp(f["req_timestamp"]),
|
2012-02-17 22:11:59 +00:00
|
|
|
"highlight"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
req.append(fcol(">>" if focus else " ", "focus"))
|
2012-02-26 21:00:44 +00:00
|
|
|
if f["req_is_replay"]:
|
2012-02-22 23:41:01 +00:00
|
|
|
req.append(fcol(SYMBOL_REPLAY, "replay"))
|
2012-02-26 21:00:44 +00:00
|
|
|
req.append(fcol(f["req_method"], "method"))
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-17 22:11:59 +00:00
|
|
|
preamble = sum(i[1] for i in req) + len(req) -1
|
2012-02-17 23:12:01 +00:00
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
if f["intercepted"] and not f["acked"]:
|
2012-02-17 23:12:01 +00:00
|
|
|
uc = "intercept"
|
2012-02-26 21:00:44 +00:00
|
|
|
elif f["resp_code"] or f["err_msg"]:
|
2012-02-17 23:12:01 +00:00
|
|
|
uc = "text"
|
|
|
|
else:
|
|
|
|
uc = "title"
|
|
|
|
|
2012-02-17 22:11:59 +00:00
|
|
|
req.append(
|
2012-02-26 21:00:44 +00:00
|
|
|
urwid.Text([(uc, f["req_url"])])
|
2012-02-17 22:11:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
pile.append(urwid.Columns(req, dividechars=1))
|
|
|
|
|
|
|
|
resp = []
|
|
|
|
resp.append(
|
|
|
|
("fixed", preamble, urwid.Text(""))
|
|
|
|
)
|
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
if f["resp_code"]:
|
2012-04-02 02:08:30 +00:00
|
|
|
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))
|
2012-02-26 21:00:44 +00:00
|
|
|
if f["resp_is_replay"]:
|
2012-02-22 23:41:01 +00:00
|
|
|
resp.append(fcol(SYMBOL_REPLAY, "replay"))
|
2012-04-02 02:08:30 +00:00
|
|
|
resp.append(fcol(f["resp_code"], ccol))
|
2014-12-23 19:33:42 +00:00
|
|
|
if f["intercepted"] and f["resp_code"] and not f["acked"]:
|
2012-02-17 23:12:01 +00:00
|
|
|
rc = "intercept"
|
|
|
|
else:
|
|
|
|
rc = "text"
|
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
if f["resp_ctype"]:
|
|
|
|
resp.append(fcol(f["resp_ctype"], rc))
|
|
|
|
resp.append(fcol(f["resp_clen"], rc))
|
2013-08-22 22:01:19 +00:00
|
|
|
resp.append(fcol(f["resp_rate"], rc))
|
2013-08-13 19:02:49 +00:00
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
elif f["err_msg"]:
|
2012-02-24 01:01:17 +00:00
|
|
|
resp.append(fcol(SYMBOL_RETURN, "error"))
|
2012-02-17 22:11:59 +00:00
|
|
|
resp.append(
|
|
|
|
urwid.Text([
|
|
|
|
(
|
|
|
|
"error",
|
2012-02-26 21:00:44 +00:00
|
|
|
f["err_msg"]
|
2012-02-17 22:11:59 +00:00
|
|
|
)
|
|
|
|
])
|
2012-02-07 03:39:37 +00:00
|
|
|
)
|
2012-02-17 22:11:59 +00:00
|
|
|
pile.append(urwid.Columns(resp, dividechars=1))
|
|
|
|
return urwid.Pile(pile)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-01-15 15:37:26 +00:00
|
|
|
## common save body parts
|
2015-01-15 15:57:00 +00:00
|
|
|
def save_body(path, master, state, content):
|
2015-01-15 15:37:26 +00:00
|
|
|
if not path:
|
|
|
|
return
|
|
|
|
state.last_saveload = path
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
try:
|
|
|
|
f = file(path, "wb")
|
|
|
|
f.write(str(content))
|
|
|
|
f.close()
|
|
|
|
except IOError, v:
|
|
|
|
master.statusbar.message(v.strerror)
|
|
|
|
|
2015-01-15 15:57:00 +00:00
|
|
|
def ask_save_body(k, master, state, content):
|
2015-01-15 15:37:26 +00:00
|
|
|
if k == "y":
|
|
|
|
master.path_prompt(
|
|
|
|
"Save response body: ",
|
|
|
|
state.last_saveload,
|
2015-01-15 15:57:00 +00:00
|
|
|
save_body,
|
2015-01-15 15:37:26 +00:00
|
|
|
master,
|
|
|
|
state,
|
|
|
|
content,
|
|
|
|
)
|
|
|
|
|
|
|
|
## common server_copy_response parts
|
|
|
|
def server_copy_response( k, master, state, response):
|
2015-01-15 12:36:59 +00:00
|
|
|
if pyperclip:
|
|
|
|
if k == "c":
|
|
|
|
try:
|
|
|
|
pyperclip.copy(response.get_decoded_content())
|
|
|
|
except TypeError:
|
2015-01-15 15:37:26 +00:00
|
|
|
master.prompt_onekey(
|
|
|
|
"Content is binary do you want to save it to a file instead?",
|
|
|
|
(
|
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
2015-01-15 15:57:00 +00:00
|
|
|
ask_save_body,
|
2015-01-15 15:37:26 +00:00
|
|
|
master,
|
|
|
|
state,
|
|
|
|
response.get_decoded_content(),
|
|
|
|
)
|
2015-01-15 12:36:59 +00:00
|
|
|
elif k == "h":
|
|
|
|
try:
|
|
|
|
pyperclip.copy(str(response.headers))
|
|
|
|
except TypeError:
|
2015-01-15 15:37:26 +00:00
|
|
|
master.statusbar.message("Error converting headers to text")
|
2015-01-15 12:36:59 +00:00
|
|
|
else:
|
2015-01-15 15:37:26 +00:00
|
|
|
master.statusbar.message("No clipboard support on your system, sorry.")
|
2015-01-15 12:36:59 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
class FlowCache:
|
|
|
|
@utils.LRUCache(200)
|
|
|
|
def format_flow(self, *args):
|
|
|
|
return raw_format_flow(*args)
|
|
|
|
flowcache = FlowCache()
|
|
|
|
|
|
|
|
|
2013-03-17 04:31:35 +00:00
|
|
|
def format_flow(f, focus, extended=False, hostheader=False, padding=2):
|
2012-02-26 21:00:44 +00:00
|
|
|
d = dict(
|
2014-12-23 19:33:42 +00:00
|
|
|
intercepted = f.intercepted,
|
2014-09-03 22:10:01 +00:00
|
|
|
acked = f.reply.acked,
|
2012-02-26 21:00:44 +00:00
|
|
|
|
2013-01-19 04:04:11 +00:00
|
|
|
req_timestamp = f.request.timestamp_start,
|
2014-02-05 13:33:17 +00:00
|
|
|
req_is_replay = f.request.is_replay,
|
2012-02-26 21:00:44 +00:00
|
|
|
req_method = f.request.method,
|
2014-09-03 21:44:54 +00:00
|
|
|
req_url = f.request.pretty_url(hostheader=hostheader),
|
2012-02-26 21:00:44 +00:00
|
|
|
|
|
|
|
err_msg = f.error.msg if f.error else None,
|
|
|
|
resp_code = f.response.code if f.response else None,
|
|
|
|
)
|
|
|
|
if f.response:
|
2012-05-16 06:16:44 +00:00
|
|
|
if f.response.content:
|
|
|
|
contentdesc = utils.pretty_size(len(f.response.content))
|
2014-03-09 20:59:53 +00:00
|
|
|
elif f.response.content == CONTENT_MISSING:
|
2012-05-16 06:16:44 +00:00
|
|
|
contentdesc = "[content missing]"
|
|
|
|
else:
|
|
|
|
contentdesc = "[no content]"
|
2013-08-13 19:02:49 +00:00
|
|
|
|
2014-09-22 15:01:06 +00:00
|
|
|
if f.response.timestamp_end:
|
|
|
|
delta = f.response.timestamp_end - f.response.timestamp_start
|
|
|
|
else:
|
|
|
|
delta = 0
|
2014-01-30 04:00:13 +00:00
|
|
|
size = f.response.size()
|
2013-11-26 22:14:51 +00:00
|
|
|
rate = utils.pretty_size(size / ( delta if delta > 0 else 1 ) )
|
2013-08-13 19:02:49 +00:00
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
d.update(dict(
|
|
|
|
resp_code = f.response.code,
|
2014-02-05 13:33:17 +00:00
|
|
|
resp_is_replay = f.response.is_replay,
|
2013-08-13 19:02:49 +00:00
|
|
|
resp_clen = contentdesc,
|
2013-08-13 23:29:57 +00:00
|
|
|
resp_rate = "{0}/s".format(rate),
|
2012-02-26 21:00:44 +00:00
|
|
|
))
|
|
|
|
t = f.response.headers["content-type"]
|
|
|
|
if t:
|
|
|
|
d["resp_ctype"] = t[0].split(";")[0]
|
|
|
|
else:
|
|
|
|
d["resp_ctype"] = ""
|
|
|
|
return flowcache.format_flow(tuple(sorted(d.items())), focus, extended, padding)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def int_version(v):
|
|
|
|
SIG = 3
|
|
|
|
v = urwid.__version__.split("-")[0].split(".")
|
|
|
|
x = 0
|
|
|
|
for i in range(min(SIG, len(v))):
|
|
|
|
x += int(v[i]) * 10**(SIG-i)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
# We have to do this to be portable over 0.9.8 and 0.9.9 If compatibility
|
|
|
|
# becomes a pain to maintain, we'll just mandate 0.9.9 or newer.
|
|
|
|
class WWrap(urwid.WidgetWrap):
|
|
|
|
if int_version(urwid.__version__) >= 990:
|
|
|
|
def set_w(self, x):
|
|
|
|
self._w = x
|
|
|
|
def get_w(self):
|
|
|
|
return self._w
|
|
|
|
w = property(get_w, set_w)
|
|
|
|
|
|
|
|
|