diff --git a/Makefile b/Makefile index 8c829cf..3b07436 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,17 @@ SPEED=9600 # End of user config ###################################################################### HTTP_FILES := $(wildcard http/*) -LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-basicauth.lua httpserver-b64decode.lua httpserver-conf.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua +LUA_FILES := \ + init.lua \ + httpserver.lua \ + httpserver-b64decode.lua \ + httpserver-basicauth.lua \ + httpserver-conf.lua \ + httpserver-connection.lua \ + httpserver-error.lua \ + httpserver-header.lua \ + httpserver-request.lua \ + httpserver-static.lua \ # Print usage usage: diff --git a/httpserver-connection.lua b/httpserver-connection.lua new file mode 100644 index 0000000..e7e1848 --- /dev/null +++ b/httpserver-connection.lua @@ -0,0 +1,47 @@ +-- httpserver-connection +-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple +-- consecutive send() calls. +-- For this to work, it must be used from a coroutine. +-- Author: Philip Gladstone, Marcos Kirsch + +BufferedConnection = {} + +-- parameter is the nodemcu-firmware connection +function BufferedConnection:new(connection) + local newInstance = {} + newInstance.connection = connection + newInstance.size = 0 + newInstance.data = {} + + function newInstance:flush() + if self.size > 0 then + self.connection:send(table.concat(self.data, "")) + self.data = {} + self.size = 0 + return true + end + return false + end + + --@TODO What are the hardcoded 1000 and 800 about? Can we increase? + function newInstance:send(payload) + local l = payload:len() + if l + self.size > 1000 then + if self:flush() then + coroutine.yield() + end + end + if l > 800 then + self.connection:send(payload) + coroutine.yield() + else + table.insert(self.data, payload) + self.size = self.size + l + end + end + + return newInstance +end + + +return BufferedConnection diff --git a/httpserver.lua b/httpserver.lua index 02cc4ef..9e1dd87 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -18,47 +18,21 @@ return function (port) local function startServing(fileServeFunction, connection, req, args) - local bufferedConnection = {} - connectionThread = coroutine.create(function(fileServeFunction, bconnection, req, args) - fileServeFunction(bconnection, req, args) - if not bconnection:flush() then + connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args) + fileServeFunction(bufferedConnection, req, args) + if not bufferedConnection:flush() then connection:close() connectionThread = nil end end) - function bufferedConnection:flush() - if self.size > 0 then - connection:send(table.concat(self.data, "")) - self.data = {} - self.size = 0 - return true - end - return false - end - - function bufferedConnection:send(payload) - local l = payload:len() - if l + self.size > 1000 then - if self:flush() then - coroutine.yield() - end - end - if l > 800 then - connection:send(payload) - coroutine.yield() - else - table.insert(self.data, payload) - self.size = self.size + l - end - end - - bufferedConnection.size = 0 - bufferedConnection.data = {} + local BufferedConnectionClass = dofile("httpserver-connection.lc") + local bufferedConnection = BufferedConnectionClass:new(connection) local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args) if not status then print("Error: ", err) end + end local function onRequest(connection, req) @@ -185,6 +159,7 @@ return function (port) -- false and nil evaluate as false local ip = wifi.sta.getip() if not ip then ip = wifi.ap.getip() end + if not ip then ip = "unknown IP" end print("nodemcu-httpserver running at http://" .. ip .. ":" .. port) return s diff --git a/init.lua b/init.lua index c40ca8e..fe654a8 100644 --- a/init.lua +++ b/init.lua @@ -56,7 +56,17 @@ local compileAndRemoveIfNeeded = function(f) end end -local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'} +local serverFiles = { + 'httpserver.lua', + 'httpserver-b64decode.lua', + 'httpserver-basicauth.lua', + 'httpserver-conf.lua', + 'httpserver-connection.lua', + 'httpserver-error.lua', + 'httpserver-header.lua', + 'httpserver-request.lua', + 'httpserver-static.lua', +} for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end compileAndRemoveIfNeeded = nil