Merge with backout
[prosody.git] / net / http / server.lua
index 94487e5e566786ae20f9b41890e63ec45109c529..d693fb528e1073e4e436e1ae5a2f66fc072f768d 100644 (file)
@@ -26,7 +26,6 @@ local function is_wildcard_event(event)
        return event:sub(-2, -1) == "/*";
 end
 local function is_wildcard_match(wildcard_event, event)
-       log("debug", "comparing %q with %q", wildcard_event:sub(1, -2), event:sub(1, #wildcard_event-1));
        return wildcard_event:sub(1, -2) == event:sub(1, #wildcard_event-1);
 end
 
@@ -35,14 +34,14 @@ setmetatable(events._handlers, {
        __index = function (handlers, curr_event)
                if is_wildcard_event(curr_event) then return; end -- Wildcard events cannot be fired
                -- Find all handlers that could match this event, sort them
-               -- and then put the array into handlers[event]
+               -- and then put the array into handlers[curr_event] (and return it)
                local matching_handlers_set = {};
                local handlers_array = {};
                for event, handlers_set in pairs(event_map) do
                        if event == curr_event or
                        is_wildcard_event(event) and is_wildcard_match(event, curr_event) then
                                for handler, priority in pairs(handlers_set) do
-                                       matching_handlers_set[handler] = { (select(2, event:gsub("/", "%1"))), priority };
+                                       matching_handlers_set[handler] = { (select(2, event:gsub("/", "%1"))), is_wildcard_event(event) and 0 or 1, priority };
                                        table.insert(handlers_array, handler);
                                end
                        end
@@ -51,7 +50,7 @@ setmetatable(events._handlers, {
                table.sort(handlers_array, function(b, a)
                        local a_score, b_score = matching_handlers_set[a], matching_handlers_set[b];
                        for i = 1, #a_score do
-                               if a ~= b then -- If equal, compare next score value
+                               if a_score[i] ~= b_score[i] then -- If equal, compare next score value
                                        return a_score[i] < b_score[i];
                                end
                        end
@@ -76,7 +75,12 @@ setmetatable(events._handlers, {
 local handle_request;
 local _1, _2, _3;
 local function _handle_request() return handle_request(_1, _2, _3); end
-local function _traceback_handler(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug.traceback()); end
+
+local last_err;
+local function _traceback_handler(err) last_err = err; log("error", "Traceback[http]: %s: %s", tostring(err), debug.traceback()); end
+events.add_handler("http-error", function (error)
+       return "Error processing request: "..codes[error.code]..". Check your error log for more information.";
+end, -1);
 
 function listener.onconnect(conn)
        local secure = conn:ssl() and true or nil;
@@ -91,7 +95,7 @@ function listener.onconnect(conn)
                        --handle_request(conn, request, process_next);
                        _1, _2, _3 = conn, request, process_next;
                        if not xpcall(_handle_request, _traceback_handler) then
-                               conn:write("HTTP/1.0 503 Internal Server Error\r\n\r\nAn error occured during the processing of this request.");
+                               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
                else
@@ -118,6 +122,11 @@ function listener.onconnect(conn)
 end
 
 function listener.ondisconnect(conn)
+       local open_response = conn._http_open_response;
+       if open_response and open_response.on_destroy then
+               open_response.finished = true;
+               open_response:on_destroy();
+       end
        sessions[conn] = nil;
 end
 
@@ -155,40 +164,62 @@ function handle_request(conn, request, finish_cb)
                send = _M.send_response;
                finish_cb = finish_cb;
        };
+       conn._http_open_response = response;
 
+       local err;
        if not request.headers.host then
+               err = "No 'Host' header";
+       elseif not request.path then
+               err = "Invalid path";
+       end
+       
+       if err then
                response.status_code = 400;
                response.headers.content_type = "text/html";
-               response:send("<html><head>400 Bad Request</head><body>400 Bad Request: No Host header.</body></html>");
+               response:send(events.fire_event("http-error", { code = 400, message = err }));
        else
-               -- TODO call handler
-               --response.headers.content_type = "text/plain";
-               --response:send("host="..(request.headers.host or "").."\npath="..request.path.."\n"..(request.body or ""));
                local host = request.headers.host;
                if host then
                        host = host:match("[^:]*"):lower();
                        local event = request.method.." "..host..request.path:match("[^?]*");
                        local payload = { request = request, response = response };
-                       --[[repeat
-                               if events.fire_event(event, payload) ~= nil then return; end
-                               event = (event:sub(-1) == "/") and event:sub(1, -1) or event:gsub("[^/]*$", "");
-                               if event:sub(-1) == "/" then
-                                       event = event:sub(1, -1);
-                               else
-                                       event = event:gsub("[^/]*$", "");
+                       --log("debug", "Firing event: %s", event);
+                       local result = events.fire_event(event, payload);
+                       if result ~= nil then
+                               if result ~= true then
+                                       local code, body = 200, "";
+                                       local result_type = type(result);
+                                       if result_type == "number" then
+                                               response.status_code = result;
+                                               if result >= 400 then
+                                                       body = events.fire_event("http-error", { code = result });
+                                               end
+                                       elseif result_type == "string" then
+                                               body = result;
+                                       elseif result_type == "table" then
+                                               body = result.body;
+                                               result.body = nil;
+                                               for k, v in pairs(result) do
+                                                       response[k] = v;
+                                               end
+                                       end
+                                       response:send(body);
                                end
-                       until not event:find("/", 1, true);]]
-                       --log("debug", "Event: %s", event);
-                       if events.fire_event(event, payload) ~= nil then return; end
-                       -- TODO try adding/stripping / at the end, but this needs to work via an HTTP redirect
-                       if events.fire_event("*", payload) ~= nil then return; end
+                               return;
+                       end
                end
 
-               -- if handler not called, fallback to legacy httpserver handlers
-               _M.legacy_handler(request, response);
+               -- if handler not called, return 404
+               response.status_code = 404;
+               response.headers.content_type = "text/html";
+               response:send(events.fire_event("http-error", { code = 404 }));
        end
 end
 function _M.send_response(response, body)
+       if response.finished then return; end
+       response.finished = true;
+       response.conn._http_open_response = nil;
+       
        local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]);
        local headers = response.headers;
        body = body or "";
@@ -202,64 +233,16 @@ function _M.send_response(response, body)
        t_insert(output, body);
 
        response.conn:write(t_concat(output));
+       if response.on_destroy then
+               response:on_destroy();
+               response.on_destroy = nil;
+       end
        if headers.connection == "Keep-Alive" then
                response:finish_cb();
        else
                response.conn:close();
        end
 end
-function _M.legacy_handler(request, response)
-       log("debug", "Invoking legacy handler");
-       local base = request.path:match("^/([^/?]+)");
-       local legacy_server = legacy_httpserver and legacy_httpserver.new.http_servers[5280];
-       local handler = legacy_server and legacy_server.handlers[base];
-       if not handler then handler = legacy_httpserver and legacy_httpserver.set_default_handler.default_handler; end
-       if handler then
-               -- add legacy properties to request object
-               request.url = { path = request.path };
-               request.handler = response.conn;
-               request.id = tostring{}:match("%x+$");
-               local headers = {};
-               for k,v in pairs(request.headers) do
-                       headers[k:gsub("_", "-")] = v;
-               end
-               request.headers = headers;
-               function request:send(resp)
-                       if self.destroyed then return; end
-                       if resp.body or resp.headers then
-                               if resp.headers then
-                                       for k,v in pairs(resp.headers) do response.headers[k] = v; end
-                               end
-                               response:send(resp.body)
-                       else
-                               response:send(resp)
-                       end
-                       self.sent = true;
-                       self:destroy();
-               end
-               function request:destroy()
-                       if self.destroyed then return; end
-                       if not self.sent then return self:send(""); end
-                       self.destroyed = true;
-                       if self.on_destroy then
-                               log("debug", "Request has destroy callback");
-                               self:on_destroy();
-                       else
-                               log("debug", "Request has no destroy callback");
-                       end
-               end
-               local r = handler(request.method, request.body, request);
-               if r ~= true then
-                       request:send(r);
-               end
-       else
-               log("debug", "No handler found");
-               response.status_code = 404;
-               response.headers.content_type = "text/html";
-               response:send("<html><head><title>404 Not Found</title></head><body>404 Not Found: No such page.</body></html>");
-       end
-end
-
 function _M.add_handler(event, handler, priority)
        events.add_handler(event, handler, priority);
 end
@@ -273,4 +256,5 @@ end
 
 _M.listener = listener;
 _M.codes = codes;
+_M._events = events;
 return _M;