Refactor pretty view forcing somewhat.
- Use a lookup table of content types -> view modes. - Add a urlencoded forcing. Remove "html" - at the moment it's the same as "xmlish". - Display type when forced.
This commit is contained in:
parent
2739cb4861
commit
2153835545
|
@ -701,10 +701,10 @@ class ConsoleMaster(flow.FlowMaster):
|
|||
def change_pretty_type(self, t):
|
||||
if t == "a":
|
||||
self.state.view_body_pretty_type = common.VIEW_BODY_PRETTY_TYPE_AUTO
|
||||
elif t == "h":
|
||||
self.state.view_body_pretty_type = common.VIEW_BODY_PRETTY_TYPE_HTML
|
||||
elif t == "j":
|
||||
self.state.view_body_pretty_type = common.VIEW_BODY_PRETTY_TYPE_JSON
|
||||
elif t == "u":
|
||||
self.state.view_body_pretty_type = common.VIEW_BODY_PRETTY_TYPE_URLENCODED
|
||||
elif t == "x":
|
||||
self.state.view_body_pretty_type = common.VIEW_BODY_PRETTY_TYPE_XML
|
||||
self.refresh_flow(self.currentflow)
|
||||
|
|
|
@ -17,27 +17,34 @@ import urwid
|
|||
import urwid.util
|
||||
from .. import utils
|
||||
|
||||
|
||||
VIEW_BODY_RAW = 0
|
||||
VIEW_BODY_HEX = 1
|
||||
VIEW_BODY_PRETTY = 2
|
||||
|
||||
|
||||
BODY_VIEWS = {
|
||||
VIEW_BODY_RAW: "raw",
|
||||
VIEW_BODY_HEX: "hex",
|
||||
VIEW_BODY_PRETTY: "pretty"
|
||||
}
|
||||
|
||||
|
||||
VIEW_BODY_PRETTY_TYPE_AUTO = 0
|
||||
VIEW_BODY_PRETTY_TYPE_HTML = 1
|
||||
VIEW_BODY_PRETTY_TYPE_JSON = 2
|
||||
VIEW_BODY_PRETTY_TYPE_XML = 3
|
||||
VIEW_BODY_PRETTY_TYPE_JSON = 1
|
||||
VIEW_BODY_PRETTY_TYPE_XML = 2
|
||||
VIEW_BODY_PRETTY_TYPE_URLENCODED = 3
|
||||
|
||||
BODY_PRETTY_NAMES = {
|
||||
VIEW_BODY_PRETTY_TYPE_JSON: "json",
|
||||
VIEW_BODY_PRETTY_TYPE_XML: "xmlish",
|
||||
VIEW_BODY_PRETTY_TYPE_URLENCODED: "urlencoded"
|
||||
}
|
||||
|
||||
BODY_PRETTY_TYPES = {
|
||||
VIEW_BODY_PRETTY_TYPE_AUTO: None,
|
||||
VIEW_BODY_PRETTY_TYPE_HTML: "text/html",
|
||||
VIEW_BODY_PRETTY_TYPE_JSON: "application/json",
|
||||
VIEW_BODY_PRETTY_TYPE_XML: "text/xml",
|
||||
"text/html": VIEW_BODY_PRETTY_TYPE_XML,
|
||||
"application/json": VIEW_BODY_PRETTY_TYPE_JSON,
|
||||
"text/xml": VIEW_BODY_PRETTY_TYPE_XML,
|
||||
"multipart/form-data": VIEW_BODY_PRETTY_TYPE_URLENCODED
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -199,32 +199,34 @@ class ConnectionView(common.WWrap):
|
|||
val = "text"
|
||||
)
|
||||
|
||||
|
||||
def _find_pretty_view(self, content, hdrItems, pretty_type=common.VIEW_BODY_PRETTY_TYPE_AUTO):
|
||||
ctype = None
|
||||
if pretty_type == common.VIEW_BODY_PRETTY_TYPE_AUTO:
|
||||
pretty_type == None
|
||||
for i in hdrItems:
|
||||
if i[0].lower() == "content-type":
|
||||
ctype = i[1]
|
||||
break
|
||||
else:
|
||||
ctype = common.BODY_PRETTY_TYPES[pretty_type]
|
||||
ct = utils.parse_content_type(ctype) if ctype else None
|
||||
if ct:
|
||||
pretty_type = common.BODY_PRETTY_TYPES.get("%s/%s"%(ct[0], ct[1]))
|
||||
if not pretty_type and utils.isXML(content):
|
||||
pretty_type = common.VIEW_BODY_PRETTY_TYPE_XML
|
||||
|
||||
if ctype and flow.HDR_FORM_URLENCODED in ctype:
|
||||
if pretty_type == common.VIEW_BODY_PRETTY_TYPE_URLENCODED:
|
||||
data = utils.urldecode(content)
|
||||
if data:
|
||||
return "URLEncoded form", self._view_flow_urlencoded(data)
|
||||
if (ctype and ("text/xml" in ctype or "text/html" in ctype)) or utils.isXML(content):
|
||||
|
||||
if pretty_type == common.VIEW_BODY_PRETTY_TYPE_XML:
|
||||
return "Indented XML-ish", self._view_flow_xmlish(content)
|
||||
elif ctype and "application/json" in ctype:
|
||||
|
||||
if pretty_type == common.VIEW_BODY_PRETTY_TYPE_JSON:
|
||||
lines = utils.pretty_json(content)
|
||||
if lines:
|
||||
return "JSON", self._view_flow_json(lines)
|
||||
elif ctype and "multipart/form-data" in ctype:
|
||||
boundary = ctype.split('boundary=')
|
||||
if len(boundary) > 1:
|
||||
return "Form data", self._view_flow_formdata(content, boundary[1].split(';')[0])
|
||||
return "", self._view_flow_raw(content)
|
||||
|
||||
return "Falling back to raw.", self._view_flow_raw(content)
|
||||
|
||||
def _cached_conn_text(self, e, content, hdrItems, viewmode, pretty_type):
|
||||
txt = common.format_keyvals(
|
||||
|
@ -245,8 +247,10 @@ class ConnectionView(common.WWrap):
|
|||
if e and e != "identity":
|
||||
emsg = "[decoded %s]"%e
|
||||
msg, body = self._find_pretty_view(content, hdrItems, pretty_type)
|
||||
|
||||
if pretty_type != common.VIEW_BODY_PRETTY_TYPE_AUTO:
|
||||
msg += " (forced)"
|
||||
emsg += " (forced to %s)"%(common.BODY_PRETTY_NAMES[pretty_type])
|
||||
|
||||
if emsg:
|
||||
msg = emsg + " " + msg
|
||||
else:
|
||||
|
@ -579,9 +583,9 @@ class ConnectionView(common.WWrap):
|
|||
"Pretty-Print format",
|
||||
(
|
||||
("auto detect", "a"),
|
||||
("html", "h"),
|
||||
("json", "j"),
|
||||
("xml", "x"),
|
||||
("urlencoded", "u"),
|
||||
("xmlish", "x"),
|
||||
),
|
||||
self.master.change_pretty_type
|
||||
)
|
||||
|
|
|
@ -289,7 +289,7 @@ def parse_content_type(c):
|
|||
clause = i.split("=", 1)
|
||||
if len(clause) == 2:
|
||||
d[clause[0].strip()] = clause[1].strip()
|
||||
return ts[0], ts[1], d
|
||||
return ts[0].lower(), ts[1].lower(), d
|
||||
|
||||
|
||||
def hostport(scheme, host, port):
|
||||
|
|
Loading…
Reference in New Issue