httpserver-error.lua 1018 B

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