From 9a459d35a459290e00f54fd6be988b69bac57dd4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 22:14:55 +0100 Subject: mod_http_files: Configurable number of index files to check for --- plugins/mod_http_files.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index bdc3a011..62cb39b2 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -13,6 +13,7 @@ local open = io.open; 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 = { @@ -34,8 +35,10 @@ function serve_file(event, path) response.headers.location = orig_path.."/"; return 301; end - if stat(full_path.."index.html", "mode") == "file" then - return serve_file(event, path.."index.html"); + for i=1,#dir_indices do + if stat(full_path..dir_indices[i], "mode") == "file" then + return serve_file(event, path..dir_indices[i]); + end end -- TODO File listing -- cgit v1.2.3 From ed92fd78b69d9f4c85aac61a96c0212ca40f4ebf Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 22:26:41 +0100 Subject: mod_http_files: Return 404 faster if file does not exist --- plugins/mod_http_files.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 62cb39b2..8d3405c9 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -27,10 +27,15 @@ local mime_map = { }; function serve_file(event, path) - local response = event.response; - local orig_path = event.request.path; + local request, response = event.request, event.response; + local orig_path = request.path; local full_path = http_base.."/"..path; - if stat(full_path, "mode") == "directory" then + local attr = stat(full_path); + if not attr then + return 404; + end + + if attr.mode == "directory" then if full_path:sub(-1) ~= "/" then response.headers.location = orig_path.."/"; return 301; @@ -44,6 +49,7 @@ function serve_file(event, path) -- TODO File listing return 403; end + local f, err = open(full_path, "rb"); if not f then module:log("warn", "Failed to open file: %s", err); -- cgit v1.2.3 From 84dcecb046e820e0cd9541d6caa5e111c1ad4c8c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 22:30:13 +0100 Subject: mod_http_files: Add ETag and check If-None-Match to allow client-side cache --- plugins/mod_http_files.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 8d3405c9..0add5c86 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -35,6 +35,13 @@ function serve_file(event, path) return 404; end + + local tag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0); + response.headers.etag = tag; + if tag == request.headers.if_none_match then + return 304; + end + if attr.mode == "directory" then if full_path:sub(-1) ~= "/" then response.headers.location = orig_path.."/"; -- cgit v1.2.3 From bcfa20dc80d5b3197ac5d3d3e0fa066e9368603c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 22:30:50 +0100 Subject: mod_http_files: Add Last-Modified header --- plugins/mod_http_files.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 0add5c86..7e181ed4 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -9,6 +9,7 @@ module:depends("http"); local lfs = require "lfs"; +local os_date = os.date; local open = io.open; local stat = lfs.attributes; @@ -35,6 +36,7 @@ function serve_file(event, path) return 404; end + response.headers.last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification); local tag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0); response.headers.etag = tag; -- cgit v1.2.3 From f2336aa6266a616d449407b8bc12d23475abe840 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 23:02:06 +0100 Subject: mod_http_files: Cache data read from disk in a weak table --- plugins/mod_http_files.lua | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 7e181ed4..b049609e 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -27,6 +27,8 @@ local mime_map = { css = "text/css"; }; +local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to. + function serve_file(event, path) local request, response = event.request, event.response; local orig_path = request.path; @@ -44,7 +46,11 @@ function serve_file(event, path) return 304; end - if attr.mode == "directory" then + local data = cache[path]; + if data then + response.headers.content_type = data.content_type; + data = data.data; + elseif attr.mode == "directory" then if full_path:sub(-1) ~= "/" then response.headers.location = orig_path.."/"; return 301; @@ -57,20 +63,19 @@ function serve_file(event, path) -- TODO File listing return 403; + else + local f = open(full_path, "rb"); + data = f and f:read("*a"); + f:close(); + if not data then + return 403; + end + local ext = path:match("%.([^.]*)$"); + local content_type = mime_map[ext]; + cache[path] = { data = data; content_type = content_type; }; + response.headers.content_type = content_type; end - local f, err = open(full_path, "rb"); - if not f then - module:log("warn", "Failed to open file: %s", err); - return 404; - end - local data = f:read("*a"); - f:close(); - if not data then - return 403; - end - local ext = path:match("%.([^.]*)$"); - response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known return response:send(data); end -- cgit v1.2.3 From f0449e8428793c51d3ef5776b883a140cc84237f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 23:21:25 +0100 Subject: mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists. --- plugins/mod_http_files.lua | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'plugins/mod_http_files.lua') 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. -- cgit v1.2.3 From 8c8d3b759f1d99a687f859c2ca96bf008bcd24ec Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 11 Dec 2012 23:40:30 +0100 Subject: mod_http_files: Generate simple directory index. --- plugins/mod_http_files.lua | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index f4246fd5..764991b8 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -15,6 +15,7 @@ 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" }); +local show_file_list = module:get_option_boolean("http_files_show_list"); local mime_map = module:shared("mime").types; if not mime_map then @@ -79,8 +80,28 @@ function serve_file(event, path) end end - -- TODO File listing - return 403; + if not show_file_list then + return 403; + else + local html = require"util.stanza".stanza("html") + :tag("head"):tag("title"):text(path):up() + :tag("meta", { charset="utf-8" }):up() + :up() + :tag("body"):tag("h1"):text(path):up() + :tag("ul"); + for file in lfs.dir(full_path) do + if file:sub(1,1) ~= "." then + local attr = stat(full_path..file) or {}; + html:tag("li", { class = attr.mode }) + :tag("a", { href = file }):text(file) + :up():up(); + end + end + data = "\n"..tostring(html); + cache[path] = { data = html, content_type = mime_map.html; hits = 0 }; + response.headers.content_type = mime_map.html; + end + else local f = open(full_path, "rb"); data = f and f:read("*a"); -- cgit v1.2.3