13 local t_concat = table.concat; |
13 local t_concat = table.concat; |
14 |
14 |
15 local http_base = config.get("*", "core", "http_path") or "www_files"; |
15 local http_base = config.get("*", "core", "http_path") or "www_files"; |
16 |
16 |
17 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; |
17 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; |
|
18 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" }; |
18 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; |
19 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; |
19 |
20 |
20 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) |
21 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) |
21 local mime_map = { |
22 local mime_map = { |
22 html = "text/html"; |
23 html = "text/html"; |
49 function serve_file(path) |
50 function serve_file(path) |
50 local f, err = open(http_base..path, "rb"); |
51 local f, err = open(http_base..path, "rb"); |
51 if not f then return response_404; end |
52 if not f then return response_404; end |
52 local data = f:read("*a"); |
53 local data = f:read("*a"); |
53 f:close(); |
54 f:close(); |
|
55 if not data then |
|
56 return response_403; |
|
57 end |
54 local ext = path:match("%.([^.]*)$"); |
58 local ext = path:match("%.([^.]*)$"); |
55 local mime = mime_map[ext]; -- Content-Type should be nil when not known |
59 local mime = mime_map[ext]; -- Content-Type should be nil when not known |
56 return { |
60 return { |
57 headers = { ["Content-Type"] = mime; }; |
61 headers = { ["Content-Type"] = mime; }; |
58 body = data; |
62 body = data; |