Improved logging via serial.
Created a single log function that prints a standard message that includes IP and port of client. Remove or commented out other debug prints.
This commit is contained in:
parent
6efa4ce3c0
commit
2e25a63807
@ -25,6 +25,10 @@ function BufferedConnection:new(connection)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function newInstance:getpeer()
|
||||||
|
return self.connection:getpeer()
|
||||||
|
end
|
||||||
|
|
||||||
function newInstance:send(payload)
|
function newInstance:send(payload)
|
||||||
local flushThreshold = 1400
|
local flushThreshold = 1400
|
||||||
local newSize = self.size + payload:len()
|
local newSize = self.size + payload:len()
|
||||||
|
|||||||
@ -14,7 +14,13 @@ return function (connection, req, args)
|
|||||||
return header
|
return header
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Error " .. args.code .. ": " .. args.errorString)
|
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 {}
|
args.headers = args.headers or {}
|
||||||
connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
|
connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
|
||||||
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
||||||
|
|||||||
@ -20,7 +20,7 @@ return function (connection, req, args)
|
|||||||
chunk = nil
|
chunk = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
end
|
end
|
||||||
print("Finished sending: ", args.file)
|
-- print("Finished sending: ", args.file)
|
||||||
fileHandle:close()
|
fileHandle:close()
|
||||||
fileHandle = nil
|
fileHandle = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|||||||
@ -16,6 +16,16 @@ return function (port)
|
|||||||
|
|
||||||
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
||||||
|
|
||||||
|
-- Pretty log function.
|
||||||
|
local function log(connection, msg, optionalMsg)
|
||||||
|
local port, ip = connection:getpeer()
|
||||||
|
if(optionalMsg == nil) then
|
||||||
|
print(ip .. ":" .. port, msg)
|
||||||
|
else
|
||||||
|
print(ip .. ":" .. port, msg, optionalMsg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function startServing(fileServeFunction, connection, req, args)
|
local function startServing(fileServeFunction, connection, req, args)
|
||||||
connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
|
connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
|
||||||
fileServeFunction(bufferedConnection, req, args)
|
fileServeFunction(bufferedConnection, req, args)
|
||||||
@ -29,9 +39,7 @@ return function (port)
|
|||||||
local BufferedConnectionClass = dofile("httpserver-connection.lc")
|
local BufferedConnectionClass = dofile("httpserver-connection.lc")
|
||||||
local bufferedConnection = BufferedConnectionClass:new(connection)
|
local bufferedConnection = BufferedConnectionClass:new(connection)
|
||||||
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
||||||
if not status then
|
if not status then log(connection, "Error: "..err) end
|
||||||
print("Error: ", err)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function handleRequest(connection, req)
|
local function handleRequest(connection, req)
|
||||||
@ -42,7 +50,7 @@ return function (port)
|
|||||||
|
|
||||||
if #(uri.file) > 32 then
|
if #(uri.file) > 32 then
|
||||||
-- nodemcu-firmware cannot handle long filenames.
|
-- nodemcu-firmware cannot handle long filenames.
|
||||||
uri.args = {code = 400, errorString = "Bad Request"}
|
uri.args = {code = 400, errorString = "Bad Request", logFunction = log}
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
else
|
else
|
||||||
local fileExists = file.open(uri.file, "r")
|
local fileExists = file.open(uri.file, "r")
|
||||||
@ -61,7 +69,7 @@ return function (port)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not fileExists then
|
if not fileExists then
|
||||||
uri.args = {code = 404, errorString = "Not Found"}
|
uri.args = {code = 404, errorString = "Not Found", logFunction = log}
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
elseif uri.isScript then
|
elseif uri.isScript then
|
||||||
fileServeFunction = dofile(uri.file)
|
fileServeFunction = dofile(uri.file)
|
||||||
@ -70,7 +78,7 @@ return function (port)
|
|||||||
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
|
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
|
||||||
fileServeFunction = dofile("httpserver-static.lc")
|
fileServeFunction = dofile("httpserver-static.lc")
|
||||||
else
|
else
|
||||||
uri.args = {code = 405, errorString = "Method not supported"}
|
uri.args = {code = 405, errorString = "Method not supported", logFunction = log}
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -102,7 +110,7 @@ return function (port)
|
|||||||
|
|
||||||
-- parse payload and decide what to serve.
|
-- parse payload and decide what to serve.
|
||||||
local req = dofile("httpserver-request.lc")(payload)
|
local req = dofile("httpserver-request.lc")(payload)
|
||||||
print(req.method .. ": " .. req.request)
|
log(connection, req.method, req.request)
|
||||||
if conf.auth.enabled then
|
if conf.auth.enabled then
|
||||||
auth = dofile("httpserver-basicauth.lc")
|
auth = dofile("httpserver-basicauth.lc")
|
||||||
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
|
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
|
||||||
@ -114,11 +122,11 @@ return function (port)
|
|||||||
local args = {}
|
local args = {}
|
||||||
local fileServeFunction = dofile("httpserver-error.lc")
|
local fileServeFunction = dofile("httpserver-error.lc")
|
||||||
if not user then
|
if not user then
|
||||||
args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}}
|
args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}, logFunction = log}
|
||||||
elseif req.methodIsValid then
|
elseif req.methodIsValid then
|
||||||
args = {code = 501, errorString = "Not Implemented"}
|
args = {code = 501, errorString = "Not Implemented", logFunction = log}
|
||||||
else
|
else
|
||||||
args = {code = 400, errorString = "Bad Request"}
|
args = {code = 400, errorString = "Bad Request", logFunction = log}
|
||||||
end
|
end
|
||||||
startServing(fileServeFunction, connection, req, args)
|
startServing(fileServeFunction, connection, req, args)
|
||||||
end
|
end
|
||||||
@ -131,9 +139,7 @@ return function (port)
|
|||||||
if connectionThreadStatus == "suspended" then
|
if connectionThreadStatus == "suspended" then
|
||||||
-- Not finished sending file, resume.
|
-- Not finished sending file, resume.
|
||||||
local status, err = coroutine.resume(connectionThread)
|
local status, err = coroutine.resume(connectionThread)
|
||||||
if not status then
|
if not status then log(getpeer(), "Error: " .. err) end
|
||||||
print(err)
|
|
||||||
end
|
|
||||||
elseif connectionThreadStatus == "dead" then
|
elseif connectionThreadStatus == "dead" then
|
||||||
-- We're done sending file.
|
-- We're done sending file.
|
||||||
connection:close()
|
connection:close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user