httpserver-error.lua 929 B

12345678910111213141516171819
  1. -- httpserver-error.lua
  2. -- Part of nodemcu-httpserver, handles sending error pages to client.
  3. -- Author: Marcos Kirsch
  4. return function (connection, args)
  5. local function sendHeader(connection, code, errorString, extraHeaders, mimeType)
  6. connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
  7. for i, header in ipairs(extraHeaders) do
  8. connection:send(header .. "\r\n")
  9. end
  10. connection:send("connection: close\r\n\r\n")
  11. end
  12. print("Error " .. args.code .. ": " .. args.errorString)
  13. args.headers = args.headers or {}
  14. sendHeader(connection, args.code, args.errorString, args.headers, "text/html")
  15. connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
  16. end