util.pposix: Fix return type of lc_abort to shush compiler warning
[prosody.git] / net / httpserver.lua
index 56dfe04efdb031522bdd7c9ce7b3280de08af07c..654025bab145190caca2f7f7678c2b9e9dd7679b 100644 (file)
@@ -23,6 +23,9 @@ local urlencode = function (s) return s and (s:gsub("%W", function (c) return st
 
 local log = require "util.logger".init("httpserver");
 
+-- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
+local mime_map = { html = "text/html", txt = "plain/text; charset=utf-8", js = "text/javascript" };
+
 local http_servers = {};
 
 module "httpserver"
@@ -65,6 +68,9 @@ local function send_response(request, response)
                
                resp = { "HTTP/1.0 200 OK\r\n" };
                t_insert(resp, "Connection: close\r\n");
+               t_insert(resp, "Content-Type: ");
+               t_insert(resp, mime_map[request.url.path:match("%.(%w+)")] or "application/octet-stream");
+               t_insert(resp, "\r\n");
                t_insert(resp, "Content-Length: ");
                t_insert(resp, #response);
                t_insert(resp, "\r\n\r\n");
@@ -209,8 +215,8 @@ end
 
 function new_request(handler)
        return { handler = handler, conn = handler.socket, 
-                       write = handler.write, state = "request", 
-                       server = http_servers[handler.serverport()],
+                       write = function (...) return handler:write(...); end, state = "request", 
+                       server = http_servers[handler:serverport()],
                        send = send_response,
                        destroy = destroy_request,
                        id = tostring{}:match("%x+$")
@@ -230,7 +236,7 @@ function destroy_request(request)
                end
                request.handler.close()
                if request.conn then
-                       listener.disconnect(request.conn, "closed");
+                       listener.ondisconnect(request.handler, "closed");
                end
        end
 end
@@ -252,13 +258,23 @@ function set_default_handler(handler)
        default_handler = handler;
 end
 
-function new_from_config(ports, default_base, handle_request)
+function new_from_config(ports, handle_request, default_options)
+       if type(handle_request) == "string" then -- COMPAT with old plugins
+               log("warn", "Old syntax of httpserver.new_from_config being used to register %s", handle_request);
+               handle_request, default_options = default_options, { base = handle_request };
+       end
        for _, options in ipairs(ports) do
-               local port, base, ssl, interface = 5280, default_base, false, nil;
+               local port = default_options.port or 5280;
+               local base = default_options.base;
+               local ssl = default_options.ssl or false;
+               local interface = default_options.interface;
                if type(options) == "number" then
                        port = options;
                elseif type(options) == "table" then
-                       port, base, ssl, interface = options.port or 5280, options.path or default_base, options.ssl or false, options.interface;
+                       port = options.port or port;
+                       base = options.path or base;
+                       ssl = options.ssl or ssl;
+                       interface = options.interface or interface;
                elseif type(options) == "string" then
                        base = options;
                end
@@ -268,7 +284,9 @@ function new_from_config(ports, default_base, handle_request)
                        ssl.protocol = "sslv23";
                end
                
-               new{ port = port, base = base, handler = handle_request, ssl = ssl, type = (ssl and "ssl") or "tcp" }
+               new{ port = port, interface = interface, 
+                       base = base, handler = handle_request, 
+                       ssl = ssl, type = (ssl and "ssl") or "tcp" };
        end
 end