Comparison

net/http/server.lua @ 9379:866cba3689f4

net.http.server: Delay host checks until after host-less event This allows handling events without any hosts enabled.
author Kim Alvefur <zash@zash.se>
date Sun, 23 Sep 2018 18:59:04 +0200
parent 9375:816591db764d
child 9562:acf74ad0b795
comparison
equal deleted inserted replaced
9378:a6f54df39624 9379:866cba3689f4
215 215
216 -- Some sanity checking 216 -- Some sanity checking
217 local err_code, err; 217 local err_code, err;
218 if not request.path then 218 if not request.path then
219 err_code, err = 400, "Invalid path"; 219 err_code, err = 400, "Invalid path";
220 elseif not hosts[host] then
221 if hosts[default_host] then
222 host = default_host;
223 elseif host then
224 err_code, err = 404, "Unknown host: "..host;
225 else
226 err_code, err = 400, "Missing or invalid 'Host' header";
227 end
228 end 220 end
229 221
230 if err then 222 if err then
231 response.status_code = err_code; 223 response.status_code = err_code;
232 response:send(events.fire_event("http-error", { code = err_code, message = err, response = response })); 224 response:send(events.fire_event("http-error", { code = err_code, message = err, response = response }));
233 return; 225 return;
234 end 226 end
235 227
236 local global_event = request.method.." "..request.path:match("[^?]*"); 228 local global_event = request.method.." "..request.path:match("[^?]*");
237 local host_event = request.method.." "..host..request.path:match("[^?]*"); 229
238 local payload = { request = request, response = response }; 230 local payload = { request = request, response = response };
239 log("debug", "Firing event: %s", global_event); 231 log("debug", "Firing event: %s", global_event);
240 local result = events.fire_event(global_event, payload); 232 local result = events.fire_event(global_event, payload);
241 if result == nil then 233 if result == nil then
234 if not hosts[host] then
235 if hosts[default_host] then
236 host = default_host;
237 elseif host then
238 err_code, err = 404, "Unknown host: "..host;
239 else
240 err_code, err = 400, "Missing or invalid 'Host' header";
241 end
242 end
243 local host_event = request.method.." "..host..request.path:match("[^?]*");
244
245 if err then
246 response.status_code = err_code;
247 response:send(events.fire_event("http-error", { code = err_code, message = err, response = response }));
248 return;
249 end
250
242 log("debug", "Firing event: %s", host_event); 251 log("debug", "Firing event: %s", host_event);
243 result = events.fire_event(host_event, payload); 252 result = events.fire_event(host_event, payload);
244 end 253 end
245 if result ~= nil then 254 if result ~= nil then
246 if result ~= true then 255 if result ~= true then