From 461367a561b4ab30561b426171cd1b569988e436 Mon Sep 17 00:00:00 2001 From: Deepak Kodihalli Date: Mon, 10 Apr 2017 07:11:38 -0500 Subject: rest_dbus: add plug-in to check content-type Add a plug-in which ensures a route's content-type header matches the expected type for that route. Change-Id: I3376a0e35aae22747e90d7c46680639dbfc1404d Signed-off-by: Deepak Kodihalli --- module/obmc/wsgi/apps/rest_dbus.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'module') diff --git a/module/obmc/wsgi/apps/rest_dbus.py b/module/obmc/wsgi/apps/rest_dbus.py index 47dacaa..7ef4afd 100644 --- a/module/obmc/wsgi/apps/rest_dbus.py +++ b/module/obmc/wsgi/apps/rest_dbus.py @@ -755,6 +755,33 @@ class JsonpPlugin(object): response_body['body'] = self.to_jsonp(response_body['body']) +class ContentCheckerPlugin(object): + ''' Ensures that a route is associated with the expected content-type + header. ''' + name = 'content_checker' + api = 2 + + class Checker: + def __init__(self, type, callback): + self.expected_type = type + self.callback = callback + self.error_str = "Expecting content type '%s', got '%s'" + + def __call__(self, *a, **kw): + if self.expected_type and \ + self.expected_type != request.content_type: + abort(415, self.error_str % (self.expected_type, + request.content_type)) + + return self.callback(*a, **kw) + + def apply(self, callback, route): + content_type = getattr( + route.get_undecorated_callback(), '_content_type', None) + + return self.Checker(content_type, callback) + + class App(Bottle): def __init__(self): super(App, self).__init__(autojson=False) @@ -772,6 +799,7 @@ class App(Bottle): json_kw = {'indent': 2, 'sort_keys': True} self.install(AuthorizationPlugin()) self.install(CorsPlugin(self)) + self.install(ContentCheckerPlugin()) self.install(JsonpPlugin(self, **json_kw)) self.install(JsonErrorsPlugin(self, **json_kw)) self.install(JsonApiResponsePlugin(self)) -- cgit v1.2.1