From 0cd674749f98657f9a86dde2abacb4bb61eac438 Mon Sep 17 00:00:00 2001 From: Nick Ramirez Date: Thu, 22 Oct 2020 20:58:52 -0400 Subject: [PATCH] Updated example cors.lua file to latest version. --- example/haproxy/cors.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/example/haproxy/cors.lua b/example/haproxy/cors.lua index 14474d5..96f086e 100644 --- a/example/haproxy/cors.lua +++ b/example/haproxy/cors.lua @@ -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 @@ -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"] @@ -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) \ No newline at end of file +core.register_action("cors", {"http-res"}, cors_response, 0)