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