-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from kxepal/refactor-client-response-decoding
Refactor client response decoding
- Loading branch information
Showing
5 changed files
with
175 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Various helper functions""" | ||
|
||
|
||
def parse_mimetype(mimetype): | ||
"""Parses a MIME type into it components. | ||
:param str mimetype: MIME type | ||
:returns: 4 element tuple for MIME type, subtype, suffix and parameters | ||
:rtype: tuple | ||
>>> parse_mimetype('*') | ||
('*', '*', '', {}) | ||
>>> parse_mimetype('application/json') | ||
('application', 'json', '', {}) | ||
>>> parse_mimetype('application/json; charset=utf-8') | ||
('application', 'json', '', {'charset': 'utf-8'}) | ||
>>> parse_mimetype('''application/json; | ||
... charset=utf-8;''') | ||
('application', 'json', '', {'charset': 'utf-8'}) | ||
>>> parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') | ||
('application', 'json', '', {'charset': 'UTF-8'}) | ||
>>> parse_mimetype('application/rss+xml') | ||
('application', 'rss', 'xml', {}) | ||
>>> parse_mimetype('text/plain;base64') | ||
('text', 'plain', '', {'base64': ''}) | ||
""" | ||
if not mimetype: | ||
return '', '', '', {} | ||
|
||
parts = mimetype.split(';') | ||
params = [] | ||
for item in parts[1:]: | ||
if not item: | ||
continue | ||
key, value = item.split('=', 2) if '=' in item else (item, '') | ||
params.append((key.lower().strip(), value.strip(' "'))) | ||
params = dict(params) | ||
|
||
fulltype = parts[0].strip().lower() | ||
if fulltype == '*': | ||
fulltype = '*/*' | ||
mtype, stype = fulltype.split('/', 2) if '/' in fulltype else (fulltype, '') | ||
stype, suffix = stype.split('+') if '+' in stype else (stype, '') | ||
|
||
return mtype, stype, suffix, params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.