diff options
author | Deepak Kodihalli <dkodihal@in.ibm.com> | 2017-04-10 07:11:38 -0500 |
---|---|---|
committer | Deepak Kodihalli <dkodihal@in.ibm.com> | 2017-04-19 04:10:31 -0500 |
commit | 461367a561b4ab30561b426171cd1b569988e436 (patch) | |
tree | 2d3550c5755550886a6a316cac6ef94d8d53c544 | |
parent | 83afbaffb4c9c0d27c767a51a88d46c124cedd04 (diff) | |
download | phosphor-rest-server-461367a561b4ab30561b426171cd1b569988e436.tar.gz phosphor-rest-server-461367a561b4ab30561b426171cd1b569988e436.zip |
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 <dkodihal@in.ibm.com>
-rw-r--r-- | module/obmc/wsgi/apps/rest_dbus.py | 28 |
1 files changed, 28 insertions, 0 deletions
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)) |