Comparison

plugins/mod_http_files.lua @ 5233:342c46e62f50

mod_http_files: Return 404 faster if file does not exist
author Kim Alvefur <zash@zash.se>
date Tue, 11 Dec 2012 22:26:41 +0100
parent 5232:c9bb5879e193
child 5234:a9f0a1becc66
comparison
equal deleted inserted replaced
5232:c9bb5879e193 5233:342c46e62f50
25 js = "text/javascript"; 25 js = "text/javascript";
26 css = "text/css"; 26 css = "text/css";
27 }; 27 };
28 28
29 function serve_file(event, path) 29 function serve_file(event, path)
30 local response = event.response; 30 local request, response = event.request, event.response;
31 local orig_path = event.request.path; 31 local orig_path = request.path;
32 local full_path = http_base.."/"..path; 32 local full_path = http_base.."/"..path;
33 if stat(full_path, "mode") == "directory" then 33 local attr = stat(full_path);
34 if not attr then
35 return 404;
36 end
37
38 if attr.mode == "directory" then
34 if full_path:sub(-1) ~= "/" then 39 if full_path:sub(-1) ~= "/" then
35 response.headers.location = orig_path.."/"; 40 response.headers.location = orig_path.."/";
36 return 301; 41 return 301;
37 end 42 end
38 for i=1,#dir_indices do 43 for i=1,#dir_indices do
42 end 47 end
43 48
44 -- TODO File listing 49 -- TODO File listing
45 return 403; 50 return 403;
46 end 51 end
52
47 local f, err = open(full_path, "rb"); 53 local f, err = open(full_path, "rb");
48 if not f then 54 if not f then
49 module:log("warn", "Failed to open file: %s", err); 55 module:log("warn", "Failed to open file: %s", err);
50 return 404; 56 return 404;
51 end 57 end