From e27e98702ec803ba2f12f6196fdadb8cb20ecf93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 28 Apr 2016 10:17:47 +0300 Subject: Add test case for negative q value --- testdata.json | 1 + 1 file changed, 1 insertion(+) diff --git a/testdata.json b/testdata.json index 409ab72..d5f079a 100644 --- a/testdata.json +++ b/testdata.json @@ -4,6 +4,7 @@ ["application/xml", ["application", "xml", {"q": "1"}]], ["application/xml;q=",["application", "xml", {"q": "1"}]], ["application/xml ;q=",["application", "xml", {"q": "1"}]], + ["application/xml ;q=-1",["application", "xml", {"q": "1"}]], ["application/xml ; q=1;b=other",["application", "xml", {"q": "1", "b":"other"}]], ["application/xml ; q=2;b=other",["application", "xml", {"q": "1", "b":"other"}]], ["application/xml ; q=0",["application", "xml", {"q": "0"}]], -- cgit v1.2.1 From 78e3e74deff9dddc54e22d1fbe9071383ab39ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 28 Apr 2016 10:33:18 +0300 Subject: Simplify q value validation --- mimeparse.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mimeparse.py b/mimeparse.py index 2884b1d..4be4eb1 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -72,8 +72,7 @@ def parse_media_range(range): necessary. """ (type, subtype, params) = parse_mime_type(range) - if 'q' not in params or not params['q'] or \ - float(params['q']) > 1 or float(params['q']) < 0: + if not params.get('q') or not 0 <= float(params['q']) <= 1: params['q'] = '1' return (type, subtype, params) -- cgit v1.2.1 From 7d3bf001d284ffbd9d555ce213241040c291f79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 28 Apr 2016 10:41:02 +0300 Subject: Handle non-numeric q value gracefully --- mimeparse.py | 5 ++++- testdata.json | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mimeparse.py b/mimeparse.py index 4be4eb1..7f4aa3c 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -72,7 +72,10 @@ def parse_media_range(range): necessary. """ (type, subtype, params) = parse_mime_type(range) - if not params.get('q') or not 0 <= float(params['q']) <= 1: + try: + if not params.get('q') or not 0 <= float(params['q']) <= 1: + params['q'] = '1' + except ValueError: # from float() params['q'] = '1' return (type, subtype, params) diff --git a/testdata.json b/testdata.json index d5f079a..ab7693c 100644 --- a/testdata.json +++ b/testdata.json @@ -8,6 +8,7 @@ ["application/xml ; q=1;b=other",["application", "xml", {"q": "1", "b":"other"}]], ["application/xml ; q=2;b=other",["application", "xml", {"q": "1", "b":"other"}]], ["application/xml ; q=0",["application", "xml", {"q": "0"}]], + ["application/xml ; q=foo", ["application", "xml", {"q": "1"}]], [" *; q=.2",["*", "*", {"q": ".2"}]] ], -- cgit v1.2.1 From 64a64e32ab8c14e91a6a196cf0a2dd7637d4f3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 28 Apr 2016 10:47:19 +0300 Subject: Handle q parameter name case insensitively https://tools.ietf.org/html/rfc7231#section-5.3.1 --- mimeparse.py | 3 ++- testdata.json | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/mimeparse.py b/mimeparse.py index 7f4aa3c..91b28b8 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -72,8 +72,9 @@ def parse_media_range(range): necessary. """ (type, subtype, params) = parse_mime_type(range) + params.setdefault('q', params.pop('Q', None)) # q is case insensitive try: - if not params.get('q') or not 0 <= float(params['q']) <= 1: + if not params['q'] or not 0 <= float(params['q']) <= 1: params['q'] = '1' except ValueError: # from float() params['q'] = '1' diff --git a/testdata.json b/testdata.json index ab7693c..4f0a648 100644 --- a/testdata.json +++ b/testdata.json @@ -9,6 +9,7 @@ ["application/xml ; q=2;b=other",["application", "xml", {"q": "1", "b":"other"}]], ["application/xml ; q=0",["application", "xml", {"q": "0"}]], ["application/xml ; q=foo", ["application", "xml", {"q": "1"}]], + ["application/xml ; Q=0.6", ["application", "xml", {"q": "0.6"}]], [" *; q=.2",["*", "*", {"q": ".2"}]] ], -- cgit v1.2.1