mod_s2s: Handle authentication of s2sin and s2sout the same way
[prosody.git] / plugins / mod_http_errors.lua
blob2bb13298df615d67eaf3d712ceef3c58217331de
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
36 h1 {
37 font-size : xx-large
40 p {
41 font-size : x-large
44 p+p {
45 font-size : large;
46 font-family : courier
48 </style>
49 </head>
50 <body>
51 <h1>{title}</h1>
52 <p>{message}</p>
53 <p>{extra?}</p>
54 </body>
55 </html>
56 ]];
58 local function get_page(code, extra)
59 local message = messages[code];
60 if always_serve or message then
61 message = message or default_message;
62 return render(html, {
63 title = rawget(codes, code) or ("Code "..tostring(code));
64 message = message[1]:gsub("%%", function ()
65 return message[math.random(2, math.max(#message,2))];
66 end);
67 extra = extra;
68 });
69 end
70 end
72 module:hook_object_event(server, "http-error", function (event)
73 if event.response then
74 event.response.headers.content_type = "text/html; charset=utf-8";
75 end
76 return get_page(event.code, (show_private and event.private_message) or event.message);
77 end);