Fix backend processing CORS handling
Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/6a501206 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/6a501206 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/6a501206 Branch: refs/heads/master Commit: 6a501206c24244110fb6d8b7796140fc88cb11f1 Parents: 32c322a Author: Michael Joyce <[email protected]> Authored: Fri Jul 18 13:50:24 2014 -0700 Committer: Michael Joyce <[email protected]> Committed: Fri Jul 18 13:50:24 2014 -0700 ---------------------------------------------------------------------- ocw-ui/backend/processing.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/6a501206/ocw-ui/backend/processing.py ---------------------------------------------------------------------- diff --git a/ocw-ui/backend/processing.py b/ocw-ui/backend/processing.py index 2b70713..e45b9c0 100644 --- a/ocw-ui/backend/processing.py +++ b/ocw-ui/backend/processing.py @@ -39,10 +39,24 @@ import numpy as np processing_app = Bottle() -@processing_app.hook('after_request') -def enable_cors(): - ''' Allow Cross-Origin Resource Sharing for all URLs. ''' - response.headers['Access-Control-Allow-Origin'] = '*' +class EnableCors(object): + name = 'enable_cors' + api = 2 + + def apply(self, fn, context): + def _enable_cors(*args, **kwargs): + # set CORS headers + response.headers['Access-Control-Allow-Origin'] = '*' + response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' + response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' + + if request.method != 'OPTIONS': + # actual request; reply with the actual response + return fn(*args, **kwargs) + + return _enable_cors + +processing_app.install(EnableCors()) @processing_app.route('/metrics/') def retrieve_metrics(): @@ -68,7 +82,7 @@ def retrieve_metrics(): return '%s(%s)' % (request.query.callback, output) return output -@processing_app.route('/run_evaluation/', method='POST') +@processing_app.route('/run_evaluation/', method=['POST', 'OPTIONS']) def run_evaluation(): ''' Run an OCW Evaluation.
