Skip to content

Commit

Permalink
Simplify content type negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 20, 2016
1 parent 5ab64d7 commit d374ce9
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions aspen/http/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@ def use_accept_from_dispatcher(self, dispatch_accept):

def negotiate_content_type(self, available, accept):
"""Given information about available and acceptable types, return a type or None.
Returns a content type if one can be negotiated.
Returns `None` if the accept header is malformed.
Raises `NegotiationFailure` if the accept header is well-formed but not
satisfiable given the available types.
"""
content_type = None
try:
best_match = mimeparse.best_match(available, accept)
content_type = mimeparse.best_match(available, accept)
except ValueError: # unparseable Accept header
best_match = None
if best_match:
content_type = best_match
elif best_match == '':
content_type = None
if content_type == '':
raise NegotiationFailure(accept, available)
return content_type

0 comments on commit d374ce9

Please sign in to comment.