Comparison

plugins/mod_http.lua @ 9796:adfb29f44412

mod_http: Set up to handle OPTIONS Lower priority to allow http modules to handle it themselves, should they wish to
author Kim Alvefur <zash@zash.se>
date Fri, 18 Jan 2019 02:03:40 +0100
parent 9793:9993fd021d19
child 9797:071538a567d5
comparison
equal deleted inserted replaced
9795:02735bc82126 9796:adfb29f44412
117 local function cors_handler(event_data) 117 local function cors_handler(event_data)
118 local request, response = event_data.request, event_data.response; 118 local request, response = event_data.request, event_data.response;
119 apply_cors_headers(response, opt_methods, opt_headers, opt_max_age, request.headers.origin); 119 apply_cors_headers(response, opt_methods, opt_headers, opt_max_age, request.headers.origin);
120 end 120 end
121 121
122 local function options_handler(event_data)
123 cors_handler(event_data);
124 return "";
125 end
126
122 for key, handler in pairs(event.item.route or {}) do 127 for key, handler in pairs(event.item.route or {}) do
123 local event_name = get_http_event(host, app_path, key); 128 local event_name = get_http_event(host, app_path, key);
124 if event_name then 129 if event_name then
130 local options_event_name = event_name:gsub("^%S+", "OPTIONS");
125 if type(handler) ~= "function" then 131 if type(handler) ~= "function" then
126 local data = handler; 132 local data = handler;
127 handler = function () return data; end 133 handler = function () return data; end
128 elseif event_name:sub(-2, -1) == "/*" then 134 elseif event_name:sub(-2, -1) == "/*" then
129 local base_path_len = #event_name:match("/.+$"); 135 local base_path_len = #event_name:match("/.+$");
138 end 144 end
139 if not app_handlers[event_name] then 145 if not app_handlers[event_name] then
140 app_handlers[event_name] = handler; 146 app_handlers[event_name] = handler;
141 module:hook_object_event(server, event_name, handler); 147 module:hook_object_event(server, event_name, handler);
142 module:hook_object_event(server, event_name, cors_handler, 1); 148 module:hook_object_event(server, event_name, cors_handler, 1);
149 module:hook_object_event(server, options_event_name, options_handler, -1);
143 else 150 else
144 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name); 151 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
145 end 152 end
146 else 153 else
147 module:log("error", "Invalid route in %s, %q. See https://prosody.im/doc/developers/http#routes", app_name, key); 154 module:log("error", "Invalid route in %s, %q. See https://prosody.im/doc/developers/http#routes", app_name, key);