forked from Kong/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1ae0ed
commit 6aebc40
Showing
15 changed files
with
190 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
local _M = {} | ||
|
||
local function configure_origin(ngx, conf) | ||
if conf.origin == nil then | ||
ngx.header["Access-Control-Allow-Origin"] = "*" | ||
else | ||
ngx.header["Access-Control-Allow-Origin"] = conf.origin | ||
ngx.header["Vary"] = "Origin" | ||
end | ||
end | ||
|
||
local function configure_credentials(ngx, conf) | ||
if (conf.credentials) then | ||
ngx.header["Access-Control-Allow-Credentials"] = "true" | ||
end | ||
end | ||
|
||
local function configure_headers(ngx, conf, headers) | ||
if conf.headers == nil then | ||
ngx.header["Access-Control-Allow-Headers"] = headers['access-control-request-headers'] or "" | ||
else | ||
ngx.header["Access-Control-Allow-Headers"] = conf.headers | ||
end | ||
end | ||
|
||
local function configure_exposed_headers(ngx, conf) | ||
if conf.exposed_headers ~= nil then | ||
ngx.header["Access-Control-Expose-Headers"] = conf.exposed_headers | ||
end | ||
end | ||
|
||
local function configure_methods(ngx, conf) | ||
if conf.methods ~= nil then | ||
ngx.header["Access-Control-Allow-Methods"] = conf.methods | ||
else | ||
ngx.header["Access-Control-Allow-Methods"] = "GET,HEAD,PUT,PATCH,POST,DELETE" | ||
end | ||
end | ||
|
||
local function configure_max_age(ngx, conf) | ||
if conf.max_age ~= nil then | ||
ngx.header["Access-Control-Max-Age"] = tostring(conf.max_age) | ||
end | ||
end | ||
|
||
function _M.execute(conf) | ||
local request = ngx.req | ||
local method = request.get_method() | ||
local headers = request.get_headers() | ||
|
||
configure_origin(ngx, conf) | ||
configure_credentials(ngx, conf) | ||
|
||
if method == "OPTIONS" then | ||
-- Preflight | ||
configure_headers(ngx, conf, headers) | ||
configure_methods(ngx, conf) | ||
configure_max_age(ngx, conf) | ||
|
||
if not conf.preflight_continue then | ||
utils.no_content() | ||
end | ||
else | ||
configure_exposed_headers(ngx, conf) | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local access = require "kong.plugins.cors.access" | ||
|
||
local CorsHandler = BasePlugin:extend() | ||
|
||
function CorsHandler:new() | ||
CorsHandler.super.new(self, "cors") | ||
end | ||
|
||
function CorsHandler:access(conf) | ||
CorsHandler.super.access(self) | ||
access.execute(conf) | ||
end | ||
|
||
CorsHandler.PRIORITY = 2000 | ||
|
||
return CorsHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
return { | ||
origin = { type = "string" }, | ||
headers = { type = "string" }, | ||
exposed_headers = { type = "string" }, | ||
methods = { type = "string" }, | ||
max_age = { type = "number" }, | ||
credentials = { type = "boolean", default = false }, | ||
preflight_continue = { type = "boolean", default = false } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.