plugins/mod_http_errors.lua
author Kim Alvefur <zash@zash.se>
Sun, 21 Feb 2021 07:59:17 +0100
changeset 11399 d336b28b4002
parent 11394 226fd5d3a295
child 11407 747e8245f180
permissions -rw-r--r--
mod_http_errors: Style tweak Weird to have the background all the way to the edge of the viewport. Would be nice if this could be done without the extra span.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
module:set_global();
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local server = require "net.http.server";
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local codes = require "net.http.codes";
8367
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
     5
local xml_escape = require "util.stanza".xml_escape;
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
     6
local render = require "util.interpolation".new("%b{}", xml_escape);
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local show_private = module:get_option_boolean("http_errors_detailed", false);
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)
Matthew Wild <mwild1@gmail.com>
parents: 4711
diff changeset
     9
local always_serve = module:get_option_boolean("http_errors_always_show", true);
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)
Matthew Wild <mwild1@gmail.com>
parents: 4711
diff changeset
    10
local default_message = { module:get_option_string("http_errors_default_message", "That's all I know.") };
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local default_messages = {
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	[400] = { "What kind of request do you call that??" };
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
	[403] = { "You're not allowed to do that." };
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
	[404] = { "Whatever you were looking for is not here. %";
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
		"Where did you put it?", "It's behind you.", "Keep looking." };
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	[500] = { "% Check your error log for more info.";
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		"Gremlins.", "It broke.", "Don't look at me." };
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
};
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
local messages = setmetatable(module:get_option("http_errors_messages", {}), { __index = default_messages });
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
local html = [[
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
<!DOCTYPE html>
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
<html>
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
<head>
8367
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    26
<meta charset="utf-8">
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    27
<title>{title}</title>
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    28
<style>
11392
60a61c509d87 mod_http_errors: Minify CSS
Kim Alvefur <zash@zash.se>
parents: 11159
diff changeset
    29
body{margin-top:14%;text-align:center;background-color:#f8f8f8;font-family:sans-serif}
60a61c509d87 mod_http_errors: Minify CSS
Kim Alvefur <zash@zash.se>
parents: 11159
diff changeset
    30
h1{font-size:xx-large}
60a61c509d87 mod_http_errors: Minify CSS
Kim Alvefur <zash@zash.se>
parents: 11159
diff changeset
    31
p{font-size:x-large}
11399
d336b28b4002 mod_http_errors: Style tweak
Kim Alvefur <zash@zash.se>
parents: 11394
diff changeset
    32
p.warning>span{font-size:large;background-color:yellow}
11392
60a61c509d87 mod_http_errors: Minify CSS
Kim Alvefur <zash@zash.se>
parents: 11159
diff changeset
    33
p.extra{font-size:large;font-family:courier}
60a61c509d87 mod_http_errors: Minify CSS
Kim Alvefur <zash@zash.se>
parents: 11159
diff changeset
    34
@media(prefers-color-scheme:dark){
60a61c509d87 mod_http_errors: Minify CSS
Kim Alvefur <zash@zash.se>
parents: 11159
diff changeset
    35
body{background-color:#161616;color:#eee}
11399
d336b28b4002 mod_http_errors: Style tweak
Kim Alvefur <zash@zash.se>
parents: 11394
diff changeset
    36
p.warning>span{background-color:inherit;color:yellow}
11158
dd81a318a794 mod_http_errors: Dark theme!
Kim Alvefur <zash@zash.se>
parents: 11157
diff changeset
    37
}
8367
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    38
</style>
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
</head>
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
<body>
8367
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    41
<h1>{title}</h1>
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    42
<p>{message}</p>
11399
d336b28b4002 mod_http_errors: Style tweak
Kim Alvefur <zash@zash.se>
parents: 11394
diff changeset
    43
{warning&<p class="warning"><span>&#9888; {warning?} &#9888;</span></p>}
11159
8d692a8a8f48 mod_http_errors: Remove 'extra' element when empty
Kim Alvefur <zash@zash.se>
parents: 11158
diff changeset
    44
{extra&<p class="extra">{extra?}</p>}
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
</body>
7495
9a749cf8c1ba mod_http_errors: Add a newline after end of HTML
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
    46
</html>
9a749cf8c1ba mod_http_errors: Add a newline after end of HTML
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
    47
]];
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
local function get_page(code, extra)
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	local message = messages[code];
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)
Matthew Wild <mwild1@gmail.com>
parents: 4711
diff changeset
    51
	if always_serve or message then
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)
Matthew Wild <mwild1@gmail.com>
parents: 4711
diff changeset
    52
		message = message or default_message;
8367
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    53
		return render(html, {
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
			title = rawget(codes, code) or ("Code "..tostring(code));
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			message = message[1]:gsub("%%", function ()
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
				return message[math.random(2, math.max(#message,2))];
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
			end);
8367
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    58
			extra = extra;
f91ab40a3105 mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents: 8366
diff changeset
    59
		});
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	end
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
end
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
module:hook_object_event(server, "http-error", function (event)
8366
e2460edc2a2f mod_http_errors: Set Content-Type header to HTML (fixes #1030)
Kim Alvefur <zash@zash.se>
parents: 7495
diff changeset
    64
	if event.response then
e2460edc2a2f mod_http_errors: Set Content-Type header to HTML (fixes #1030)
Kim Alvefur <zash@zash.se>
parents: 7495
diff changeset
    65
		event.response.headers.content_type = "text/html; charset=utf-8";
e2460edc2a2f mod_http_errors: Set Content-Type header to HTML (fixes #1030)
Kim Alvefur <zash@zash.se>
parents: 7495
diff changeset
    66
	end
10578
f70c874b7936 mod_http_errors: Use text from util.errror object if included
Kim Alvefur <zash@zash.se>
parents: 10434
diff changeset
    67
	return get_page(event.code, (show_private and event.private_message) or event.message or (event.error and event.error.text));
4711
4ddf3ba0c749 mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
end);
10434
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    69
11393
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    70
module:hook("http-message", function (event)
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    71
	if event.response then
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    72
		event.response.headers.content_type = "text/html; charset=utf-8";
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    73
	end
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    74
	return render(html, event);
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    75
end);
29e7ed75ed3f mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents: 11392
diff changeset
    76
10434
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    77
module:hook_object_event(server, "http-error", function (event)
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    78
	local request, response = event.request, event.response;
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    79
	if request and response and request.path == "/" and response.status_code == 404 then
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    80
		response.headers.content_type = "text/html; charset=utf-8";
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    81
		return render(html, {
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    82
				title = "Prosody is running!";
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    83
				message = "Welcome to the XMPP world!";
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    84
			});
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    85
	end
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    86
end, 1);
46dd9df2db0c mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents: 9764
diff changeset
    87