summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Wójcik <wojcikstefan@gmail.com>2016-05-01 22:54:52 -0700
committerStefan Wójcik <wojcikstefan@gmail.com>2016-05-01 22:54:52 -0700
commitc1e94e242ed3a499df7bc463aee239218629931e (patch)
treea16cace66acf4400d4731f11af3a88ec5331585d
parent21385419371c212449fee83bd8b1cbe0824f845d (diff)
parent109b38802db3a1128b064142a6a9fd72df74d08d (diff)
downloadpython-mimeparse-c1e94e242ed3a499df7bc463aee239218629931e.tar.gz
Merge pull request #19 from scop/use-cgi
Use cgi.parse_header for main type parsing
-rw-r--r--mimeparse.py7
-rw-r--r--testdata.json1
2 files changed, 3 insertions, 5 deletions
diff --git a/mimeparse.py b/mimeparse.py
index f1677f9..2884b1d 100644
--- a/mimeparse.py
+++ b/mimeparse.py
@@ -18,6 +18,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'
@@ -41,11 +42,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]
]