Changeset

5258:79bdd093d6ba

Merge 0.9->trunk
author Kim Alvefur <zash@zash.se>
date Fri, 21 Dec 2012 09:04:54 +0100
parents 5251:80cb52150773 (current diff) 5257:b125892e187c (diff)
children 5271:f716a3c58b4b
files
diffstat 1 files changed, 25 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_http_files.lua	Sun Dec 16 14:36:44 2012 +0100
+++ b/plugins/mod_http_files.lua	Fri Dec 21 09:04:54 2012 +0100
@@ -12,6 +12,7 @@
 local os_date = os.date;
 local open = io.open;
 local stat = lfs.attributes;
+local build_path = require"socket.url".build_path;
 
 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
 local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" });
@@ -57,24 +58,30 @@
 		return 404;
 	end
 
+	local request_headers, response_headers = request.headers, response.headers;
+
 	local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification);
-	response.headers.last_modified = last_modified;
+	response_headers.last_modified = last_modified;
 
 	local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0);
-	response.headers.etag = etag;
+	response_headers.etag = etag;
 
-	if etag == request.headers.if_none_match
-	or last_modified == request.headers.if_modified_since then
+	local if_none_match = request_headers.if_none_match
+	local if_modified_since = request_headers.if_modified_since;
+	if etag == if_none_match
+	or (not if_none_match and last_modified == if_modified_since) then
 		return 304;
 	end
 
 	local data = cache[path];
-	if data then
-		response.headers.content_type = data.content_type;
+	if data and data.etag == etag then
+		response_headers.content_type = data.content_type;
 		data = data.data;
 	elseif attr.mode == "directory" then
 		if full_path:sub(-1) ~= "/" then
-			response.headers.location = orig_path.."/";
+			local path = { is_absolute = true, is_directory = true };
+			for dir in orig_path:gmatch("[^/]+") do path[#path+1]=dir; end
+			response_headers.location = build_path(path);
 			return 301;
 		end
 		for i=1,#dir_indices do
@@ -101,21 +108,23 @@
 				end
 			end
 			data = "<!DOCTYPE html>\n"..tostring(html);
-			cache[path] = { data = data, content_type = mime_map.html; hits = 0 };
-			response.headers.content_type = mime_map.html;
+			cache[path] = { data = data, content_type = mime_map.html; etag = etag; };
+			response_headers.content_type = mime_map.html;
 		end
 
 	else
-		local f = open(full_path, "rb");
-		data = f and f:read("*a");
-		f:close();
+		local f, err = open(full_path, "rb");
+		if f then
+			data = f:read("*a");
+			f:close();
+		end
 		if not data then
 			return 403;
 		end
-		local ext = path:match("%.([^.]*)$");
-		local content_type = mime_map[ext];
-		cache[path] = { data = data; content_type = content_type; };
-		response.headers.content_type = content_type;
+		local ext = path:match("%.([^./]+)$");
+		local content_type = ext and mime_map[ext];
+		cache[path] = { data = data; content_type = content_type; etag = etag };
+		response_headers.content_type = content_type;
 	end
 
 	return response:send(data);