Comparison

plugins/mod_http_files.lua @ 5264:700ff21a0451

mod_http_files: Work with non-wildcard-routes. Key cache on the original HTTP path.
author Kim Alvefur <zash@zash.se>
date Fri, 21 Dec 2012 20:34:40 +0100
parent 5263:736e7ec8cd2e
child 5265:cc2aed452a62
comparison
equal deleted inserted replaced
5263:736e7ec8cd2e 5264:700ff21a0451
76 if etag == if_none_match 76 if etag == if_none_match
77 or (not if_none_match and last_modified == if_modified_since) then 77 or (not if_none_match and last_modified == if_modified_since) then
78 return 304; 78 return 304;
79 end 79 end
80 80
81 local data = cache[path]; 81 local data = cache[orig_path];
82 if data and data.etag == etag then 82 if data and data.etag == etag then
83 response_headers.content_type = data.content_type; 83 response_headers.content_type = data.content_type;
84 data = data.data; 84 data = data.data;
85 elseif attr.mode == "directory" then 85 elseif attr.mode == "directory" and path then
86 if full_path:sub(-1) ~= "/" then 86 if full_path:sub(-1) ~= "/" then
87 local path = { is_absolute = true, is_directory = true }; 87 local path = { is_absolute = true, is_directory = true };
88 for dir in orig_path:gmatch("[^/]+") do path[#path+1]=dir; end 88 for dir in orig_path:gmatch("[^/]+") do path[#path+1]=dir; end
89 response_headers.location = build_path(path); 89 response_headers.location = build_path(path);
90 return 301; 90 return 301;
99 data = server._events.fire_event("directory-index", { path = request.path, full_path = full_path }); 99 data = server._events.fire_event("directory-index", { path = request.path, full_path = full_path });
100 end 100 end
101 if not data then 101 if not data then
102 return 403; 102 return 403;
103 end 103 end
104 cache[path] = { data = data, content_type = mime_map.html; etag = etag; }; 104 cache[orig_path] = { data = data, content_type = mime_map.html; etag = etag; };
105 response_headers.content_type = mime_map.html; 105 response_headers.content_type = mime_map.html;
106 106
107 else 107 else
108 local f, err = open(full_path, "rb"); 108 local f, err = open(full_path, "rb");
109 if f then 109 if f then
112 end 112 end
113 if not data then 113 if not data then
114 module:log("debug", "Could not open or read %s. Error was %s", full_path, err); 114 module:log("debug", "Could not open or read %s. Error was %s", full_path, err);
115 return 403; 115 return 403;
116 end 116 end
117 local ext = path:match("%.([^./]+)$"); 117 local ext = orig_path:match("%.([^./]+)$");
118 local content_type = ext and mime_map[ext]; 118 local content_type = ext and mime_map[ext];
119 cache[path] = { data = data; content_type = content_type; etag = etag }; 119 cache[orig_path] = { data = data; content_type = content_type; etag = etag };
120 response_headers.content_type = content_type; 120 response_headers.content_type = content_type;
121 end 121 end
122 122
123 return response:send(data); 123 return response:send(data);
124 end 124 end