c0d9b2ec4130a27d9b31905844f186f83ba27f24
[prosody.git] / plugins / mod_http_files.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 module:depends("http");
10 local lfs = require "lfs";
11
12 local os_date = os.date;
13 local open = io.open;
14 local stat = lfs.attributes;
15
16 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
17 local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" });
18 local show_file_list = module:get_option_boolean("http_files_show_list");
19
20 local mime_map = module:shared("mime").types;
21 if not mime_map then
22         mime_map = {
23                 html = "text/html", htm = "text/html",
24                 xml = "application/xml",
25                 txt = "text/plain",
26                 css = "text/css",
27                 js = "application/javascript",
28                 png = "image/png",
29                 gif = "image/gif",
30                 jpeg = "image/jpeg", jpg = "image/jpeg",
31                 svg = "image/svg+xml",
32         };
33         module:shared("mime").types = mime_map;
34
35         local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r");
36         if mime_types then
37                 local mime_data = mime_types:read("*a");
38                 mime_types:close();
39                 setmetatable(mime_map, {
40                         __index = function(t, ext)
41                                 local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
42                                 t[ext] = typ;
43                                 return typ;
44                         end
45                 });
46         end
47 end
48
49 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.
50
51 function serve_file(event, path)
52         local request, response = event.request, event.response;
53         local orig_path = request.path;
54         local full_path = http_base.."/"..path;
55         local attr = stat(full_path);
56         if not attr then
57                 return 404;
58         end
59
60         local request_headers, response_headers = request.headers, response.headers;
61
62         local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification);
63         response_headers.last_modified = last_modified;
64
65         local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0);
66         response_headers.etag = etag;
67
68         local if_none_match = request_headers.if_none_match
69         local if_modified_since = request_headers.if_modified_since;
70         if etag == if_none_match
71         or (not if_none_match and last_modified == if_modified_since) then
72                 return 304;
73         end
74
75         local data = cache[path];
76         if data and data.etag == etag then
77                 response_headers.content_type = data.content_type;
78                 data = data.data;
79         elseif attr.mode == "directory" then
80                 if full_path:sub(-1) ~= "/" then
81                         response_headers.location = orig_path.."/";
82                         return 301;
83                 end
84                 for i=1,#dir_indices do
85                         if stat(full_path..dir_indices[i], "mode") == "file" then
86                                 return serve_file(event, path..dir_indices[i]);
87                         end
88                 end
89
90                 if not show_file_list then
91                         return 403;
92                 else
93                         local html = require"util.stanza".stanza("html")
94                                 :tag("head"):tag("title"):text(path):up()
95                                         :tag("meta", { charset="utf-8" }):up()
96                                 :up()
97                                 :tag("body"):tag("h1"):text(path):up()
98                                         :tag("ul");
99                         for file in lfs.dir(full_path) do
100                                 if file:sub(1,1) ~= "." then
101                                         local attr = stat(full_path..file) or {};
102                                         html:tag("li", { class = attr.mode })
103                                                 :tag("a", { href = file }):text(file)
104                                         :up():up();
105                                 end
106                         end
107                         data = "<!DOCTYPE html>\n"..tostring(html);
108                         cache[path] = { data = data, content_type = mime_map.html; etag = etag; };
109                         response_headers.content_type = mime_map.html;
110                 end
111
112         else
113                 local f, err = open(full_path, "rb");
114                 if f then
115                         data = f:read("*a");
116                         f:close();
117                 end
118                 if not data then
119                         return 403;
120                 end
121                 local ext = path:match("%.([^./]+)$");
122                 local content_type = ext and mime_map[ext];
123                 cache[path] = { data = data; content_type = content_type; etag = etag };
124                 response_headers.content_type = content_type;
125         end
126
127         return response:send(data);
128 end
129
130 module:provides("http", {
131         route = {
132                 ["GET /*"] = serve_file;
133         };
134 });
135