mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_http_errors.lua
blob13473219554300ef6b0edec06d45dd188fc641f5
1 module:set_global();
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 });
22 local html = [[
23 <!DOCTYPE html>
24 <html>
25 <head>
26 <meta charset="utf-8">
27 <title>{title}</title>
28 <style>
29 body{
30 margin-top:14%;
31 text-align:center;
32 background-color:#F8F8F8;
33 font-family:sans-serif;
35 h1{
36 font-size:xx-large;
39 font-size:x-large;
41 p+p {
42 font-size:large;
43 font-family:courier;
45 </style>
46 </head>
47 <body>
48 <h1>{title}</h1>
49 <p>{message}</p>
50 <p>{extra?}</p>
51 </body>
52 </html>
53 ]];
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;
59 return render(html, {
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))];
63 end);
64 extra = extra;
65 });
66 end
67 end
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";
72 end
73 return get_page(event.code, (show_private and event.private_message) or event.message);
74 end);