Skip to content

Commit

Permalink
Merge pull request #16 from scop/q
Browse files Browse the repository at this point in the history
q value handling improvements
  • Loading branch information
wojcikstefan committed May 2, 2016
2 parents c1e94e2 + 64a64e3 commit 3cb332e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mimeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ 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:
params.setdefault('q', params.pop('Q', None)) # q is case insensitive
try:
if not params['q'] or not 0 <= float(params['q']) <= 1:
params['q'] = '1'
except ValueError: # from float()
params['q'] = '1'

return (type, subtype, params)
Expand Down
3 changes: 3 additions & 0 deletions testdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
["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"}]],
["application/xml ; q=foo", ["application", "xml", {"q": "1"}]],
["application/xml ; Q=0.6", ["application", "xml", {"q": "0.6"}]],
[" *; q=.2",["*", "*", {"q": ".2"}]]
],

Expand Down

0 comments on commit 3cb332e

Please sign in to comment.