Comparison

plugins/mod_http_files.lua @ 4868:550f0a5e85c5

mod_http_files: Respond with a 301 redirect for directories to append a / (fixes relative links)
author Kim Alvefur <zash@zash.se>
date Sat, 12 May 2012 02:17:08 +0200
parent 4722:1138fd3d5846
child 5232:c9bb5879e193
comparison
equal deleted inserted replaced
4863:8974a9b7363f 4868:550f0a5e85c5
25 css = "text/css"; 25 css = "text/css";
26 }; 26 };
27 27
28 function serve_file(event, path) 28 function serve_file(event, path)
29 local response = event.response; 29 local response = event.response;
30 local orig_path = event.request.path;
30 local full_path = http_base.."/"..path; 31 local full_path = http_base.."/"..path;
31 if stat(full_path, "mode") == "directory" then 32 if stat(full_path, "mode") == "directory" then
32 if stat(full_path.."/index.html", "mode") == "file" then 33 if full_path:sub(-1) ~= "/" then
33 return serve_file(event, path.."/index.html"); 34 response.headers.location = orig_path.."/";
35 return 301;
34 end 36 end
37 if stat(full_path.."index.html", "mode") == "file" then
38 return serve_file(event, path.."index.html");
39 end
40
41 -- TODO File listing
35 return 403; 42 return 403;
36 end 43 end
37 local f, err = open(full_path, "rb"); 44 local f, err = open(full_path, "rb");
38 if not f then 45 if not f then
39 module:log("warn", "Failed to open file: %s", err); 46 module:log("warn", "Failed to open file: %s", err);