From 40e441782286b62caf521892af6b6e83021b5898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20W=C3=B3jcik?= Date: Thu, 21 Apr 2016 06:18:39 +0200 Subject: make sure mimeparse gracefully handles an invalid mime type of the form "text/extra/part" (#14) --- mimeparse.py | 5 +++-- testdata.json | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/mimeparse.py b/mimeparse.py index 3563249..8cdb6e2 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -50,10 +50,11 @@ def parse_mime_type(mime_type): if full_type == '*': full_type = '*/*' - if '/' not in full_type: + type_parts = full_type.split('/') if '/' in full_type else None + if not type_parts or len(type_parts) > 2: raise MimeTypeParseException("Can't parse type \"{}\"".format(full_type)) - (type, subtype) = full_type.split('/') + (type, subtype) = type_parts return (type.strip(), subtype.strip(), params) diff --git a/testdata.json b/testdata.json index c23f668..bb89e6f 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"}]], - ["text", null] + ["text", null], + ["text/something/invalid", null] ] } -- cgit v1.2.1