aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/utils.py
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 /libmproxy/utils.py
parentbc3ba4c9930fabe4c0c98b8cd550ad91f878227f (diff)
downloadmitmproxy-2739cb4861b5d8b35ab9db0d20128b1bdc5808cb.tar.gz
mitmproxy-2739cb4861b5d8b35ab9db0d20128b1bdc5808cb.tar.bz2
mitmproxy-2739cb4861b5d8b35ab9db0d20128b1bdc5808cb.zip
Add a simple parser for content type specifications.
Diffstat (limited to 'libmproxy/utils.py')
-rw-r--r--libmproxy/utils.py27
1 files changed, 27 insertions, 0 deletions
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.