3 local server
= require
"net.http.server";
4 local codes
= require
"net.http.codes";
5 local xml_escape
= require
"util.stanza".xml_escape
;
6 local render
= require
"util.interpolation".new("%b{}", xml_escape
);
8 local show_private
= module
:get_option_boolean("http_errors_detailed", false);
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.") };
11 local default_messages
= {
12 [400] = { "What kind of request do you call that??" };
13 [403] = { "You're not allowed to do that." };
14 [404] = { "Whatever you were looking for is not here. %";
15 "Where did you put it?", "It's behind you.", "Keep looking." };
16 [500] = { "% Check your error log for more info.";
17 "Gremlins.", "It broke.", "Don't look at me." };
20 local messages
= setmetatable(module
:get_option("http_errors_messages", {}), { __index
= default_messages
});
26 <meta charset="utf-8">
27 <title>{title}</title>
32 background-color:#F8F8F8;
33 font-family:sans-serif;
55 local function get_page(code
, extra
)
56 local message
= messages
[code
];
57 if always_serve
or message
then
58 message
= message
or default_message
;
60 title
= rawget(codes
, code
) or ("Code "..tostring(code
));
61 message
= message
[1]:gsub("%%", function ()
62 return message
[math
.random(2, math
.max(#message
,2))];
69 module
:hook_object_event(server
, "http-error", function (event
)
70 if event
.response
then
71 event
.response
.headers
.content_type
= "text/html; charset=utf-8";
73 return get_page(event
.code
, (show_private
and event
.private_message
) or event
.message
);