mod_httpserver: Skip returning a Content-Type when not known (application/octet-strea...
[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 = 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_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
19
20 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
21 local mime_map = {
22         html = "text/html";
23         htm = "text/html";
24         xml = "text/xml";
25         xsl = "text/xml";
26         txt = "plain/text; charset=utf-8";
27         js = "text/javascript";
28         css = "text/css";
29 };
30
31 local function preprocess_path(path)
32         if path:sub(1,1) ~= "/" then
33                 path = "/"..path;
34         end
35         local level = 0;
36         for component in path:gmatch("([^/]+)/") do
37                 if component == ".." then
38                         level = level - 1;
39                 elseif component ~= "." then
40                         level = level + 1;
41                 end
42                 if level < 0 then
43                         return nil;
44                 end
45         end
46         return path;
47 end
48
49 function serve_file(path)
50         local f, err = open(http_base..path, "r");
51         if not f then return response_404; end
52         local data = f:read("*a");
53         f:close();
54         local ext = path:match("%.([^.]*)$");
55         local mime = mime_map[ext]; -- Content-Type should be nil when not known
56         module:log("warn", "ext: %s, mime: %s", ext or "(nil)", mime or "(nil)");
57         return {
58                 headers = { ["Content-Type"] = mime; };
59                 body = data;
60         };
61 end
62
63 local function handle_file_request(method, body, request)
64         local path = preprocess_path(request.url.path);
65         if not path then return response_400; end
66         path = path:gsub("^/[^/]+", ""); -- Strip /files/
67         return serve_file(path);
68 end
69
70 local function handle_default_request(method, body, request)
71         local path = preprocess_path(request.url.path);
72         if not path then return response_400; end
73         return serve_file(path);
74 end
75
76 local ports = config.get(module.host, "core", "http_ports") or { 5280 };
77 httpserver.set_default_handler(handle_default_request);
78 httpserver.new_from_config(ports, handle_file_request, { base = "files" });