forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver-geterrorpage.lua
More file actions
58 lines (48 loc) · 1.49 KB
/
httpserver-geterrorpage.lua
File metadata and controls
58 lines (48 loc) · 1.49 KB
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
-- httpserver-geterrorpage.lc
-- Part of nodemcu-httpserver, knows how to find an error page.
-- Author: Gregor Hartmann
return function(connection, req, code, header)
local function getErrorHandler(code, ext)
local filename = "error"
if (code) then
filename = filename .. "-" .. code
end
if (ext) then
filename = filename .. "-" .. ext
end
-- TODO extend for arbitrary file extensions
filename = filename..".html"
-- print("looking for "..filename)
-- TODO return filename and extension as we know them both here
if (file.exists(filename)) then
return filename
else
return nil
end
end
local uri = req.uri
req.originalUri = req.uri
req.uri = {}
req.code = code
--print("uri: ", uri or "nil")
local errorhandler = getErrorHandler(code,uri.ext)
or getErrorHandler(uri.ext)
or getErrorHandler(code)
or getErrorHandler()
req.method = "GET"
req.methodIsValid = true
if (header) then
req.headers = req.headers or {}
table.insert(req.headers, header)
end
-- TODO: extend for arbitrary file extensions
if (errorhandler) then
req.uri.ext = "html"
else
req.uri.ext = "lc"
errorhandler = "httpserver-error.lc"
end
req.uri.file = errorhandler
local port, ip = connection:getpeer()
print(ip .. ":" .. port, "Error " .. code .. " - Using errorhandler " .. errorhandler)
end