aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-03-20 10:31:07 +1300
committerAldo Cortesi <aldo@nullcube.com>2012-03-20 10:31:07 +1300
commit2739cb4861b5d8b35ab9db0d20128b1bdc5808cb (patch)
tree04844aa16798308d849f976269708e496d4e2da3
parentbc3ba4c9930fabe4c0c98b8cd550ad91f878227f (diff)
downloadmitmproxy-2739cb4861b5d8b35ab9db0d20128b1bdc5808cb.tar.gz
mitmproxy-2739cb4861b5d8b35ab9db0d20128b1bdc5808cb.tar.bz2
mitmproxy-2739cb4861b5d8b35ab9db0d20128b1bdc5808cb.zip
Add a simple parser for content type specifications.
-rw-r--r--libmproxy/console/flowview.py1
-rw-r--r--libmproxy/utils.py27
-rw-r--r--test/test_utils.py11
3 files changed, 39 insertions, 0 deletions
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(),