Merge with 0.5
[prosody.git] / plugins / mod_httpserver.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local httpserver = require "net.httpserver";
11
12 local open = io.open;
13 local t_concat = table.concat;
14
15 local http_base = "www_files";
16
17 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
18
19 local http_path = { http_base };
20 local function handle_request(method, body, request)
21         local path = request.url.path:gsub("%.%.%/", ""):gsub("^/[^/]+", "");
22         http_path[2] = path;
23         local f, err = open(t_concat(http_path), "r");
24         if not f then return response_404; end
25         local data = f:read("*a");
26         f:close();
27         return data;
28 end
29
30 local ports = config.get(module.host, "core", "http_ports") or { 5280 };
31 httpserver.new_from_config(ports, "files", handle_request);