Skip to content

Commit c1e94e2

Browse files
committed
Merge pull request #19 from scop/use-cgi
Use cgi.parse_header for main type parsing
2 parents 2138541 + 109b388 commit c1e94e2

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
@@ -18,6 +18,7 @@
1818
- best_match(): Choose the mime-type with the highest quality ('q')
1919
from a list of candidates.
2020
"""
21+
import cgi
2122
from functools import reduce
2223

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