Skip to content

Commit 0cd6747

Browse files
committed
Updated example cors.lua file to latest version.
1 parent 586aca1 commit 0cd6747

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

example/haproxy/cors.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ end
9797
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
9898
function cors_request(txn, allowed_methods, allowed_origins, allowed_headers)
9999
local headers = txn.http:req_get_headers()
100-
local origin = headers["origin"][0]
101-
102100
local transaction_data = {}
103-
104-
if origin ~= nil then
101+
local origin = nil
102+
103+
if headers["origin"] ~= nil and headers["origin"][0] ~= nil then
105104
core.Debug("CORS: Got 'Origin' header: " .. headers["origin"][0])
106-
transaction_data["origin"] = origin
105+
origin = headers["origin"][0]
107106
end
108-
107+
108+
transaction_data["origin"] = origin
109109
transaction_data["allowed_methods"] = allowed_methods
110110
transaction_data["allowed_origins"] = allowed_origins
111111
transaction_data["allowed_headers"] = allowed_headers
@@ -124,6 +124,11 @@ end
124124
-- txn: The current transaction object that gives access to response properties.
125125
function cors_response(txn)
126126
local transaction_data = txn:get_priv()
127+
128+
if transaction_data == nil then
129+
return
130+
end
131+
127132
local origin = transaction_data["origin"]
128133
local allowed_origins = transaction_data["allowed_origins"]
129134
local allowed_methods = transaction_data["allowed_methods"]
@@ -154,4 +159,4 @@ end
154159

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

0 commit comments

Comments
 (0)