Software /
code /
prosody
Comparison
plugins/mod_http_files.lua @ 7490:b75b08af7a78
mod_http_files: Switch to use util.cache for cache
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 11 Jul 2016 12:17:59 +0200 (2016-07-11) |
parent | 7487:88a0a947e58c |
child | 7491:491975f5d383 |
comparison
equal
deleted
inserted
replaced
7489:d32406f27efd | 7490:b75b08af7a78 |
---|---|
15 local stat = lfs.attributes; | 15 local stat = lfs.attributes; |
16 local build_path = require"socket.url".build_path; | 16 local build_path = require"socket.url".build_path; |
17 local path_sep = package.config:sub(1,1); | 17 local path_sep = package.config:sub(1,1); |
18 | 18 |
19 local base_path = module:get_option_string("http_files_dir", module:get_option_string("http_path")); | 19 local base_path = module:get_option_string("http_files_dir", module:get_option_string("http_path")); |
20 local cache_size = module:get_option_number("http_files_cache_size", 128); | |
20 local dir_indices = module:get_option("http_index_files", { "index.html", "index.htm" }); | 21 local dir_indices = module:get_option("http_index_files", { "index.html", "index.htm" }); |
21 local directory_index = module:get_option_boolean("http_dir_listing"); | 22 local directory_index = module:get_option_boolean("http_dir_listing"); |
22 | 23 |
23 local mime_map = module:shared("/*/http_files/mime").types; | 24 local mime_map = module:shared("/*/http_files/mime").types; |
24 if not mime_map then | 25 if not mime_map then |
79 out[c+1] = ""; | 80 out[c+1] = ""; |
80 end | 81 end |
81 return "/"..table.concat(out, "/"); | 82 return "/"..table.concat(out, "/"); |
82 end | 83 end |
83 | 84 |
84 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to. | 85 local cache = require "util.cache".new(cache_size); |
85 | 86 |
86 function serve(opts) | 87 function serve(opts) |
87 if type(opts) ~= "table" then -- assume path string | 88 if type(opts) ~= "table" then -- assume path string |
88 opts = { path = opts }; | 89 opts = { path = opts }; |
89 end | 90 end |
117 if etag == if_none_match | 118 if etag == if_none_match |
118 or (not if_none_match and last_modified == if_modified_since) then | 119 or (not if_none_match and last_modified == if_modified_since) then |
119 return 304; | 120 return 304; |
120 end | 121 end |
121 | 122 |
122 local data = cache[orig_path]; | 123 local data = cache:get(orig_path); |
123 if data and data.etag == etag then | 124 if data and data.etag == etag then |
124 response_headers.content_type = data.content_type; | 125 response_headers.content_type = data.content_type; |
125 data = data.data; | 126 data = data.data; |
126 elseif attr.mode == "directory" and path then | 127 elseif attr.mode == "directory" and path then |
127 if full_path:sub(-1) ~= "/" then | 128 if full_path:sub(-1) ~= "/" then |
155 module:log("debug", "Could not open or read %s. Error was %s", full_path, err); | 156 module:log("debug", "Could not open or read %s. Error was %s", full_path, err); |
156 return 403; | 157 return 403; |
157 end | 158 end |
158 local ext = full_path:match("%.([^./]+)$"); | 159 local ext = full_path:match("%.([^./]+)$"); |
159 local content_type = ext and mime_map[ext]; | 160 local content_type = ext and mime_map[ext]; |
160 cache[orig_path] = { data = data; content_type = content_type; etag = etag }; | 161 cache:set(orig_path, { data = data; content_type = content_type; etag = etag }); |
161 response_headers.content_type = content_type; | 162 response_headers.content_type = content_type; |
162 end | 163 end |
163 | 164 |
164 return response:send(data); | 165 return response:send(data); |
165 end | 166 end |