s2smanager, hostmanager: Make dialback secrets per-host
[prosody.git] / net / httpserver.lua
index 654025bab145190caca2f7f7678c2b9e9dd7679b..dec13a0bafebc6df8f80e3e52780267e38ff137c 100644 (file)
@@ -23,9 +23,6 @@ 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"
@@ -42,42 +39,35 @@ local function send_response(request, response)
        if response.body then
                local body = tostring(response.body);
                log("debug", "Sending response to %s", request.id);
-               resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
+               resp = { "HTTP/1.0 "..(response.status or "200 OK").."\r\n" };
                local h = response.headers;
                if h then
                        for k, v in pairs(h) do
-                               t_insert(resp, k);
-                               t_insert(resp, ": ");
-                               t_insert(resp, v);
-                               t_insert(resp, "\r\n");
+                               t_insert(resp, k..": "..v.."\r\n");
                        end
                end
                if not (h and h["Content-Length"]) then
-                       t_insert(resp, "Content-Length: ");
-                       t_insert(resp, #body);
-                       t_insert(resp, "\r\n");
+                       t_insert(resp, "Content-Length: "..#body.."\r\n");
                end
                t_insert(resp, "\r\n");
                
                if request.method ~= "HEAD" then
                        t_insert(resp, body);
                end
+               request.write(t_concat(resp));
        else
                -- Response we have is just a string (the body)
                log("debug", "Sending 200 response to %s", request.id or "<none>");
                
-               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");
+               local resp = "HTTP/1.0 200 OK\r\n"
+                       .. "Connection: close\r\n"
+                       .. "Content-Type: text/html\r\n"
+                       .. "Content-Length: "..#response.."\r\n"
+                       .. "\r\n"
+                       .. response;
                
-               t_insert(resp, response);
+               request.write(resp);
        end
-       request.write(t_concat(resp));
        if not request.stayopen then
                request:destroy();
        end
@@ -188,7 +178,7 @@ local function request_reader(request, data, startpos)
                
                request.url = url_parse(request.path);
                
-               log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport());
+               log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
                
                if request.onlystatus then
                        if not call_callback(request) then
@@ -206,7 +196,7 @@ end
 
 -- The default handler for requests
 default_handler = function (method, body, request)
-       log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport());
+       log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
        return { status = "404 Not Found", 
                        headers = { ["Content-Type"] = "text/html" },
                        body = "<html><head><title>Page Not Found</title></head><body>Not here :(</body></html>" };
@@ -234,7 +224,7 @@ function destroy_request(request)
                else
                        log("debug", "Request has no destroy callback");
                end
-               request.handler.close()
+               request.handler:close()
                if request.conn then
                        listener.ondisconnect(request.handler, "closed");
                end
@@ -282,6 +272,7 @@ function new_from_config(ports, handle_request, default_options)
                if ssl then
                        ssl.mode = "server";
                        ssl.protocol = "sslv23";
+                       ssl.options = "no_sslv2";
                end
                
                new{ port = port, interface = interface,