diff options
author | Ville Skyttä <ville.skytta@iki.fi> | 2016-04-28 11:49:05 +0300 |
---|---|---|
committer | Ville Skyttä <ville.skytta@iki.fi> | 2016-04-28 11:49:05 +0300 |
commit | 109b38802db3a1128b064142a6a9fd72df74d08d (patch) | |
tree | 9529c1e2f592c9e7f25b6c3ed20988818d9a1c19 | |
parent | cc66740c6e46b85ac55e989fd4883d5697e15c35 (diff) | |
download | python-mimeparse-109b38802db3a1128b064142a6a9fd72df74d08d.tar.gz |
Use cgi.parse_header for main type parsing
Adds support for quoted string parameter values.
-rw-r--r-- | mimeparse.py | 7 | ||||
-rw-r--r-- | testdata.json | 1 |
2 files changed, 3 insertions, 5 deletions
diff --git a/mimeparse.py b/mimeparse.py index 6c83bcd..d16713f 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -17,6 +17,7 @@ Contents: - best_match(): Choose the mime-type with the highest quality ('q') from a list of candidates. """ +import cgi from functools import reduce __version__ = '1.5.2' @@ -40,11 +41,7 @@ def parse_mime_type(mime_type): ('application', 'xhtml', {'q', '0.5'}) """ - parts = mime_type.split(';') - params = dict([tuple([s.strip() for s in param.split('=', 1)]) - for param in parts[1:] - ]) - full_type = parts[0].strip() + full_type, params = cgi.parse_header(mime_type) # Java URLConnection class sends an Accept header that includes a # single '*'. Turn it into a legal wildcard. if full_type == '*': diff --git a/testdata.json b/testdata.json index bb89e6f..409ab72 100644 --- a/testdata.json +++ b/testdata.json @@ -41,6 +41,7 @@ "parse_mime_type": [ ["application/xhtml;q=0.5", ["application", "xhtml", {"q": "0.5"}]], ["application/xhtml;q=0.5;ver=1.2", ["application", "xhtml", {"q": "0.5", "ver": "1.2"}]], + ["application/xhtml;q=0.5;foo=\"bar quux\"", ["application", "xhtml", {"q": "0.5", "foo": "bar quux"}]], ["text", null], ["text/something/invalid", null] ] |