aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_http_files.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2012-12-11 23:21:25 +0100
committerKim Alvefur <zash@zash.se>2012-12-11 23:21:25 +0100
commitf0449e8428793c51d3ef5776b883a140cc84237f (patch)
tree25cf0e22f28addf40d5237e26d190d7f4278ac5e /plugins/mod_http_files.lua
parentf2336aa6266a616d449407b8bc12d23475abe840 (diff)
downloadprosody-f0449e8428793c51d3ef5776b883a140cc84237f.tar.gz
prosody-f0449e8428793c51d3ef5776b883a140cc84237f.zip
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Diffstat (limited to 'plugins/mod_http_files.lua')
-rw-r--r--plugins/mod_http_files.lua38
1 files changed, 28 insertions, 10 deletions
diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua
index b049609e..f4246fd5 100644
--- a/plugins/mod_http_files.lua
+++ b/plugins/mod_http_files.lua
@@ -16,16 +16,34 @@ local stat = lfs.attributes;
local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" });
--- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
-local mime_map = {
- html = "text/html";
- htm = "text/html";
- xml = "text/xml";
- xsl = "text/xml";
- txt = "text/plain; charset=utf-8";
- js = "text/javascript";
- css = "text/css";
-};
+local mime_map = module:shared("mime").types;
+if not mime_map then
+ mime_map = {
+ html = "text/html", htm = "text/html",
+ xml = "application/xml",
+ txt = "text/plain",
+ css = "text/css",
+ js = "application/javascript",
+ png = "image/png",
+ gif = "image/gif",
+ jpeg = "image/jpeg", jpg = "image/jpeg",
+ svg = "image/svg+xml",
+ };
+ module:shared("mime").types = mime_map;
+
+ local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r");
+ if mime_types then
+ local mime_data = mime_types:read("*a");
+ mime_types:close();
+ setmetatable(mime_map, {
+ __index = function(t, ext)
+ local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
+ t[ext] = typ;
+ return typ;
+ end
+ });
+ end
+end
local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.