9797-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
9898function 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
124124-- txn: The current transaction object that gives access to response properties.
125125function 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" ]
154159
155160-- Register the actions with HAProxy
156161core .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