Skip to content

Commit

Permalink
Updated example cors.lua file to latest version.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickMRamirez committed Oct 23, 2020
1 parent 586aca1 commit 0cd6747
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions example/haproxy/cors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ end
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
function cors_request(txn, allowed_methods, allowed_origins, allowed_headers)
local headers = txn.http:req_get_headers()
local origin = headers["origin"][0]

local transaction_data = {}

if origin ~= nil then
local origin = nil

if headers["origin"] ~= nil and headers["origin"][0] ~= nil then
core.Debug("CORS: Got 'Origin' header: " .. headers["origin"][0])
transaction_data["origin"] = origin
origin = headers["origin"][0]
end


transaction_data["origin"] = origin
transaction_data["allowed_methods"] = allowed_methods
transaction_data["allowed_origins"] = allowed_origins
transaction_data["allowed_headers"] = allowed_headers
Expand All @@ -124,6 +124,11 @@ end
-- txn: The current transaction object that gives access to response properties.
function cors_response(txn)
local transaction_data = txn:get_priv()

if transaction_data == nil then
return
end

local origin = transaction_data["origin"]
local allowed_origins = transaction_data["allowed_origins"]
local allowed_methods = transaction_data["allowed_methods"]
Expand Down Expand Up @@ -154,4 +159,4 @@ end

-- Register the actions with HAProxy
core.register_action("cors", {"http-req"}, cors_request, 3)
core.register_action("cors", {"http-res"}, cors_response, 0)
core.register_action("cors", {"http-res"}, cors_response, 0)

0 comments on commit 0cd6747

Please sign in to comment.