Comparison

plugins/mod_http_files.lua @ 7491:491975f5d383

mod_http_files: Send larger files using new file handle API
author Kim Alvefur <zash@zash.se>
date Mon, 11 Jul 2016 12:20:25 +0200
parent 7490:b75b08af7a78
child 7977:01d6298de991
comparison
equal deleted inserted replaced
7490:b75b08af7a78 7491:491975f5d383
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 cache_size = module:get_option_number("http_files_cache_size", 128);
21 local cache_max_file_size = module:get_option_number("http_files_cache_max_file_size", 4096);
21 local dir_indices = module:get_option("http_index_files", { "index.html", "index.htm" }); 22 local dir_indices = module:get_option("http_index_files", { "index.html", "index.htm" });
22 local directory_index = module:get_option_boolean("http_dir_listing"); 23 local directory_index = module:get_option_boolean("http_dir_listing");
23 24
24 local mime_map = module:shared("/*/http_files/mime").types; 25 local mime_map = module:shared("/*/http_files/mime").types;
25 if not mime_map then 26 if not mime_map then
146 cache[orig_path] = { data = data, content_type = mime_map.html; etag = etag; }; 147 cache[orig_path] = { data = data, content_type = mime_map.html; etag = etag; };
147 response_headers.content_type = mime_map.html; 148 response_headers.content_type = mime_map.html;
148 149
149 else 150 else
150 local f, err = open(full_path, "rb"); 151 local f, err = open(full_path, "rb");
151 if f then 152 if not f then
152 data, err = f:read("*a"); 153 module:log("debug", "Could not open %s. Error was %s", full_path, err);
153 f:close();
154 end
155 if not data then
156 module:log("debug", "Could not open or read %s. Error was %s", full_path, err);
157 return 403; 154 return 403;
158 end 155 end
159 local ext = full_path:match("%.([^./]+)$"); 156 local ext = full_path:match("%.([^./]+)$");
160 local content_type = ext and mime_map[ext]; 157 local content_type = ext and mime_map[ext];
158 response_headers.content_type = content_type;
159 if attr.size > cache_max_file_size then
160 response_headers.content_length = attr.size;
161 module:log("debug", "%d > cache_max_file_size", attr.size);
162 return response:send_file(f);
163 else
164 data = f:read("*a");
165 f:close();
166 end
161 cache:set(orig_path, { data = data; content_type = content_type; etag = etag }); 167 cache:set(orig_path, { data = data; content_type = content_type; etag = etag });
162 response_headers.content_type = content_type;
163 end 168 end
164 169
165 return response:send(data); 170 return response:send(data);
166 end 171 end
167 172