Comparison

plugins/mod_http_files.lua @ 5237:b1038f449e15

mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
author Kim Alvefur <zash@zash.se>
date Tue, 11 Dec 2012 23:21:25 +0100
parent 5236:8d116a0cdacd
child 5238:0cc0359d8c39
comparison
equal deleted inserted replaced
5236:8d116a0cdacd 5237:b1038f449e15
14 local stat = lfs.attributes; 14 local stat = lfs.attributes;
15 15
16 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files")); 16 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
17 local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" }); 17 local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" });
18 18
19 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) 19 local mime_map = module:shared("mime").types;
20 local mime_map = { 20 if not mime_map then
21 html = "text/html"; 21 mime_map = {
22 htm = "text/html"; 22 html = "text/html", htm = "text/html",
23 xml = "text/xml"; 23 xml = "application/xml",
24 xsl = "text/xml"; 24 txt = "text/plain",
25 txt = "text/plain; charset=utf-8"; 25 css = "text/css",
26 js = "text/javascript"; 26 js = "application/javascript",
27 css = "text/css"; 27 png = "image/png",
28 }; 28 gif = "image/gif",
29 jpeg = "image/jpeg", jpg = "image/jpeg",
30 svg = "image/svg+xml",
31 };
32 module:shared("mime").types = mime_map;
33
34 local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r");
35 if mime_types then
36 local mime_data = mime_types:read("*a");
37 mime_types:close();
38 setmetatable(mime_map, {
39 __index = function(t, ext)
40 local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
41 t[ext] = typ;
42 return typ;
43 end
44 });
45 end
46 end
29 47
30 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to. 48 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.
31 49
32 function serve_file(event, path) 50 function serve_file(event, path)
33 local request, response = event.request, event.response; 51 local request, response = event.request, event.response;