From e104ed8cac86435d879cdbd21cb45c01bb026782 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(-) 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 dc08340e9f78c5f76e22b981cdaacc3ee0b30729 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(-) 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 0ff07edb453e5afd98e7236ed467034bf9fb4e33 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(-) 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 f098458b690ecc4b71ec604f81acf8191f0990f8 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(-) 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 d1880e05fd34668daa71ed14b70a9a13189d7e9f 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(-) 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 b1dd7b0be798b3f446cea7f0e4e1a4f05aea7750 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(-) 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