Skip to content

Commit 109b388

Browse files
committed
Use cgi.parse_header for main type parsing
Adds support for quoted string parameter values.
1 parent cc66740 commit 109b388

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

mimeparse.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- best_match(): Choose the mime-type with the highest quality ('q')
1818
from a list of candidates.
1919
"""
20+
import cgi
2021
from functools import reduce
2122

2223
__version__ = '1.5.2'
@@ -40,11 +41,7 @@ def parse_mime_type(mime_type):
4041
4142
('application', 'xhtml', {'q', '0.5'})
4243
"""
43-
parts = mime_type.split(';')
44-
params = dict([tuple([s.strip() for s in param.split('=', 1)])
45-
for param in parts[1:]
46-
])
47-
full_type = parts[0].strip()
44+
full_type, params = cgi.parse_header(mime_type)
4845
# Java URLConnection class sends an Accept header that includes a
4946
# single '*'. Turn it into a legal wildcard.
5047
if full_type == '*':

testdata.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"parse_mime_type": [
4242
["application/xhtml;q=0.5", ["application", "xhtml", {"q": "0.5"}]],
4343
["application/xhtml;q=0.5;ver=1.2", ["application", "xhtml", {"q": "0.5", "ver": "1.2"}]],
44+
["application/xhtml;q=0.5;foo=\"bar quux\"", ["application", "xhtml", {"q": "0.5", "foo": "bar quux"}]],
4445
["text", null],
4546
["text/something/invalid", null]
4647
]

0 commit comments

Comments
 (0)