Comparison

plugins/mod_http_files.lua @ 7231:c018a44b822a

Merge 0.9->0.10
author Kim Alvefur <zash@zash.se>
date Thu, 03 Mar 2016 16:05:34 +0100
parent 7061:eda0feeaf759
parent 7230:20246b139607
child 7487:88a0a947e58c
comparison
equal deleted inserted replaced
7227:c6f9d694d778 7231:c018a44b822a
54 forbidden_chars_pattern = "[/%z\001-\031\127\"*:<>?|]" 54 forbidden_chars_pattern = "[/%z\001-\031\127\"*:<>?|]"
55 end 55 end
56 56
57 local urldecode = require "util.http".urldecode; 57 local urldecode = require "util.http".urldecode;
58 function sanitize_path(path) 58 function sanitize_path(path)
59 if not path then return end
59 local out = {}; 60 local out = {};
60 61
61 local c = 0; 62 local c = 0;
62 for component in path:gmatch("([^/]+)") do 63 for component in path:gmatch("([^/]+)") do
63 component = urldecode(component); 64 component = urldecode(component);
72 elseif component ~= "." then 73 elseif component ~= "." then
73 c = c + 1; 74 c = c + 1;
74 out[c] = component; 75 out[c] = component;
75 end 76 end
76 end 77 end
78 if path:sub(-1,-1) == "/" then
79 out[c+1] = "";
80 end
77 return "/"..table.concat(out, "/"); 81 return "/"..table.concat(out, "/");
78 end 82 end
79 83
80 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to. 84 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.
81 85
86 local base_path = opts.path; 90 local base_path = opts.path;
87 local dir_indices = opts.index_files or dir_indices; 91 local dir_indices = opts.index_files or dir_indices;
88 local directory_index = opts.directory_index; 92 local directory_index = opts.directory_index;
89 local function serve_file(event, path) 93 local function serve_file(event, path)
90 local request, response = event.request, event.response; 94 local request, response = event.request, event.response;
91 path = sanitize_path(path); 95 local sanitized_path = sanitize_path(path);
92 if not path then 96 if path and not sanitized_path then
93 return 400; 97 return 400;
94 end 98 end
99 path = sanitized_path;
95 local orig_path = sanitize_path(request.path); 100 local orig_path = sanitize_path(request.path);
96 local full_path = base_path .. (path and "/"..path or ""):gsub("/", path_sep); 101 local full_path = base_path .. (path or ""):gsub("/", path_sep);
97 local attr = stat(full_path:match("^.*[^\\/]")); -- Strip trailing path separator because Windows 102 local attr = stat(full_path:match("^.*[^\\/]")); -- Strip trailing path separator because Windows
98 if not attr then 103 if not attr then
99 return 404; 104 return 404;
100 end 105 end
101 106