From 9a2939f64a722af33e84789336a8d2d11f8eacac Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 21 Dec 2012 08:10:07 +0100 Subject: mod_http_files: No use in closing a file handle if we couldn't open it --- plugins/mod_http_files.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index d83e6f97..92c49e11 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -106,9 +106,11 @@ function serve_file(event, path) 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 -- cgit v1.2.3 From bf297afdb61032b38c060eee47f2ca3f1aa07ad3 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 21 Dec 2012 08:14:33 +0100 Subject: mod_http_files: Avoid a bunch of table lookups --- plugins/mod_http_files.lua | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 92c49e11..47423f55 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -57,24 +57,28 @@ function serve_file(event, path) 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 last_modified == if_modified_since then return 304; end local data = cache[path]; if data then - response.headers.content_type = data.content_type; + 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.."/"; + response_headers.location = orig_path.."/"; return 301; end for i=1,#dir_indices do @@ -102,7 +106,7 @@ function serve_file(event, path) end data = "\n"..tostring(html); cache[path] = { data = data, content_type = mime_map.html; hits = 0 }; - response.headers.content_type = mime_map.html; + response_headers.content_type = mime_map.html; end else @@ -117,7 +121,7 @@ function serve_file(event, path) local ext = path:match("%.([^.]*)$"); local content_type = mime_map[ext]; cache[path] = { data = data; content_type = content_type; }; - response.headers.content_type = content_type; + response_headers.content_type = content_type; end return response:send(data); -- cgit v1.2.3 From 19473b8b72ce4060039e768f3c9255c15847c055 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 21 Dec 2012 08:19:58 +0100 Subject: mod_http_files: Make sure file extensions are not nil or empty string --- plugins/mod_http_files.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 47423f55..70ef5623 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -118,8 +118,8 @@ function serve_file(event, path) if not data then return 403; end - local ext = path:match("%.([^.]*)$"); - local content_type = mime_map[ext]; + local ext = path:match("%.([^./]+)$"); + local content_type = ext and mime_map[ext]; cache[path] = { data = data; content_type = content_type; }; response_headers.content_type = content_type; end -- cgit v1.2.3 From a9937ee757b77f991fb0a091fb8173252f23580b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 21 Dec 2012 08:25:09 +0100 Subject: mod_http_files: Only serve cached data if etag is unchanged. --- plugins/mod_http_files.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 70ef5623..dea209c8 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -73,7 +73,7 @@ function serve_file(event, path) end local data = cache[path]; - if data then + if data and data.etag == etag then response_headers.content_type = data.content_type; data = data.data; elseif attr.mode == "directory" then @@ -105,7 +105,7 @@ function serve_file(event, path) end end data = "\n"..tostring(html); - cache[path] = { data = data, content_type = mime_map.html; hits = 0 }; + cache[path] = { data = data, content_type = mime_map.html; etag = etag; }; response_headers.content_type = mime_map.html; end @@ -120,7 +120,7 @@ function serve_file(event, path) end local ext = path:match("%.([^./]+)$"); local content_type = ext and mime_map[ext]; - cache[path] = { data = data; content_type = content_type; }; + cache[path] = { data = data; content_type = content_type; etag = etag }; response_headers.content_type = content_type; end -- cgit v1.2.3 From e19d7ebdde19520082aa64d930658aa0e01ea316 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 21 Dec 2012 08:27:14 +0100 Subject: mod_http_files: Only match on modification date when if-none-match is not present --- plugins/mod_http_files.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index dea209c8..c0d9b2ec 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -68,7 +68,7 @@ function serve_file(event, path) local if_none_match = request_headers.if_none_match local if_modified_since = request_headers.if_modified_since; if etag == if_none_match - or last_modified == if_modified_since then + or (not if_none_match and last_modified == if_modified_since) then return 304; end -- cgit v1.2.3 From 5c0e25da68b8fe6b4ca70d1f6b85ef5de3fc2ced Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 21 Dec 2012 09:04:02 +0100 Subject: mod_http_files: Escape paths in redirects --- plugins/mod_http_files.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index c0d9b2ec..43094eb4 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -12,6 +12,7 @@ local lfs = require "lfs"; 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" }); @@ -78,7 +79,9 @@ function serve_file(event, path) 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 -- cgit v1.2.3