This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherror-page.lua
105 lines (97 loc) · 2.49 KB
/
error-page.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
--
-- error-page.lua
--
-- A script for handling HTTP error pages in HAProxy
--
core.Alert("lua.error-page: init started")
--
-- find_config_file_path:
--
-- returns path of the main HAProxy config file, e.g. `haproxy.cfg`
--
function find_config_file_path()
local cmd_file = io.open("/proc/self/cmdline", "rb")
local cmd_line = cmd_file:read("a*")
cmd_file:close()
for s in string.gmatch(cmd_line, "%g+") do
if s == "-f" then
found = true
elseif found then
file_path = s
break
end
end
if file_path == nil then
core.Warning("lua.error-page: config file path not found")
else
core.Debug("lua.error-page: config file path: " .. file_path)
end
return file_path
end
--
-- read_error_file
--
-- reads and returns the content of an error file
--
function read_error_file(file_path)
local error_file = io.open(file_path, "r")
if error_file == nil then
core.Warning("lua.error-page: error file not found: " .. file_path)
return nil
end
local error_page = error_file:read("a*")
error_file:close()
core.Debug(string.format("lua.error-page: error page: %s [%d]", file_path, error_page:len()))
return error_page
end
--
-- read_error_pages
--
-- reads `errorfile` keywords from config file and returns a mapping between
-- status codes and error pages
--
function read_error_pages(file_path)
local config_file = io.open(file_path, "r")
local error_pages = {}
while true do
local line = config_file:read()
if line == nil then
break
end
local parts = line:gmatch("%S+")
if parts() == "errorfile" then
local error_code = parts()
local error_file = parts()
if error_code ~= nil and error_file ~= nil then
error_page = read_error_file(error_file)
if error_page ~= nil then
error_pages[error_code] = error_page
core.Debug(string.format("lua.error-page: %s page is available", error_code))
end
end
end
end
config_file:close()
return error_pages
end
--
-- error_pages
--
-- status code to error page mapping
--
error_pages = read_error_pages(find_config_file_path())
--
-- lua.error-page
--
-- the error-page action for http-response
--
core.register_action("error-page", { "http-res" }, function(txn)
local error_code = txn.sf:status()
local error_page = error_pages[error_code]
if error_page ~= nil then
txn:Debug("lua.error-page: rewrite error page: " .. error_code)
txn.res:set("")
txn.res:send(error_page)
txn:done()
end
end)