mod_s2s: Handle authentication of s2sin and s2sout the same way
[prosody.git] / plugins / mod_http_files.lua
blobc357ddfc3211581ff4836b54a5c7afa2ee74006c
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 --
9 module:depends("http");
11 local open = io.open;
12 local fileserver = require"net.http.files";
14 local base_path = module:get_option_path("http_files_dir", module:get_option_path("http_path"));
15 local cache_size = module:get_option_number("http_files_cache_size", 128);
16 local cache_max_file_size = module:get_option_number("http_files_cache_max_file_size", 4096);
17 local dir_indices = module:get_option_array("http_index_files", { "index.html", "index.htm" });
18 local directory_index = module:get_option_boolean("http_dir_listing");
20 local mime_map = module:shared("/*/http_files/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",
33 module:shared("/*/http_files/mime").types = mime_map;
35 local mime_types, err = open(module:get_option_path("mime_types_file", "/etc/mime.types", "config"), "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
49 local function get_calling_module()
50 local info = debug.getinfo(3, "S");
51 if not info then return "An unknown module"; end
52 return info.source:match"mod_[^/\\.]+" or info.short_src;
53 end
55 -- COMPAT -- TODO deprecate
56 function serve(opts)
57 if type(opts) ~= "table" then -- assume path string
58 opts = { path = opts };
59 end
60 if opts.directory_index == nil then
61 opts.directory_index = directory_index;
62 end
63 if opts.mime_map == nil then
64 opts.mime_map = mime_map;
65 end
66 if opts.cache_size == nil then
67 opts.cache_size = cache_size;
68 end
69 if opts.cache_max_file_size == nil then
70 opts.cache_max_file_size = cache_max_file_size;
71 end
72 if opts.index_files == nil then
73 opts.index_files = dir_indices;
74 end
75 -- TODO Crank up to warning
76 module:log("debug", "%s should be updated to use 'net.http.files' insead of mod_http_files", get_calling_module());
77 return fileserver.serve(opts);
78 end
80 function wrap_route(routes)
81 module:log("debug", "%s should be updated to use 'net.http.files' insead of mod_http_files", get_calling_module());
82 for route,handler in pairs(routes) do
83 if type(handler) ~= "function" then
84 routes[route] = fileserver.serve(handler);
85 end
86 end
87 return routes;
88 end
90 module:provides("http", {
91 route = {
92 ["GET /*"] = fileserver.serve({
93 path = base_path;
94 directory_index = directory_index;
95 mime_map = mime_map;
96 cache_size = cache_size;
97 cache_max_file_size = cache_max_file_size;
98 index_files = dir_indices;
99 });