-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver-error.lua
28 lines (22 loc) · 1.2 KB
/
httpserver-error.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
-- httpserver-error.lua
-- Part of nodemcu-httpserver, handles sending error pages to client.
-- Author: Marcos Kirsch
return function (connection, req, args)
-- @TODO: would be nice to use httpserver-header.lua
local function getHeader(connection, code, errorString, extraHeaders, mimeType)
local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n"
for i, extraHeader in ipairs(extraHeaders) do
header = header .. extraHeader .. "\r\n"
end
header = header .. "connection: close\r\n\r\n"
return header
end
args.logFunction(connection, "Error " .. args.code .. ": " .. args.errorString)
-- local port, ip = connection:getpeer()
-- print("FIX", ip .. ":" .. port, "Error " .. args.code .. ": " .. args.errorString)
-- port = nil
-- ip = nil
args.headers = args.headers or {}
connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
end