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/utils.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'libmproxy/utils.py') 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. -- cgit v1.2.3