[fix] CORS Prefech OPTIONS

This commit is contained in:
ljf 2022-08-06 22:20:56 +02:00
parent 4239f466f8
commit 3f8f573d7e

View file

@ -728,12 +728,10 @@ class Interface:
app = Bottle(autojson=True) app = Bottle(autojson=True)
# Wrapper which sets proper header # Wrapper which sets proper header
def apiheader(callback): # Doesn't work if the HTTPResponse has been raised
def wrapper(*args, **kwargs): # like HTTP Error
def apiheader():
response.set_header("Access-Control-Allow-Origin", "*") response.set_header("Access-Control-Allow-Origin", "*")
return callback(*args, **kwargs)
return wrapper
# Attempt to retrieve and set locale # Attempt to retrieve and set locale
def api18n(callback): def api18n(callback):
@ -749,7 +747,7 @@ class Interface:
# Install plugins # Install plugins
app.install(filter_csrf) app.install(filter_csrf)
app.install(apiheader) app.add_hook("after_request", apiheader)
app.install(api18n) app.install(api18n)
actionsmapplugin = _ActionsMapPlugin(actionsmap, log_queues) actionsmapplugin = _ActionsMapPlugin(actionsmap, log_queues)
app.install(actionsmapplugin) app.install(actionsmapplugin)
@ -758,6 +756,19 @@ class Interface:
self.display = actionsmapplugin.display self.display = actionsmapplugin.display
self.prompt = actionsmapplugin.prompt self.prompt = actionsmapplugin.prompt
# Answer to prefetch OPTIONS request to make CORS working
# and our damned swagger api documentation tool
def options_prefetch_cors():
response = HTTPResponse("", 200)
response.set_header("Access-Control-Allow-Origin", "*")
response.set_header("Access-Control-Allow-Headers", "x-requested-with")
response.set_header("Allow", "GET, POST, PUT, DELETE, OPTIONS")
return response
app.route('/<:re:.*>', method="OPTIONS",
callback=options_prefetch_cors, skip=["actionsmap"])
# Append additional routes # Append additional routes
# TODO: Add optional authentication to those routes? # TODO: Add optional authentication to those routes?
for (m, p), c in routes.items(): for (m, p), c in routes.items():