Skip to content

Commit 40e4417

Browse files
wojcikstefandbtsai
authored andcommitted
make sure mimeparse gracefully handles an invalid mime type of the form "text/extra/part" (#14)
1 parent cbf1c86 commit 40e4417

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

mimeparse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ def parse_mime_type(mime_type):
5050
if full_type == '*':
5151
full_type = '*/*'
5252

53-
if '/' not in full_type:
53+
type_parts = full_type.split('/') if '/' in full_type else None
54+
if not type_parts or len(type_parts) > 2:
5455
raise MimeTypeParseException("Can't parse type \"{}\"".format(full_type))
5556

56-
(type, subtype) = full_type.split('/')
57+
(type, subtype) = type_parts
5758

5859
return (type.strip(), subtype.strip(), params)
5960

testdata.json

Lines changed: 2 additions & 1 deletion
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-
["text", null]
44+
["text", null],
45+
["text/something/invalid", null]
4546
]
4647
}

0 commit comments

Comments
 (0)