httpserver-error.lua 1.2 KB

12345678910111213141516171819202122232425262728
  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. -- @TODO: would be nice to use httpserver-header.lua
  6. local function getHeader(connection, code, errorString, extraHeaders, mimeType)
  7. local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n"
  8. for i, extraHeader in ipairs(extraHeaders) do
  9. header = header .. extraHeader .. "\r\n"
  10. end
  11. header = header .. "connection: close\r\n\r\n"
  12. return header
  13. end
  14. args.logFunction(connection, "Error " .. args.code .. ": " .. args.errorString)
  15. -- local port, ip = connection:getpeer()
  16. -- print("FIX", ip .. ":" .. port, "Error " .. args.code .. ": " .. args.errorString)
  17. -- port = nil
  18. -- ip = nil
  19. args.headers = args.headers or {}
  20. connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
  21. connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
  22. end