From 94ae720a220da4beaa2fc6111b4cafb60b41d33b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 15 Jul 2011 16:16:43 +1200 Subject: [PATCH] Add a pretty-printing mode for urlencoded form data. --- libmproxy/console.py | 17 ++++++++++++++++- libmproxy/utils.py | 7 ++++++- test/test_utils.py | 7 +++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/libmproxy/console.py b/libmproxy/console.py index 46aacab64..09149186e 100644 --- a/libmproxy/console.py +++ b/libmproxy/console.py @@ -924,6 +924,17 @@ class ConsoleMaster(flow.FlowMaster): break self._trailer(sum(len(i) for i in lines), txt) + def _view_conn_urlencoded(self, lines, txt): + txt.append(urwid.Text(("highlight", "URLencoded data:\n"))) + txt.append( + urwid.Text( + format_keyvals( + [(k+":", v) for (k, v) in lines], + key = "header", + val = "text" + ) + ) + ) def _find_pretty_view(self, content, hdrItems, txt): ctype = None @@ -931,9 +942,13 @@ class ConsoleMaster(flow.FlowMaster): if i[0] == "content-type": ctype = i[1] break + if ctype and "x-www-form-urlencoded" in ctype: + data = utils.urldecode(content) + if data: + return self._view_conn_urlencoded(data, txt) if utils.isXML(content): return self._view_conn_xmlish(content, txt) - if ctype and "application/json" in ctype: + elif ctype and "application/json" in ctype: lines = utils.pretty_json(content) if lines: return self._view_conn_json(lines, txt) diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 38fc61078..04a145d52 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -12,7 +12,8 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import re, os, subprocess, datetime, textwrap, errno, sys, time, functools, copy +import re, os, subprocess, datetime, textwrap, errno +import time, functools, copy, cgi import json CERT_SLEEP_TIME = 1 @@ -120,6 +121,10 @@ def pretty_json(s): return json.dumps(p, sort_keys=True, indent=4).split("\n") +def urldecode(s): + return cgi.parse_qsl(s) + + def hexdump(s): """ Returns a set of typles: diff --git a/test/test_utils.py b/test/test_utils.py index d5957872d..2b0f43422 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -221,6 +221,12 @@ class upretty_json(libpry.AutoTree): assert not utils.pretty_json("moo") +class u_urldecode(libpry.AutoTree): + def test_one(self): + s = "one=two&three=four" + assert len(utils.urldecode(s)) == 2 + + class udummy_ca(libpry.AutoTree): def test_all(self): d = self.tmpdir() @@ -306,6 +312,7 @@ tests = [ uData(), upretty_xmlish(), upretty_json(), + u_urldecode(), udummy_ca(), udummy_cert(), uLRUCache(),