net.dns: Support for resolving AAAA records
[prosody.git] / net / httpserver.lua
index 6fd9b65eb63c008cb175612c7299a63a917a15a5..74f61c56a46182b27d654b6b17cfab77a8d6df73 100644 (file)
@@ -7,7 +7,6 @@
 --
 
 
-local socket = require "socket"
 local server = require "net.server"
 local url_parse = require "socket.url".parse;
 local httpstream_new = require "util.httpstream".new;
@@ -17,8 +16,9 @@ local connlisteners_get = require "net.connlisteners".get;
 local listener;
 
 local t_insert, t_concat = table.insert, table.concat;
-local s_match, s_gmatch = string.match, string.gmatch;
 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
+local xpcall = xpcall;
+local debug_traceback = debug.traceback;
 
 local urlencode = function (s) return s and (s:gsub("%W", function (c) return ("%%%02x"):format(c:byte()); end)); end
 
@@ -30,10 +30,6 @@ module "httpserver"
 
 local default_handler;
 
-local function expectbody(reqt)
-       return reqt.method == "POST";
-end
-
 local function send_response(request, response)
        -- Write status line
        local resp;
@@ -88,6 +84,22 @@ local function call_callback(request, err)
                callback = (request.server and request.server.handlers[base]) or default_handler;
        end
        if callback then
+               local _callback = callback;
+               function callback(method, body, request)
+                       local ok, result = xpcall(function() return _callback(method, body, request) end, debug_traceback);
+                       if ok then return result; end
+                       log("error", "Error in HTTP server handler: %s", result);
+                       -- TODO: When we support pipelining, request.destroyed
+                       -- won't be the right flag - we just want to see if there
+                       -- has been a response to this request yet.
+                       if not request.destroyed then
+                               return {
+                                       status = "500 Internal Server Error";
+                                       headers = { ["Content-Type"] = "text/plain" };
+                                       body = "There was an error processing your request. See the error log for more details.";
+                               };
+                       end
+               end
                if err then
                        log("debug", "Request error: "..err);
                        if not callback(nil, err, request) then