mod_welcome: Use module:hook instead of module:add_event_hook
[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 httpserver.new{ port = 5280, base = "files", handler = handle_request, ssl = false}