Fix CORS when sending an OPTIONS request

This commit is contained in:
Théophile Diot 2023-05-08 12:39:09 -04:00
parent ac2e4dd645
commit 63f4e44c61
No known key found for this signature in database
GPG Key ID: E752C80DB72BB014
1 changed files with 19 additions and 7 deletions

View File

@ -1,8 +1,8 @@
local class = require "middleclass"
local plugin = require "bunkerweb.plugin"
local utils = require "bunkerweb.utils"
local class = require "middleclass"
local plugin = require "bunkerweb.plugin"
local utils = require "bunkerweb.utils"
local cors = class("cors", plugin)
local cors = class("cors", plugin)
function cors:initialize()
-- Call parent initialize
@ -31,9 +31,21 @@ function cors:header()
end
ngx.header["Content-Type"] = "text/html"
ngx.header["Content-Length"] = "0"
-- Send CORS policy with a 204 (no content) status
return self:ret(true, "sent CORS policy")
end
return cors
function cors:access()
-- Check if access is needed
if self.variables["USE_CORS"] ~= "yes" then
return self:ret(true, "service doesn't use CORS")
end
if ngx.ctx.bw.request_method ~= "OPTIONS" then
return self:ret(true, "method is not OPTIONS")
end
-- Send CORS policy with a 204 (no content) status
return self:ret(true, "sent CORS policy", ngx.HTTP_NO_CONTENT)
end
return cors