2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2015-03-13 12:14:37 +00:00
|
|
|
|
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
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2015-09-16 16:45:22 +00:00
|
|
|
from netlib.http import CONTENT_MISSING
|
2015-07-29 09:39:53 +00:00
|
|
|
import netlib.utils
|
|
|
|
|
2014-03-09 20:59:53 +00:00
|
|
|
from .. import utils
|
2015-08-30 13:27:29 +00:00
|
|
|
from ..models import decoded
|
2015-03-20 22:19:20 +00:00
|
|
|
from . import signals
|
2015-07-29 09:39:53 +00:00
|
|
|
|
2015-02-06 23:33:29 +00:00
|
|
|
|
2015-01-15 12:36:59 +00:00
|
|
|
try:
|
|
|
|
import pyperclip
|
|
|
|
except:
|
|
|
|
pyperclip = False
|
2012-03-19 19:35:20 +00:00
|
|
|
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
VIEW_FLOW_REQUEST = 0
|
|
|
|
VIEW_FLOW_RESPONSE = 1
|
|
|
|
|
2015-02-10 18:24:21 +00:00
|
|
|
METHOD_OPTIONS = [
|
|
|
|
("get", "g"),
|
|
|
|
("post", "p"),
|
|
|
|
("put", "u"),
|
|
|
|
("head", "h"),
|
|
|
|
("trace", "t"),
|
|
|
|
("delete", "d"),
|
|
|
|
("options", "o"),
|
|
|
|
("edit raw", "e"),
|
|
|
|
]
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-03-19 22:00:24 +00:00
|
|
|
def is_keypress(k):
|
|
|
|
"""
|
|
|
|
Is this input event a keypress?
|
|
|
|
"""
|
|
|
|
if isinstance(k, basestring):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-04-03 01:38:04 +00:00
|
|
|
def highlight_key(str, key, textattr="text", keyattr="key"):
|
2012-02-07 03:39:37 +00:00
|
|
|
l = []
|
2015-04-03 01:38:04 +00:00
|
|
|
parts = str.split(key, 1)
|
2012-02-07 03:39:37 +00:00
|
|
|
if parts[0]:
|
2015-04-03 01:38:04 +00:00
|
|
|
l.append((textattr, parts[0]))
|
|
|
|
l.append((keyattr, key))
|
2012-02-07 03:39:37 +00:00
|
|
|
if parts[1]:
|
2015-04-03 01:38:04 +00:00
|
|
|
l.append((textattr, parts[1]))
|
2012-02-07 03:39:37 +00:00
|
|
|
return l
|
|
|
|
|
|
|
|
|
2012-02-11 05:23:07 +00:00
|
|
|
KEY_MAX = 30
|
2015-03-13 11:29:21 +00:00
|
|
|
|
|
|
|
|
2012-02-11 05:23:07 +00:00
|
|
|
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:
|
2015-04-15 22:57:12 +00:00
|
|
|
if isinstance(kv[1], urwid.Widget):
|
|
|
|
v = kv[1]
|
|
|
|
elif kv[1] is None:
|
|
|
|
v = urwid.Text("")
|
|
|
|
else:
|
|
|
|
v = urwid.Text([(val, kv[1])])
|
|
|
|
ret.append(
|
|
|
|
urwid.Columns(
|
|
|
|
[
|
|
|
|
("fixed", indent, urwid.Text("")),
|
|
|
|
(
|
|
|
|
"fixed",
|
|
|
|
maxk,
|
|
|
|
urwid.Text([(key, kv[0] or "")])
|
|
|
|
),
|
|
|
|
v
|
|
|
|
],
|
|
|
|
dividechars = 2
|
|
|
|
)
|
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def shortcuts(k):
|
|
|
|
if k == " ":
|
|
|
|
k = "page down"
|
2015-10-30 03:32:50 +00:00
|
|
|
elif k == "ctrl f":
|
|
|
|
k = "page down"
|
|
|
|
elif k == "ctrl b":
|
|
|
|
k = "page up"
|
2012-02-07 03:39:37 +00:00
|
|
|
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"
|
2015-06-11 21:32:13 +00:00
|
|
|
SYMBOL_MARK = u"\u25cf"
|
2012-02-22 23:41:01 +00:00
|
|
|
else:
|
|
|
|
SYMBOL_REPLAY = u"[r]"
|
|
|
|
SYMBOL_RETURN = u"<-"
|
2015-06-11 21:32:13 +00:00
|
|
|
SYMBOL_MARK = "[m]"
|
2012-02-17 22:11:59 +00:00
|
|
|
|
2012-02-17 23:12:01 +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"))
|
2015-07-29 09:39:53 +00:00
|
|
|
|
2015-06-11 15:27:48 +00:00
|
|
|
if f["marked"]:
|
|
|
|
req.append(fcol(SYMBOL_MARK, "mark"))
|
|
|
|
|
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
|
|
|
|
2015-03-13 11:29:21 +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",
|
|
|
|
}
|
2015-05-30 00:03:28 +00:00
|
|
|
ccol = codes.get(f["resp_code"] / 100, "code_other")
|
2012-04-02 02:08:30 +00:00
|
|
|
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))
|
2015-02-26 21:14:20 +00:00
|
|
|
resp.append(fcol(f["roundtrip"], 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-02-06 22:32:22 +00:00
|
|
|
|
|
|
|
# Save file to disk
|
2015-06-09 17:51:11 +00:00
|
|
|
def save_data(path, data):
|
2015-01-15 15:37:26 +00:00
|
|
|
if not path:
|
|
|
|
return
|
|
|
|
try:
|
2015-02-06 03:44:43 +00:00
|
|
|
with file(path, "wb") as f:
|
2015-02-06 22:32:22 +00:00
|
|
|
f.write(data)
|
2015-05-30 00:03:28 +00:00
|
|
|
except IOError as v:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(message=v.strerror)
|
2015-01-15 15:37:26 +00:00
|
|
|
|
2015-04-26 17:25:59 +00:00
|
|
|
|
2015-06-09 17:51:11 +00:00
|
|
|
def ask_save_overwrite(path, data):
|
2015-04-23 06:02:57 +00:00
|
|
|
if not path:
|
|
|
|
return
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
if os.path.exists(path):
|
2015-06-09 17:51:11 +00:00
|
|
|
def save_overwrite(k):
|
2015-04-23 06:02:57 +00:00
|
|
|
if k == "y":
|
2015-06-09 17:51:11 +00:00
|
|
|
save_data(path, data)
|
2015-04-23 06:02:57 +00:00
|
|
|
|
|
|
|
signals.status_prompt_onekey.send(
|
2015-06-09 17:51:11 +00:00
|
|
|
prompt = "'" + path + "' already exists. Overwrite?",
|
2015-04-23 06:02:57 +00:00
|
|
|
keys = (
|
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
2015-06-09 17:51:11 +00:00
|
|
|
callback = save_overwrite
|
2015-04-23 06:02:57 +00:00
|
|
|
)
|
|
|
|
else:
|
2015-06-09 17:51:11 +00:00
|
|
|
save_data(path, data)
|
2015-01-15 15:37:26 +00:00
|
|
|
|
2015-04-26 17:25:59 +00:00
|
|
|
|
2015-06-09 17:51:11 +00:00
|
|
|
def ask_save_path(prompt, data):
|
2015-03-22 00:59:34 +00:00
|
|
|
signals.status_prompt_path.send(
|
2015-03-22 00:32:24 +00:00
|
|
|
prompt = prompt,
|
2015-06-09 17:51:11 +00:00
|
|
|
callback = ask_save_overwrite,
|
2015-10-11 03:42:46 +00:00
|
|
|
args = (data, )
|
2015-02-06 22:32:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-02-06 23:33:29 +00:00
|
|
|
def copy_flow_format_data(part, scope, flow):
|
|
|
|
if part == "u":
|
|
|
|
data = flow.request.url
|
|
|
|
else:
|
|
|
|
data = ""
|
|
|
|
if scope in ("q", "a"):
|
2015-04-26 17:25:59 +00:00
|
|
|
if flow.request.content is None or flow.request.content == CONTENT_MISSING:
|
|
|
|
return None, "Request content is missing"
|
2015-02-06 23:33:29 +00:00
|
|
|
with decoded(flow.request):
|
|
|
|
if part == "h":
|
2015-11-05 03:09:22 +00:00
|
|
|
data += netlib.http.http1.assemble_request(flow.request)
|
2015-02-06 23:33:29 +00:00
|
|
|
elif part == "c":
|
|
|
|
data += flow.request.content
|
|
|
|
else:
|
|
|
|
raise ValueError("Unknown part: {}".format(part))
|
|
|
|
if scope == "a" and flow.request.content and flow.response:
|
|
|
|
# Add padding between request and response
|
|
|
|
data += "\r\n" * 2
|
|
|
|
if scope in ("s", "a") and flow.response:
|
2015-04-26 17:25:59 +00:00
|
|
|
if flow.response.content is None or flow.response.content == CONTENT_MISSING:
|
|
|
|
return None, "Response content is missing"
|
2015-02-06 23:33:29 +00:00
|
|
|
with decoded(flow.response):
|
|
|
|
if part == "h":
|
2015-11-05 03:09:22 +00:00
|
|
|
data += netlib.http.http1.assemble_response(flow.response)
|
2015-02-06 23:33:29 +00:00
|
|
|
elif part == "c":
|
|
|
|
data += flow.response.content
|
|
|
|
else:
|
|
|
|
raise ValueError("Unknown part: {}".format(part))
|
2015-04-23 06:40:57 +00:00
|
|
|
return data, False
|
2015-02-06 23:33:29 +00:00
|
|
|
|
2016-01-13 11:07:28 +00:00
|
|
|
|
2015-10-11 14:04:18 +00:00
|
|
|
def export_prompt(k, flow):
|
|
|
|
if k == "c":
|
|
|
|
copy_as_curl_command(flow)
|
2016-01-13 11:07:28 +00:00
|
|
|
elif k == "p":
|
|
|
|
copy_as_python_code(flow)
|
2016-01-13 13:51:13 +00:00
|
|
|
elif k == "r":
|
|
|
|
copy_as_raw_request(flow)
|
2016-01-13 11:07:28 +00:00
|
|
|
|
2015-10-11 14:04:18 +00:00
|
|
|
|
2015-06-09 02:27:14 +00:00
|
|
|
def copy_as_curl_command(flow):
|
2016-01-13 10:47:01 +00:00
|
|
|
data = "curl "
|
2015-06-09 02:27:14 +00:00
|
|
|
|
2016-01-13 10:47:01 +00:00
|
|
|
for k, v in flow.request.headers.fields:
|
|
|
|
data += "-H '%s:%s' " % (k, v)
|
2015-06-09 02:27:14 +00:00
|
|
|
|
|
|
|
if flow.request.method != "GET":
|
2016-01-13 10:47:01 +00:00
|
|
|
data += "-X %s " % flow.request.method
|
2015-06-09 02:27:14 +00:00
|
|
|
|
|
|
|
full_url = flow.request.scheme + "://" + flow.request.host + flow.request.path
|
2016-01-13 10:47:01 +00:00
|
|
|
data += "'%s'" % full_url
|
2015-06-09 02:27:14 +00:00
|
|
|
|
2016-01-13 10:47:01 +00:00
|
|
|
if flow.request.content:
|
|
|
|
data += " --data-binary '%s'" % flow.request.content
|
2015-06-09 02:27:14 +00:00
|
|
|
|
|
|
|
copy_to_clipboard_or_prompt(data)
|
2015-04-26 17:25:59 +00:00
|
|
|
|
2015-02-06 23:33:29 +00:00
|
|
|
|
2016-01-13 11:07:28 +00:00
|
|
|
def copy_as_python_code(flow):
|
|
|
|
if flow.request.method != "GET":
|
|
|
|
signals.status_message.send(message="Currently, only GET methods are supported")
|
|
|
|
return
|
|
|
|
|
|
|
|
data = ("import requests\n"
|
|
|
|
"headers = {%s}\n"
|
|
|
|
"url = '%s'\n"
|
|
|
|
"resp = requests.get(url, headers=headers)")
|
|
|
|
|
|
|
|
headers = "\n"
|
|
|
|
for k, v in flow.request.headers.fields:
|
|
|
|
headers += " '%s': '%s',\n" % (k, v)
|
|
|
|
|
|
|
|
full_url = flow.request.scheme + "://" + flow.request.host + flow.request.path
|
|
|
|
|
|
|
|
data = data % (headers, full_url)
|
|
|
|
|
|
|
|
copy_to_clipboard_or_prompt(data)
|
|
|
|
|
|
|
|
|
2016-01-13 13:51:13 +00:00
|
|
|
def copy_as_raw_request(flow):
|
|
|
|
data = netlib.http.http1.assemble_request(flow.request)
|
|
|
|
copy_to_clipboard_or_prompt(data)
|
|
|
|
|
|
|
|
|
2015-06-09 02:27:14 +00:00
|
|
|
def copy_to_clipboard_or_prompt(data):
|
2015-06-05 18:33:36 +00:00
|
|
|
# pyperclip calls encode('utf-8') on data to be copied without checking.
|
|
|
|
# if data are already encoded that way UnicodeDecodeError is thrown.
|
2015-06-05 18:19:57 +00:00
|
|
|
toclip = ""
|
2015-02-06 23:33:29 +00:00
|
|
|
try:
|
2015-06-05 18:19:57 +00:00
|
|
|
toclip = data.decode('utf-8')
|
2015-07-29 09:39:53 +00:00
|
|
|
except (UnicodeDecodeError):
|
2015-06-05 18:19:57 +00:00
|
|
|
toclip = data
|
|
|
|
|
|
|
|
try:
|
|
|
|
pyperclip.copy(toclip)
|
2015-06-01 15:06:46 +00:00
|
|
|
except (RuntimeError, UnicodeDecodeError, AttributeError):
|
2015-02-06 23:33:29 +00:00
|
|
|
def save(k):
|
|
|
|
if k == "y":
|
2015-06-09 17:51:11 +00:00
|
|
|
ask_save_path("Save data", data)
|
2015-03-22 00:32:24 +00:00
|
|
|
signals.status_prompt_onekey.send(
|
2015-06-01 15:06:46 +00:00
|
|
|
prompt = "Cannot copy data to clipboard. Save as file?",
|
2015-03-22 00:32:24 +00:00
|
|
|
keys = (
|
2015-02-06 23:33:29 +00:00
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
2015-03-22 00:32:24 +00:00
|
|
|
callback = save
|
2015-02-06 23:33:29 +00:00
|
|
|
)
|
|
|
|
|
2016-01-13 11:07:28 +00:00
|
|
|
|
2015-06-09 02:27:14 +00:00
|
|
|
def copy_flow(part, scope, flow, master, state):
|
|
|
|
"""
|
|
|
|
part: _c_ontent, _h_eaders+content, _u_rl
|
|
|
|
scope: _a_ll, re_q_uest, re_s_ponse
|
|
|
|
"""
|
|
|
|
data, err = copy_flow_format_data(part, scope, flow)
|
|
|
|
|
|
|
|
if err:
|
|
|
|
signals.status_message.send(message=err)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
if scope == "q":
|
|
|
|
signals.status_message.send(message="No request content to copy.")
|
|
|
|
elif scope == "s":
|
|
|
|
signals.status_message.send(message="No response content to copy.")
|
|
|
|
else:
|
|
|
|
signals.status_message.send(message="No contents to copy.")
|
|
|
|
return
|
|
|
|
|
|
|
|
copy_to_clipboard_or_prompt(data)
|
|
|
|
|
2015-02-06 23:33:29 +00:00
|
|
|
|
|
|
|
def ask_copy_part(scope, flow, master, state):
|
|
|
|
choices = [
|
|
|
|
("content", "c"),
|
|
|
|
("headers+content", "h")
|
|
|
|
]
|
|
|
|
if scope != "s":
|
|
|
|
choices.append(("url", "u"))
|
|
|
|
|
2015-03-22 00:32:24 +00:00
|
|
|
signals.status_prompt_onekey.send(
|
|
|
|
prompt = "Copy",
|
|
|
|
keys = choices,
|
|
|
|
callback = copy_flow,
|
|
|
|
args = (scope, flow, master, state)
|
2015-02-06 23:33:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-02-06 22:32:22 +00:00
|
|
|
def ask_save_body(part, master, state, flow):
|
|
|
|
"""
|
2015-03-13 12:14:37 +00:00
|
|
|
Save either the request or the response body to disk. part can either be
|
|
|
|
"q" (request), "s" (response) or None (ask user if necessary).
|
2015-02-06 22:32:22 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
request_has_content = flow.request and flow.request.content
|
|
|
|
response_has_content = flow.response and flow.response.content
|
|
|
|
|
|
|
|
if part is None:
|
2015-03-13 12:14:37 +00:00
|
|
|
# We first need to determine whether we want to save the request or the
|
|
|
|
# response content.
|
2015-02-06 22:32:22 +00:00
|
|
|
if request_has_content and response_has_content:
|
2015-03-22 00:32:24 +00:00
|
|
|
signals.status_prompt_onekey.send(
|
|
|
|
prompt = "Save",
|
|
|
|
keys = (
|
2015-02-06 22:32:22 +00:00
|
|
|
("request", "q"),
|
|
|
|
("response", "s"),
|
|
|
|
),
|
2015-03-22 00:32:24 +00:00
|
|
|
callback = ask_save_body,
|
|
|
|
args = (master, state, flow)
|
2015-01-16 16:08:25 +00:00
|
|
|
)
|
2015-02-06 22:32:22 +00:00
|
|
|
elif response_has_content:
|
|
|
|
ask_save_body("s", master, state, flow)
|
2015-01-16 16:08:25 +00:00
|
|
|
else:
|
2015-02-06 22:32:22 +00:00
|
|
|
ask_save_body("q", master, state, flow)
|
|
|
|
|
|
|
|
elif part == "q" and request_has_content:
|
2015-03-13 12:14:37 +00:00
|
|
|
ask_save_path(
|
2015-03-22 00:59:34 +00:00
|
|
|
"Save request content",
|
2015-06-09 17:51:11 +00:00
|
|
|
flow.request.get_decoded_content()
|
2015-03-13 12:14:37 +00:00
|
|
|
)
|
2015-02-06 22:32:22 +00:00
|
|
|
elif part == "s" and response_has_content:
|
2015-03-13 12:14:37 +00:00
|
|
|
ask_save_path(
|
2015-03-22 00:59:34 +00:00
|
|
|
"Save response content",
|
2015-06-09 17:51:11 +00:00
|
|
|
flow.response.get_decoded_content()
|
2015-03-13 12:14:37 +00:00
|
|
|
)
|
2015-02-06 22:32:22 +00:00
|
|
|
else:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(message="No content to save.")
|
2015-01-16 16:08:25 +00:00
|
|
|
|
2015-02-06 22:32:22 +00:00
|
|
|
|
2015-03-22 08:00:41 +00:00
|
|
|
flowcache = utils.LRUCache(800)
|
2012-02-26 21:00:44 +00:00
|
|
|
|
|
|
|
|
2015-06-12 01:27:33 +00:00
|
|
|
def format_flow(f, focus, extended=False, hostheader=False, padding=2,
|
|
|
|
marked=False):
|
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,
|
2015-09-25 23:23:59 +00:00
|
|
|
req_url = f.request.pretty_url if hostheader else f.request.url,
|
2012-02-26 21:00:44 +00:00
|
|
|
|
|
|
|
err_msg = f.error.msg if f.error else None,
|
2015-09-17 13:16:33 +00:00
|
|
|
resp_code = f.response.status_code if f.response else None,
|
2015-07-29 09:39:53 +00:00
|
|
|
|
2015-06-12 01:27:33 +00:00
|
|
|
marked = marked,
|
2012-02-26 21:00:44 +00:00
|
|
|
)
|
|
|
|
if f.response:
|
2012-05-16 06:16:44 +00:00
|
|
|
if f.response.content:
|
2015-04-30 00:18:01 +00:00
|
|
|
contentdesc = netlib.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]"
|
2015-02-26 21:14:20 +00:00
|
|
|
duration = 0
|
2015-03-02 01:19:32 +00:00
|
|
|
if f.response.timestamp_end and f.request.timestamp_start:
|
|
|
|
duration = f.response.timestamp_end - f.request.timestamp_start
|
2015-02-26 21:14:20 +00:00
|
|
|
roundtrip = utils.pretty_duration(duration)
|
2013-08-13 19:02:49 +00:00
|
|
|
|
2012-02-26 21:00:44 +00:00
|
|
|
d.update(dict(
|
2015-09-17 13:16:33 +00:00
|
|
|
resp_code = f.response.status_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,
|
2015-02-26 21:14:20 +00:00
|
|
|
roundtrip = roundtrip,
|
2012-02-26 21:00:44 +00:00
|
|
|
))
|
2015-09-05 18:45:58 +00:00
|
|
|
t = f.response.headers.get("content-type")
|
2012-02-26 21:00:44 +00:00
|
|
|
if t:
|
2015-09-05 18:45:58 +00:00
|
|
|
d["resp_ctype"] = t.split(";")[0]
|
2012-02-26 21:00:44 +00:00
|
|
|
else:
|
|
|
|
d["resp_ctype"] = ""
|
2015-03-22 08:00:41 +00:00
|
|
|
return flowcache.get(
|
|
|
|
raw_format_flow,
|
2015-03-13 11:29:21 +00:00
|
|
|
tuple(sorted(d.items())), focus, extended, padding
|
|
|
|
)
|