mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating...
[prosody.git] / plugins / mod_httpserver.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 = config.get("*", "core", "http_path") or "www_files";
16
17 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" };
18 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" };
19 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
20
21 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
22 local mime_map = {
23         html = "text/html";
24         htm = "text/html";
25         xml = "text/xml";
26         xsl = "text/xml";
27         txt = "text/plain; charset=utf-8";
28         js = "text/javascript";
29         css = "text/css";
30 };
31
32 local function preprocess_path(path)
33         if path:sub(1,1) ~= "/" then
34                 path = "/"..path;
35         end
36         local level = 0;
37         for component in path:gmatch("([^/]+)/") do
38                 if component == ".." then
39                         level = level - 1;
40                 elseif component ~= "." then
41                         level = level + 1;
42                 end
43                 if level < 0 then
44                         return nil;
45                 end
46         end
47         return path;
48 end
49
50 function serve_file(path)
51         local f, err = open(http_base..path, "rb");
52         if not f then return response_404; end
53         local data = f:read("*a");
54         f:close();
55         if not data then
56                 return response_403;
57         end
58         local ext = path:match("%.([^.]*)$");
59         local mime = mime_map[ext]; -- Content-Type should be nil when not known
60         return {
61                 headers = { ["Content-Type"] = mime; };
62                 body = data;
63         };
64 end
65
66 local function handle_file_request(method, body, request)
67         local path = preprocess_path(request.url.path);
68         if not path then return response_400; end
69         path = path:gsub("^/[^/]+", ""); -- Strip /files/
70         return serve_file(path);
71 end
72
73 local function handle_default_request(method, body, request)
74         local path = preprocess_path(request.url.path);
75         if not path then return response_400; end
76         return serve_file(path);
77 end
78
79 local function setup()
80         local ports = config.get(module.host, "core", "http_ports") or { 5280 };
81         httpserver.set_default_handler(handle_default_request);
82         httpserver.new_from_config(ports, handle_file_request, { base = "files" });
83 end
84 if prosody.start_time then -- already started
85         setup();
86 else
87         prosody.events.add_handler("server-started", setup);
88 end