From 215383554525535816accdd8580d06b9e4cf7565 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 20 Mar 2012 10:58:43 +1300 Subject: 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. --- libmproxy/console/__init__.py | 4 ++-- libmproxy/console/common.py | 23 +++++++++++++++-------- libmproxy/console/flowview.py | 32 ++++++++++++++++++-------------- libmproxy/utils.py | 2 +- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index 0473a711..553b560b 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -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) diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index 8dbd6a98..9e8c3076 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -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 } diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index 6038a777..4c4cc86b 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -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 ) diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 0eaada7c..97aa1e55 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -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): -- cgit v1.2.3