forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver-error.lua
21 lines (18 loc) · 980 Bytes
/
httpserver-error.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- httpserver-error.lua
-- Part of nodemcu-httpserver, handles sending error pages to client.
-- Author: Marcos Kirsch
local function getHTTPStatusString(code)
if code == 404 then return "Not Found" end
if code == 400 then return "Bad Request" end
if code == 501 then return "Not Implemented" end
return "Unknown HTTP status"
end
local function sendHeader(connection, code, codeString, mimeType)
connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n")
end
return function (connection, args)
errorString = getHTTPStatusString(args.code)
print("Error: " .. args.code .. ": " .. errorString)
sendHeader(connection, args.code, errorString, "text/html")
connection:send("<html><head><title>" .. args.code .. " - " .. errorString .. "</title></head><body><h1>" .. args.code .. " - " .. errorString .. "</h1></body></html>\r\n")
end