Diff

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
line wrap: on
line diff
--- a/plugins/mod_http_files.lua	Tue Dec 11 22:14:55 2012 +0100
+++ b/plugins/mod_http_files.lua	Tue Dec 11 22:26:41 2012 +0100
@@ -27,10 +27,15 @@
 };
 
 function serve_file(event, path)
-	local response = event.response;
-	local orig_path = event.request.path;
+	local request, response = event.request, event.response;
+	local orig_path = request.path;
 	local full_path = http_base.."/"..path;
-	if stat(full_path, "mode") == "directory" then
+	local attr = stat(full_path);
+	if not attr then
+		return 404;
+	end
+
+	if attr.mode == "directory" then
 		if full_path:sub(-1) ~= "/" then
 			response.headers.location = orig_path.."/";
 			return 301;
@@ -44,6 +49,7 @@
 		-- TODO File listing
 		return 403;
 	end
+
 	local f, err = open(full_path, "rb");
 	if not f then
 		module:log("warn", "Failed to open file: %s", err);