Merge 0.9->0.10
[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 server = require"net.http.server";
11 local lfs = require "lfs";
12
13 local os_date = os.date;
14 local open = io.open;
15 local stat = lfs.attributes;
16 local build_path = require"socket.url".build_path;
17 local path_sep = package.config:sub(1,1);
18
19 local base_path = module:get_option_string("http_files_dir", module:get_option_string("http_path"));
20 local dir_indices = module:get_option("http_index_files", { "index.html", "index.htm" });
21 local directory_index = module:get_option_boolean("http_dir_listing");
22
23 local mime_map = module:shared("/*/http_files/mime").types;
24 if not mime_map then
25         mime_map = {
26                 html = "text/html", htm = "text/html",
27                 xml = "application/xml",
28                 txt = "text/plain",
29                 css = "text/css",
30                 js = "application/javascript",
31                 png = "image/png",
32                 gif = "image/gif",
33                 jpeg = "image/jpeg", jpg = "image/jpeg",
34                 svg = "image/svg+xml",
35         };
36         module:shared("/*/http_files/mime").types = mime_map;
37
38         local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r");
39         if mime_types then
40                 local mime_data = mime_types:read("*a");
41                 mime_types:close();
42                 setmetatable(mime_map, {
43                         __index = function(t, ext)
44                                 local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
45                                 t[ext] = typ;
46                                 return typ;
47                         end
48                 });
49         end
50 end
51
52 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.
53
54 function serve(opts)
55         if type(opts) ~= "table" then -- assume path string
56                 opts = { path = opts };
57         end
58         local base_path = opts.path;
59         local dir_indices = opts.index_files or dir_indices;
60         local directory_index = opts.directory_index;
61         local function serve_file(event, path)
62                 local request, response = event.request, event.response;
63                 local orig_path = request.path;
64                 local full_path = base_path .. (path and "/"..path or "");
65                 local attr = stat((full_path:gsub('%'..path_sep..'+$','')));
66                 if not attr then
67                         return 404;
68                 end
69
70                 local request_headers, response_headers = request.headers, response.headers;
71
72                 local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification);
73                 response_headers.last_modified = last_modified;
74
75                 local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0);
76                 response_headers.etag = etag;
77
78                 local if_none_match = request_headers.if_none_match
79                 local if_modified_since = request_headers.if_modified_since;
80                 if etag == if_none_match
81                 or (not if_none_match and last_modified == if_modified_since) then
82                         return 304;
83                 end
84
85                 local data = cache[orig_path];
86                 if data and data.etag == etag then
87                         response_headers.content_type = data.content_type;
88                         data = data.data;
89                 elseif attr.mode == "directory" and path then
90                         if full_path:sub(-1) ~= "/" then
91                                 local path = { is_absolute = true, is_directory = true };
92                                 for dir in orig_path:gmatch("[^/]+") do path[#path+1]=dir; end
93                                 response_headers.location = build_path(path);
94                                 return 301;
95                         end
96                         for i=1,#dir_indices do
97                                 if stat(full_path..dir_indices[i], "mode") == "file" then
98                                         return serve_file(event, path..dir_indices[i]);
99                                 end
100                         end
101
102                         if directory_index then
103                                 data = server._events.fire_event("directory-index", { path = request.path, full_path = full_path });
104                         end
105                         if not data then
106                                 return 403;
107                         end
108                         cache[orig_path] = { data = data, content_type = mime_map.html; etag = etag; };
109                         response_headers.content_type = mime_map.html;
110
111                 else
112                         local f, err = open(full_path, "rb");
113                         if f then
114                                 data, err = f:read("*a");
115                                 f:close();
116                         end
117                         if not data then
118                                 module:log("debug", "Could not open or read %s. Error was %s", full_path, err);
119                                 return 403;
120                         end
121                         local ext = full_path:match("%.([^./]+)$");
122                         local content_type = ext and mime_map[ext];
123                         cache[orig_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         return serve_file;
131 end
132
133 function wrap_route(routes)
134         for route,handler in pairs(routes) do
135                 if type(handler) ~= "function" then
136                         routes[route] = serve(handler);
137                 end
138         end
139         return routes;
140 end
141
142 if base_path then
143         module:provides("http", {
144                 route = {
145                         ["GET /*"] = serve {
146                                 path = base_path;
147                                 directory_index = directory_index;
148                         }
149                 };
150         });
151 else
152         module:log("debug", "http_files_dir not set, assuming use by some other module");
153 end
154