Comparison

plugins/mod_httpserver.lua @ 2785:08e0659ba1f2

mod_httpserver: Rudimentary directory detection, return forbidden instead of causing a traceback (since commit 0325f241a26c)
author Matthew Wild <mwild1@gmail.com>
date Fri, 01 Jan 2010 21:32:23 +0000
parent 2776:bdca5025fb46
child 2923:b7049746bd29
comparison
equal deleted inserted replaced
2784:e165414a454c 2785:08e0659ba1f2
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;