diff --git a/README.md b/README.md index 573cd75..6287e7c 100644 --- a/README.md +++ b/README.md @@ -45,4 +45,11 @@ You can also whitelist all domains by setting the second parameter to an asteris ``` http-response lua.cors "GET,PUT,POST" "*" -``` \ No newline at end of file +``` + +To terminate OPTIONS request without sending it to the backend and return an empty response to the client add the following to the same section: + +``` +acl is_options method OPTIONS +http-request use-service lua.empty-response if is_options +``` diff --git a/lib/cors.lua b/lib/cors.lua index b131f59..328e0e2 100644 --- a/lib/cors.lua +++ b/lib/cors.lua @@ -75,4 +75,15 @@ end -- Register the actions with HAProxy core.register_action("cors", {"http-req"}, cors_request, 0) -core.register_action("cors", {"http-res"}, cors_response, 2) \ No newline at end of file +core.register_action("cors", {"http-res"}, cors_response, 2) + +-- Returns an empty response to the client +function empty_response(applet) + applet:add_header("content-length", 0) + applet:set_status(204) + applet:start_response("") + applet:send(response) +end + +-- Register the empty-response service with HAProxy +core.register_service("empty-response", "http", empty_response)