Software /
code /
prosody
Annotate
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 |
rev | line source |
---|---|
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 module:depends("http"); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 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
|
5 local codes = require "net.http.codes"; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local termcolours = require "util.termcolours"; |
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> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 <meta charset="utf-8"> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 <style> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 body{ |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 margin-top:14%; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 text-align:center; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 background-color:#F8F8F8; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 font-family:sans-serif; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 } |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 h1{ |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 font-size:xx-large; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 } |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 p{ |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 font-size:x-large; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 } |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 p+p { font-size: large; font-family: courier } |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 </style> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 </head> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 <body> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 <h1>$title</h1> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 <p>$message</p> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 <p>$extra</p> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 </body> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 </html>]]; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 html = html:gsub("%s%s+", ""); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local entities = { |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 ["<"] = "<", [">"] = ">", ["&"] = "&", |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 ["'"] = "'", ["\""] = """, ["\n"] = "<br/>", |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 }; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local function tohtml(plain) |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 return (plain:gsub("[<>&'\"\n]", entities)); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 message = message or default_message; |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return (html:gsub("$(%a+)", { |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 end); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 extra = tohtml(extra or ""); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 })); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 module:hook_object_event(server, "http-error", function (event) |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 return get_page(event.code, (show_private and event.private_message) or event.message); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end); |