Comparison

plugins/mod_http_files.lua @ 5238:0cc0359d8c39

mod_http_files: Generate simple directory index.
author Kim Alvefur <zash@zash.se>
date Tue, 11 Dec 2012 23:40:30 +0100
parent 5237:b1038f449e15
child 5247:9257f4c47ffa
comparison
equal deleted inserted replaced
5237:b1038f449e15 5238:0cc0359d8c39
13 local open = io.open; 13 local open = io.open;
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 local show_file_list = module:get_option_boolean("http_files_show_list");
18 19
19 local mime_map = module:shared("mime").types; 20 local mime_map = module:shared("mime").types;
20 if not mime_map then 21 if not mime_map then
21 mime_map = { 22 mime_map = {
22 html = "text/html", htm = "text/html", 23 html = "text/html", htm = "text/html",
77 if stat(full_path..dir_indices[i], "mode") == "file" then 78 if stat(full_path..dir_indices[i], "mode") == "file" then
78 return serve_file(event, path..dir_indices[i]); 79 return serve_file(event, path..dir_indices[i]);
79 end 80 end
80 end 81 end
81 82
82 -- TODO File listing 83 if not show_file_list then
83 return 403; 84 return 403;
85 else
86 local html = require"util.stanza".stanza("html")
87 :tag("head"):tag("title"):text(path):up()
88 :tag("meta", { charset="utf-8" }):up()
89 :up()
90 :tag("body"):tag("h1"):text(path):up()
91 :tag("ul");
92 for file in lfs.dir(full_path) do
93 if file:sub(1,1) ~= "." then
94 local attr = stat(full_path..file) or {};
95 html:tag("li", { class = attr.mode })
96 :tag("a", { href = file }):text(file)
97 :up():up();
98 end
99 end
100 data = "<!DOCTYPE html>\n"..tostring(html);
101 cache[path] = { data = html, content_type = mime_map.html; hits = 0 };
102 response.headers.content_type = mime_map.html;
103 end
104
84 else 105 else
85 local f = open(full_path, "rb"); 106 local f = open(full_path, "rb");
86 data = f and f:read("*a"); 107 data = f and f:read("*a");
87 f:close(); 108 f:close();
88 if not data then 109 if not data then