Software /
code /
prosody
Comparison
plugins/mod_http_files.lua @ 4700:63386138e393
mod_http_files: Return numeric error codes instead of custom error responses
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 26 Apr 2012 06:10:14 +0100 |
parent | 4672:e17fe44146b0 |
child | 4701:3ce9e1ca9c15 |
comparison
equal
deleted
inserted
replaced
4699:c66179261551 | 4700:63386138e393 |
---|---|
11 | 11 |
12 local open = io.open; | 12 local open = io.open; |
13 local stat = lfs.attributes; | 13 local stat = lfs.attributes; |
14 | 14 |
15 local http_base = module:get_option_string("http_path", "www_files"); | 15 local http_base = module:get_option_string("http_path", "www_files"); |
16 | |
17 local response_400 = "<h1>Bad Request</h1>Sorry, we didn't understand your request :("; | |
18 local response_403 = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :("; | |
19 local response_404 = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :("; | |
20 | 16 |
21 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) | 17 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) |
22 local mime_map = { | 18 local mime_map = { |
23 html = "text/html"; | 19 html = "text/html"; |
24 htm = "text/html"; | 20 htm = "text/html"; |
49 | 45 |
50 function serve_file(event, path) | 46 function serve_file(event, path) |
51 local response = event.response; | 47 local response = event.response; |
52 path = path and preprocess_path(path); | 48 path = path and preprocess_path(path); |
53 if not path then | 49 if not path then |
54 response.status = 400; | 50 return 400; |
55 return response:send(response_400); | |
56 end | 51 end |
57 local full_path = http_base..path; | 52 local full_path = http_base..path; |
58 if stat(full_path, "mode") == "directory" then | 53 if stat(full_path, "mode") == "directory" then |
59 if stat(full_path.."/index.html", "mode") == "file" then | 54 if stat(full_path.."/index.html", "mode") == "file" then |
60 return serve_file(event, path.."/index.html"); | 55 return serve_file(event, path.."/index.html"); |
61 end | 56 end |
62 response.status = 403; | 57 return 403; |
63 return response:send(response_403); | |
64 end | 58 end |
65 local f, err = open(full_path, "rb"); | 59 local f, err = open(full_path, "rb"); |
66 if not f then | 60 if not f then |
67 response.status = 404; | 61 return 404; |
68 return response:send(response_404.."<br/>"..tostring(err)); | |
69 end | 62 end |
70 local data = f:read("*a"); | 63 local data = f:read("*a"); |
71 f:close(); | 64 f:close(); |
72 if not data then | 65 if not data then |
73 response.status = 403; | 66 return 403; |
74 return response:send(response_403); | |
75 end | 67 end |
76 local ext = path:match("%.([^.]*)$"); | 68 local ext = path:match("%.([^.]*)$"); |
77 response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known | 69 response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known |
78 return response:send(data); | 70 return response:send(data); |
79 end | 71 end |