httpserver-error.lua 980 B

123456789101112131415161718192021
  1. -- httpserver-error.lua
  2. -- Part of nodemcu-httpserver, handles sending error pages to client.
  3. -- Author: Marcos Kirsch
  4. local function getHTTPStatusString(code)
  5. if code == 404 then return "Not Found" end
  6. if code == 400 then return "Bad Request" end
  7. if code == 501 then return "Not Implemented" end
  8. return "Unknown HTTP status"
  9. end
  10. local function sendHeader(connection, code, codeString, mimeType)
  11. connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n")
  12. end
  13. return function (connection, args)
  14. errorString = getHTTPStatusString(args.code)
  15. print("Error: " .. args.code .. ": " .. errorString)
  16. sendHeader(connection, args.code, errorString, "text/html")
  17. connection:send("<html><head><title>" .. args.code .. " - " .. errorString .. "</title></head><body><h1>" .. args.code .. " - " .. errorString .. "</h1></body></html>\r\n")
  18. end