mod_http: Allow modifying CORS header list via :provides API
authorKim Alvefur <zash@zash.se>
Mon, 30 Dec 2019 09:50:59 +0100
changeset 11401 27a22a1f141c
parent 11400 f6bb3b193277
child 11402 a1f26d17d70f
mod_http: Allow modifying CORS header list via :provides API E.g. module:provides("http", { cors = { headers = { Accept = true; Expect = false; }; }; route = { ... }; }); Case might be weird.
plugins/mod_http.lua
--- a/plugins/mod_http.lua	Mon Dec 30 09:49:28 2019 +0100
+++ b/plugins/mod_http.lua	Mon Dec 30 09:50:59 2019 +0100
@@ -129,11 +129,12 @@
 		local app_handlers = apps[app_name];
 
 		local app_methods = opt_methods;
+		local app_headers = opt_headers;
 		local app_credentials = opt_credentials;
 
 		local function cors_handler(event_data)
 			local request, response = event_data.request, event_data.response;
-			apply_cors_headers(response, app_methods, opt_headers, opt_max_age, app_credentials, request.headers.origin);
+			apply_cors_headers(response, app_methods, app_headers, opt_max_age, app_credentials, request.headers.origin);
 		end
 
 		local function options_handler(event_data)
@@ -146,6 +147,15 @@
 			if cors.credentials ~= nil then
 				app_credentials = cors.credentials;
 			end
+			if cors.headers then
+				for header, enable in pairs(cors.headers) do
+					if enable and not app_headers:contains(header) then
+						app_headers = app_headers + set.new { header };
+					elseif not enable and app_headers:contains(header) then
+						app_headers = app_headers - set.new { header };
+					end
+				end
+			end
 		end
 
 		local streaming = event.item.streaming_uploads;