Example #1
0
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No dispatchers for method, send 405 with list of allowed methods.
     if dispatchers is None:
         return http.method_not_allowed(", ".join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         response = callable(self, request)
         # Try to autocomplete the content-type header if not set
         # explicitly.
         # If there's no accept from the client and there's only one
         # possible type from the match then use that as the best match.
         # Otherwise use mimeparse to work out what the best match was. If
         # the best match if not a wildcard then we know what content-type
         # should be.
         if isinstance(response, http.Response) and not response.headers.get("content-type"):
             accept = str(request.accept)
             if not accept and len(match["accept"]) == 1:
                 best_match = match["accept"][0]
             else:
                 best_match = mimeparse.best_match(match["accept"], accept)
             if "*" not in best_match:
                 response.headers["content-type"] = best_match
         return response
     # No match, send 406
     return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
Example #2
0
 def test_not_acceptable(self):
     r = http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
     assert r.status.startswith("406")
     assert r.headers["Content-Type"] == "text/plain"
     assert "406 Not Acceptable" in r.body
     exc = http.NotAcceptableError([("Content-Type", "text/plain")], "406 Not Acceptable")
     r = exc.make_response()
     assert r.status.startswith("406")
Example #3
0
 def test_not_acceptable(self):
     r = http.not_acceptable([('Content-Type', 'text/plain')], '406 Not Acceptable')
     assert r.status.startswith('406')
     assert r.headers['Content-Type'] == 'text/plain'
     assert '406 Not Acceptable' in r.body
     exc = http.NotAcceptableError([('Content-Type', 'text/plain')], '406 Not Acceptable')
     r = exc.make_response()
     assert r.status.startswith('406')
Example #4
0
def _best_dispatcher_error_response(reason):
    """ Create an HTTP response for a _best_dispatcher failure. """
    if reason == 406:
        return http.not_acceptable([('Content-Type', 'text/plain')], \
                                    '406 Not Acceptable')
    elif reason == 415:
        return http.unsupported_media_type([('Content-Type', 'text/plain')], \
                                           '415 Unsupported Media Type')
Example #5
0
 def test_not_acceptable(self):
     r = http.not_acceptable([('Content-Type', 'text/plain')],
                             '406 Not Acceptable')
     assert r.status.startswith('406')
     assert r.headers['Content-Type'] == 'text/plain'
     assert '406 Not Acceptable' in r.body
     exc = http.NotAcceptableError([('Content-Type', 'text/plain')],
                                   '406 Not Acceptable')
     r = exc.make_response()
     assert r.status.startswith('406')
Example #6
0
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No dispatchers for method, send 405 with list of allowed methods.
     if dispatchers is None:
         return http.method_not_allowed(", ".join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         return _dispatch(request, match, lambda r: callable(self, r))
     # No match, send 406
     return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
Example #7
0
 def __call__(self, request):
     # Extract annotations.
     method = getattr(self, _RESTISH_METHOD)
     match = getattr(self, _RESTISH_MATCH)
     # Check for correct method.
     if request.method != method:
         return http.method_not_allowed([method])
     # Look for a dispatcher.
     dispatcher = _best_dispatcher([(self.func, match)], request)
     if dispatcher is not None:
         return _dispatch(request, match, self.func)
     # No dispatcher.
     return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
Example #8
0
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No normal dispatchers for method found,
     if dispatchers is None:
         # Looking for a magic dispatcher
         dispatchers = self.request_dispatchers.get(ALL.method)
         # No magic dispatchers found either,
         # send 405 with list of allowed methods.
         if dispatchers is None:
             return http.method_not_allowed(', '.join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         return _dispatch(request, match, lambda r: callable(self, r))
     # No match, send 406
     return http.not_acceptable([('Content-Type', 'text/plain')], \
                                '406 Not Acceptable')
Example #9
0
def _best_dispatcher_error_response(reason):
    """ Create an HTTP response for a _best_dispatcher failure. """
    if reason == 406:
        return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
    elif reason == 415:
        return http.unsupported_media_type([("Content-Type", "text/plain")], "415 Unsupported Media Type")