diff --git a/http/node_info.lua b/http/node_info.lua
index 025b3aa..4bd2502 100644
--- a/http/node_info.lua
+++ b/http/node_info.lua
@@ -1,5 +1,5 @@
local function sendAttr(connection, attr, val)
- connection:send("
".. attr .. ": " .. val .. "
\n")
+ connection:send("".. attr .. ": " .. (val or "nil") .. "
\n")
end
return function (connection, req, args)
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
diff --git a/httpserver-request.lua b/httpserver-request.lua
index bf51d49..c6414da 100644
--- a/httpserver-request.lua
+++ b/httpserver-request.lua
@@ -112,6 +112,11 @@ return function (request)
local line = request:sub(1, e - 1)
local r = {}
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
+ if not (r.method and r.request) then
+ --print("invalid request: ")
+ --print(request)
+ return nil
+ end
r.methodIsValid = validateMethod(r.method)
r.uri = parseUri(r.request)
r.getRequestData = getRequestData(request)
diff --git a/httpserver.lua b/httpserver.lua
index 681b3af..4ed260b 100644
--- a/httpserver.lua
+++ b/httpserver.lua
@@ -117,7 +117,7 @@ return function (port)
end
if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then
- handleRequest(connection, req)
+ handleRequest(connection, req, handleError)
else
local args = {}
local fileServeFunction = dofile("httpserver-error.lc")