diff options
Diffstat (limited to 'net/http/server.lua')
-rw-r--r-- | net/http/server.lua | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/net/http/server.lua b/net/http/server.lua index 877c7f17..9b63d516 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -8,7 +8,7 @@ local os_date = os.date; local pairs = pairs; local s_upper = string.upper; local setmetatable = setmetatable; -local xpcall = xpcall; +local xpcall = require "util.xpcall".xpcall; local traceback = debug.traceback; local tostring = tostring; local cache = require "util.cache"; @@ -88,8 +88,6 @@ setmetatable(events._handlers, { }); local handle_request; -local _1, _2, _3; -local function _handle_request() return handle_request(_1, _2, _3); end local last_err; local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end @@ -107,9 +105,7 @@ function listener.onconnect(conn) while sessions[conn] and #pending > 0 do local request = t_remove(pending); --log("debug", "process_next: %s", request.path); - --handle_request(conn, request, process_next); - _1, _2, _3 = conn, request, process_next; - if not xpcall(_handle_request, _traceback_handler) then + if not xpcall(handle_request, _traceback_handler, conn, request, process_next) then conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err })); conn:close(); end @@ -217,14 +213,6 @@ function handle_request(conn, request, finish_cb) local err_code, err; if not request.path then err_code, err = 400, "Invalid path"; - elseif not hosts[host] then - if hosts[default_host] then - host = default_host; - elseif host then - err_code, err = 404, "Unknown host: "..host; - else - err_code, err = 400, "Missing or invalid 'Host' header"; - end end if err then @@ -233,10 +221,32 @@ function handle_request(conn, request, finish_cb) return; end - local event = request.method.." "..host..request.path:match("[^?]*"); + local global_event = request.method.." "..request.path:match("[^?]*"); + local payload = { request = request, response = response }; - log("debug", "Firing event: %s", event); - local result = events.fire_event(event, payload); + log("debug", "Firing event: %s", global_event); + local result = events.fire_event(global_event, payload); + if result == nil then + if not hosts[host] then + if hosts[default_host] then + host = default_host; + elseif host then + err_code, err = 404, "Unknown host: "..host; + else + err_code, err = 400, "Missing or invalid 'Host' header"; + end + end + + if err then + response.status_code = err_code; + response:send(events.fire_event("http-error", { code = err_code, message = err, response = response })); + return; + end + + local host_event = request.method.." "..host..request.path:match("[^?]*"); + log("debug", "Firing event: %s", host_event); + result = events.fire_event(host_event, payload); + end if result ~= nil then if result ~= true then local body; |