Mercurial > prosody-hg
comparison plugins/mod_http_files.lua @ 4716:6eeb142a8073
mod_http_files, net.http.parser: Move path normalization to net.http.parser so that all modules can benefit
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 26 Apr 2012 16:48:16 +0100 |
| parents | 684ea42ca77a |
| children | 1138fd3d5846 |
comparison
equal
deleted
inserted
replaced
| 4715:4d6ebe54671e | 4716:6eeb142a8073 |
|---|---|
| 23 txt = "text/plain; charset=utf-8"; | 23 txt = "text/plain; charset=utf-8"; |
| 24 js = "text/javascript"; | 24 js = "text/javascript"; |
| 25 css = "text/css"; | 25 css = "text/css"; |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 local function preprocess_path(path) | |
| 29 if path:sub(1,1) ~= "/" then | |
| 30 path = "/"..path; | |
| 31 end | |
| 32 local level = 0; | |
| 33 for component in path:gmatch("([^/]+)/") do | |
| 34 if component == ".." then | |
| 35 level = level - 1; | |
| 36 elseif component ~= "." then | |
| 37 level = level + 1; | |
| 38 end | |
| 39 if level < 0 then | |
| 40 return nil; | |
| 41 end | |
| 42 end | |
| 43 return path; | |
| 44 end | |
| 45 | |
| 46 function serve_file(event, path) | 28 function serve_file(event, path) |
| 47 local response = event.response; | 29 local response = event.response; |
| 48 path = path and preprocess_path(path); | 30 local full_path = http_base.."/"..path; |
| 49 if not path then | |
| 50 return 400; | |
| 51 end | |
| 52 local full_path = http_base..path; | |
| 53 if stat(full_path, "mode") == "directory" then | 31 if stat(full_path, "mode") == "directory" then |
| 54 if stat(full_path.."/index.html", "mode") == "file" then | 32 if stat(full_path.."/index.html", "mode") == "file" then |
| 55 return serve_file(event, path.."/index.html"); | 33 return serve_file(event, path.."/index.html"); |
| 56 end | 34 end |
| 57 return 403; | 35 return 403; |
