mod_http_files: Work with non-wildcard-routes. Key cache on the original HTTP path.
[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("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("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         local base_path = opts.path;
55         local dir_indices = opts.index_files or dir_indices;
56         local directory_index = opts.directory_index;
57         local function serve_file(event, path)
58                 local request, response = event.request, event.response;
59                 local orig_path = request.path;
60                 local full_path = base_path .. (path and "/"..path or "");
61                 local attr = stat(full_path);
62                 if not attr then
63                         return 404;
64                 end
65
66                 local request_headers, response_headers = request.headers, response.headers;
67
68                 local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification);
69                 response_headers.last_modified = last_modified;
70
71                 local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0);
72                 response_headers.etag = etag;
73
74                 local if_none_match = request_headers.if_none_match
75                 local if_modified_since = request_headers.if_modified_since;
76                 if etag == if_none_match
77                 or (not if_none_match and last_modified == if_modified_since) then
78                         return 304;
79                 end
80
81                 local data = cache[orig_path];
82                 if data and data.etag == etag then
83                         response_headers.content_type = data.content_type;
84                         data = data.data;
85                 elseif attr.mode == "directory" and path then
86                         if full_path:sub(-1) ~= "/" then
87                                 local path = { is_absolute = true, is_directory = true };
88                                 for dir in orig_path:gmatch("[^/]+") do path[#path+1]=dir; end
89                                 response_headers.location = build_path(path);
90                                 return 301;
91                         end
92                         for i=1,#dir_indices do
93                                 if stat(full_path..dir_indices[i], "mode") == "file" then
94                                         return serve_file(event, path..dir_indices[i]);
95                                 end
96                         end
97
98                         if directory_index then
99                                 data = server._events.fire_event("directory-index", { path = request.path, full_path = full_path });
100                         end
101                         if not data then
102                                 return 403;
103                         end
104                         cache[orig_path] = { data = data, content_type = mime_map.html; etag = etag; };
105                         response_headers.content_type = mime_map.html;
106
107                 else
108                         local f, err = open(full_path, "rb");
109                         if f then
110                                 data, err = f:read("*a");
111                                 f:close();
112                         end
113                         if not data then
114                                 module:log("debug", "Could not open or read %s. Error was %s", full_path, err);
115                                 return 403;
116                         end
117                         local ext = orig_path:match("%.([^./]+)$");
118                         local content_type = ext and mime_map[ext];
119                         cache[orig_path] = { data = data; content_type = content_type; etag = etag };
120                         response_headers.content_type = content_type;
121                 end
122
123                 return response:send(data);
124         end
125
126         return serve_file;
127 end
128
129
130 if base_path then
131         module:provides("http", {
132                 route = {
133                         ["GET /*"] = serve {
134                                 path = base_path;
135                                 directory_index = directory_index;
136                         }
137                 };
138         });
139 else
140         module:log("debug", "http_files_dir not set, assuming use by some other module");
141 end
142