diff --git a/httpserver-error.lua b/httpserver-error.lua index e4f2be9..936482c 100644 --- a/httpserver-error.lua +++ b/httpserver-error.lua @@ -1,28 +1,8 @@ -- httpserver-error.lua -- Part of nodemcu-httpserver, handles sending error pages to client. --- Author: Marcos Kirsch +-- Author: Marcos Kirsch, Gregor Hartmann return function (connection, req, args) - - -- @TODO: would be nice to use httpserver-header.lua - local function getHeader(connection, code, errorString, extraHeaders, mimeType) - local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n" - for i, extraHeader in ipairs(extraHeaders) do - header = header .. extraHeader .. "\r\n" - end - header = header .. "connection: close\r\n\r\n" - return header - end - - args.logFunction(connection, "Error " .. args.code .. ": " .. args.errorString) - --- local port, ip = connection:getpeer() --- print("FIX", ip .. ":" .. port, "Error " .. args.code .. ": " .. args.errorString) --- port = nil --- ip = nil - - args.headers = args.headers or {} - connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html")) - connection:send("" .. args.code .. " - " .. args.errorString .. "

" .. args.code .. " - " .. args.errorString .. "

\r\n") - + local statusString = dofile("httpserver-header.lc")(connection, req.code, "html", false, req.headers) + connection:send("" .. req.code .. " - " .. statusString .. "

" .. req.code .. " - " .. statusString .. "

\r\n") end diff --git a/httpserver-header.lua b/httpserver-header.lua index 3b69cd3..8f157ba 100644 --- a/httpserver-header.lua +++ b/httpserver-header.lua @@ -2,10 +2,10 @@ -- Part of nodemcu-httpserver, knows how to send an HTTP header. -- Author: Marcos Kirsch -return function(connection, code, extension, isGzipped) +return function(connection, code, extension, isGzipped, extraHeaders) local function getHTTPStatusString(code) - local codez = { [200] = "OK", [400] = "Bad Request", [404] = "Not Found", [500] = "Internal Server Error", } + local codez = { [200] = "OK", [400] = "Bad Request", [401] = "Unauthorized", [404] = "Not Found", [405] = "Method Not Allowed", [500] = "Internal Server Error", [501] = "Not Implemented", } local myResult = codez[code] -- enforce returning valid http codes all the way throughout? if myResult then return myResult else return "Not Implemented" end @@ -19,11 +19,19 @@ return function(connection, code, extension, isGzipped) end local mimeType = getMimeType(extension) - - connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n") + local statusString = getHTTPStatusString(code) + + connection:send("HTTP/1.0 " .. code .. " " .. statusString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n") if isGzipped then connection:send("Cache-Control: private, max-age=2592000\r\nContent-Encoding: gzip\r\n") end + if (extraHeaders) then + for i, extraHeader in ipairs(extraHeaders) do + connection:send(extraHeader .. "\r\n") + end + end + connection:send("Connection: close\r\n\r\n") + return statusString end