Comparison

plugins/mod_http_errors.lua @ 4737:7b9e2a8c4710

mod_http_errors: Add two new config options, http_errors_always_show (show even for unknown errors) and http_errors_default_message (message for unknown errors)
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Apr 2012 23:12:30 +0100
parent 4711:4ddf3ba0c749
child 4850:3662091d303c
comparison
equal deleted inserted replaced
4736:3514338c59c3 4737:7b9e2a8c4710
4 local server = require "net.http.server"; 4 local server = require "net.http.server";
5 local codes = require "net.http.codes"; 5 local codes = require "net.http.codes";
6 local termcolours = require "util.termcolours"; 6 local termcolours = require "util.termcolours";
7 7
8 local show_private = module:get_option_boolean("http_errors_detailed", false); 8 local show_private = module:get_option_boolean("http_errors_detailed", false);
9 9 local always_serve = module:get_option_boolean("http_errors_always_show", true);
10 local default_message = { module:get_option_string("http_errors_default_message", "That's all I know.") };
10 local default_messages = { 11 local default_messages = {
11 [400] = { "What kind of request do you call that??" }; 12 [400] = { "What kind of request do you call that??" };
12 [403] = { "You're not allowed to do that." }; 13 [403] = { "You're not allowed to do that." };
13 [404] = { "Whatever you were looking for is not here. %"; 14 [404] = { "Whatever you were looking for is not here. %";
14 "Where did you put it?", "It's behind you.", "Keep looking." }; 15 "Where did you put it?", "It's behind you.", "Keep looking." };
57 58
58 end 59 end
59 60
60 local function get_page(code, extra) 61 local function get_page(code, extra)
61 local message = messages[code]; 62 local message = messages[code];
62 if message then 63 if always_serve or message then
64 message = message or default_message;
63 return (html:gsub("$(%a+)", { 65 return (html:gsub("$(%a+)", {
64 title = rawget(codes, code) or ("Code "..tostring(code)); 66 title = rawget(codes, code) or ("Code "..tostring(code));
65 message = message[1]:gsub("%%", function () 67 message = message[1]:gsub("%%", function ()
66 return message[math.random(2, math.max(#message,2))]; 68 return message[math.random(2, math.max(#message,2))];
67 end); 69 end);