From ec34cae6181d6af0150ac730d70b96104a07e9d5 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 19:07:55 +1200 Subject: utils.multipartdecode -> http.multipart.decode also utils.parse_content_type -> http.headers.parse_content_type --- netlib/http/headers.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'netlib/http/headers.py') diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 6165fd61..8f669ec1 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -175,3 +175,30 @@ class Headers(MultiDict): fields.append([name, value]) self.fields = fields return replacements + + +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].lower(), ts[1].lower(), d -- cgit v1.2.3 From 40a030f215e1943aefdb2eb6fe2a264b9b1ee33c Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 19:58:28 +1200 Subject: Satisfy flake8 --- netlib/http/headers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'netlib/http/headers.py') diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 8f669ec1..fa7b7180 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -176,7 +176,7 @@ class Headers(MultiDict): self.fields = fields return replacements - + def parse_content_type(c): """ A simple parser for content-type values. Returns a (type, subtype, -- cgit v1.2.3