comparison plugins/mod_http.lua @ 12791:4f69423603f2

Merge 0.12->trunk
author Matthew Wild <mwild1@gmail.com>
date Mon, 31 Oct 2022 14:32:26 +0000
parents 24b55f0e2db9
children 419e55abd285
comparison
equal deleted inserted replaced
12789:517b5702c5a1 12791:4f69423603f2
35 local opt_methods = module:get_option_set("access_control_allow_methods", { "GET", "OPTIONS" }); 35 local opt_methods = module:get_option_set("access_control_allow_methods", { "GET", "OPTIONS" });
36 local opt_headers = module:get_option_set("access_control_allow_headers", { "Content-Type" }); 36 local opt_headers = module:get_option_set("access_control_allow_headers", { "Content-Type" });
37 local opt_origins = module:get_option_set("access_control_allow_origins"); 37 local opt_origins = module:get_option_set("access_control_allow_origins");
38 local opt_credentials = module:get_option_boolean("access_control_allow_credentials", false); 38 local opt_credentials = module:get_option_boolean("access_control_allow_credentials", false);
39 local opt_max_age = module:get_option_number("access_control_max_age", 2 * 60 * 60); 39 local opt_max_age = module:get_option_number("access_control_max_age", 2 * 60 * 60);
40 local opt_default_cors = module:get_option_boolean("http_default_cors_enabled", true);
40 41
41 local function get_http_event(host, app_path, key) 42 local function get_http_event(host, app_path, key)
42 local method, path = key:match("^(%S+)%s+(.+)$"); 43 local method, path = key:match("^(%S+)%s+(.+)$");
43 if not method then -- No path specified, default to "" (base path) 44 if not method then -- No path specified, default to "" (base path)
44 method, path = key, ""; 45 method, path = key, "";
181 app_origins = nil; 182 app_origins = nil;
182 else 183 else
183 app_origins = set.new(cors.origins)._items; 184 app_origins = set.new(cors.origins)._items;
184 end 185 end
185 end 186 end
187 elseif cors.enabled == false then
188 cors = nil;
186 end 189 end
190 else
191 cors = opt_default_cors;
187 end 192 end
188 193
189 local streaming = event.item.streaming_uploads; 194 local streaming = event.item.streaming_uploads;
190 195
191 if not event.item.route then 196 if not event.item.route then
226 end 231 end
227 end 232 end
228 if not app_handlers[event_name] then 233 if not app_handlers[event_name] then
229 app_handlers[event_name] = { 234 app_handlers[event_name] = {
230 main = handler; 235 main = handler;
231 cors = cors_handler; 236 cors = cors and cors_handler;
232 options = options_handler; 237 options = cors and options_handler;
233 }; 238 };
234 module:hook_object_event(server, event_name, handler); 239 module:hook_object_event(server, event_name, handler);
235 module:hook_object_event(server, event_name, cors_handler, 1); 240 if cors then
236 module:hook_object_event(server, options_event_name, options_handler, -1); 241 module:hook_object_event(server, event_name, cors_handler, 1);
242 module:hook_object_event(server, options_event_name, options_handler, -1);
243 end
237 else 244 else
238 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name); 245 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
239 end 246 end
240 else 247 else
241 module:log("error", "Invalid route in %s, %q. See https://prosody.im/doc/developers/http#routes", app_name, key); 248 module:log("error", "Invalid route in %s, %q. See https://prosody.im/doc/developers/http#routes", app_name, key);