aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_http_files.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2012-12-11 23:02:06 +0100
committerKim Alvefur <zash@zash.se>2012-12-11 23:02:06 +0100
commitddaa8006bc9ec70b6cf3ebf3da5f1249edf177ea (patch)
tree7f86e2bde126c13d069eb60deb72d005a91230c0 /plugins/mod_http_files.lua
parent3860775600e5a3b6706c8c96e50d8500d578e687 (diff)
downloadprosody-ddaa8006bc9ec70b6cf3ebf3da5f1249edf177ea.tar.gz
prosody-ddaa8006bc9ec70b6cf3ebf3da5f1249edf177ea.zip
mod_http_files: Cache data read from disk in a weak table
Diffstat (limited to 'plugins/mod_http_files.lua')
-rw-r--r--plugins/mod_http_files.lua31
1 files changed, 18 insertions, 13 deletions
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