Skip to content

Commit

Permalink
Better CORS support (#3)
Browse files Browse the repository at this point in the history
allow unauthenticated OPTIONS, add default ACAH
  • Loading branch information
phanimahesh authored Sep 21, 2018
1 parent 61eca0c commit d4605a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api-umbrella/proxy/middleware/api_settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ return function(api)
deep_merge_overwrite_arrays(settings, api["settings"])
end

-- Add some defaults for OPTIONS
-- See https://github.com/apinf/platform/issues/3531
local request_method = ngx.ctx.request_method
if(request_method == "OPTIONS") then

This comment has been minimized.

Copy link
@phanimahesh

phanimahesh Oct 17, 2018

Author Member

Welp. This should have been "options" instead

settings["disable_api_key"] = true
end

-- See if there's any settings for a matching sub-url.
if api["sub_settings"] then
local request_method = ngx.ctx.request_method
Expand Down
11 changes: 11 additions & 0 deletions src/api-umbrella/proxy/middleware/rewrite_response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ local function set_via_header()
end
end

local function set_cors_headers()
local acah = ngx.header["Access-Control-Allow-Headers"]
if acah == "" or acah == nil then
ngx.header["Access-Control-Allow-Headers"] = "x-api-key"
else
ngx.header["Access-Control-Allow-Headers"] = acah .. ",x-api-key"
end
end

local function set_default_headers(settings)
if settings["_default_response_headers"] then
local existing_headers = ngx.resp.get_headers()
Expand Down Expand Up @@ -121,6 +130,8 @@ return function(settings)
set_cache_header()
set_via_header()

set_cors_headers()

if settings then
set_default_headers(settings)
set_override_headers(settings)
Expand Down

0 comments on commit d4605a6

Please sign in to comment.