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"
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");
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+$")
end
request.handler.close()
if request.conn then
- listener.disconnect(request.conn, "closed");
+ listener.ondisconnect(request.handler, "closed");
end
end
end
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