Comparison

plugins/mod_http.lua @ 10315:d4c538a7d655

mod_http: Unhook CORS related event handlers Prevents CORS related handlers from being left over on reload. BC: `mod_http.apps[app_name][event_name]` is now a table instead of the main handler function.
author Kim Alvefur <zash@zash.se>
date Thu, 10 Oct 2019 20:46:27 +0200
parent 10258:4ff2f14f9ac7
child 10409:abfc05495d8b
comparison
equal deleted inserted replaced
10314:bbc879eab1bf 10315:d4c538a7d655
152 module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1); 152 module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1);
153 elseif event_name:sub(-1, -1) == "/" then 153 elseif event_name:sub(-1, -1) == "/" then
154 module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1); 154 module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1);
155 end 155 end
156 if not app_handlers[event_name] then 156 if not app_handlers[event_name] then
157 app_handlers[event_name] = handler; 157 app_handlers[event_name] = {
158 main = handler;
159 cors = cors_handler;
160 options = options_handler;
161 };
158 module:hook_object_event(server, event_name, handler); 162 module:hook_object_event(server, event_name, handler);
159 module:hook_object_event(server, event_name, cors_handler, 1); 163 module:hook_object_event(server, event_name, cors_handler, 1);
160 module:hook_object_event(server, options_event_name, options_handler, -1); 164 module:hook_object_event(server, options_event_name, options_handler, -1);
161 else 165 else
162 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name); 166 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
174 end 178 end
175 179
176 local function http_app_removed(event) 180 local function http_app_removed(event)
177 local app_handlers = apps[event.item.name]; 181 local app_handlers = apps[event.item.name];
178 apps[event.item.name] = nil; 182 apps[event.item.name] = nil;
179 for event_name, handler in pairs(app_handlers) do 183 for event_name, handlers in pairs(app_handlers) do
180 module:unhook_object_event(server, event_name, handler); 184 module:unhook_object_event(server, event_name, handlers.main);
185 module:unhook_object_event(server, event_name, handlers.cors);
186 local options_event_name = event_name:gsub("^%S+", "OPTIONS");
187 module:unhook_object_event(server, options_event_name, handlers.options);
181 end 188 end
182 end 189 end
183 190
184 module:handle_items("http-provider", http_app_added, http_app_removed); 191 module:handle_items("http-provider", http_app_added, http_app_removed);
185 192