From 2739cb4861b5d8b35ab9db0d20128b1bdc5808cb Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 20 Mar 2012 10:31:07 +1300 Subject: Add a simple parser for content type specifications. --- libmproxy/console/flowview.py | 1 + libmproxy/utils.py | 27 +++++++++++++++++++++++++++ test/test_utils.py | 11 +++++++++++ 3 files changed, 39 insertions(+) diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index eb496732..6038a777 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -209,6 +209,7 @@ class ConnectionView(common.WWrap): break else: ctype = common.BODY_PRETTY_TYPES[pretty_type] + if ctype and flow.HDR_FORM_URLENCODED in ctype: data = utils.urldecode(content) if data: diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 02bc49cb..0eaada7c 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -265,6 +265,33 @@ def parse_proxy_spec(url): return p[:3] +def parse_content_type(c): + """ + A simple parser for content-type values. Returns a (type, subtype, + parameters) tuple, where type and subtype are strings, and parameters + is a dict. If the string could not be parsed, return None. + + E.g. the following string: + + text/html; charset=UTF-8 + + Returns: + + ("text", "html", {"charset": "UTF-8"}) + """ + parts = c.split(";", 1) + ts = parts[0].split("/", 1) + if len(ts) != 2: + return None + d = {} + if len(parts) == 2: + for i in parts[1].split(";"): + clause = i.split("=", 1) + if len(clause) == 2: + d[clause[0].strip()] = clause[1].strip() + return ts[0], ts[1], d + + def hostport(scheme, host, port): """ Returns the host component, with a port specifcation if needed. diff --git a/test/test_utils.py b/test/test_utils.py index 9f07c706..d65fa5b4 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -214,7 +214,18 @@ class u_parse_size(libpry.AutoTree): libpry.raises(ValueError, utils.parse_size, "ak") +class u_parse_content_type(libpry.AutoTree): + def test_simple(self): + p = utils.parse_content_type + assert p("text/html") == ("text", "html", {}) + assert p("text") == None + + v = p("text/html; charset=UTF-8") + assert v == ('text', 'html', {'charset': 'UTF-8'}) + + tests = [ + u_parse_content_type(), uformat_timestamp(), uisBin(), uisXML(), -- cgit v1.2.3