mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_httpserver.lua
1
2 local httpserver = require "net.httpserver";
3
4 local open = io.open;
5 local t_concat = table.concat;
6
7 local http_base = "www_files";
8
9 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
10
11 local http_path = { http_base };
12 local function handle_request(method, body, request)
13         local path = request.url.path:gsub("%.%.%/", ""):gsub("^/[^/]+", "");
14         http_path[2] = path;
15         local f, err = open(t_concat(http_path), "r");
16         if not f then return response_404; end
17         local data = f:read("*a");
18         f:close();
19         return data;
20 end
21
22 local ports = config.get(module.host, "core", "http_ports") or { 5280 };
23 for _, options in ipairs(ports) do
24         local port, base, ssl, interface = 5280, "files", false, nil;
25         if type(options) == "number" then
26                 port = options;
27         elseif type(options) == "table" then
28                 port, base, ssl, interface = options.port or 5280, options.path or "files", options.ssl or false, options.interface;
29         elseif type(options) == "string" then
30                 base = options;
31         end
32         httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl }
33 end