nodemcu-httpserver/httpserver-connection.lua
Fractal147 9f4d7a9988 Fixed unnecessary globals as in issue #113. (#122)
* Fixed global assignment that should be local

Made result variable be local, see Issue #113

* Made global variable local

Made ASCII variable be local, see Issue #113

* Made more variables local

Related to Issue #113. questionMarkPos, and b,c,d,e,f all are global in scope, and are not cleared from memory, so leak.
Frankly, b, c, d, e, and f are not used either, but will now get GC'd later, if they ever were assigned, so not problematic
line 114 also has _ and i to make local too, so were put on their own line.
i on line 24 also was unnecessarily global, and undetected in issue #113

* Made module more local

Made the basicAuth table local in scope. Since it is returned when dofile is called in httpserver.lua, that already has a correctly scoped table, 'auth'. This is related to issue #113, and should reduce memory loss to globals

* Made bufferedConnection local

bufferedConnection was global and didn't have to be. Part of issue #113.
Now no longer remains in _G (globals table) after a connection has closed.
2018-01-14 22:03:37 -06:00

64 lines
2.1 KiB
Lua

-- httpserver-connection
-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple
-- consecutive send() calls, and buffers small payloads to send once they get big.
-- For this to work, it must be used from a coroutine and owner is responsible for the final
-- flush() and for closing the connection.
-- Author: Philip Gladstone, Marcos Kirsch
local BufferedConnection = {}
-- parameter is the nodemcu-firmware connection
function BufferedConnection:new(connection)
local newInstance = {}
newInstance.connection = connection
newInstance.size = 0
newInstance.data = {}
-- Returns true if there was any data to be sent.
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
function newInstance:getpeer()
return self.connection:getpeer()
end
function newInstance:send(payload)
local flushThreshold = 1400
local newSize = self.size + payload:len()
while newSize >= flushThreshold do
--STEP1: cut out piece from payload to complete threshold bytes in table
local pieceSize = flushThreshold - self.size
local piece = payload:sub(1, pieceSize)
payload = payload:sub(pieceSize + 1, -1)
--STEP2: insert piece into table
table.insert(self.data, piece)
piece = nil
self.size = self.size + pieceSize --size should be same as flushThreshold
--STEP3: flush entire table
if self:flush() then
coroutine.yield()
end
--at this point, size should be 0, because the table was just flushed
newSize = self.size + payload:len()
end
--at this point, whatever is left in payload should be < flushThreshold
if payload:len() ~= 0 then
--leave remaining data in the table
table.insert(self.data, payload)
self.size = self.size + payload:len()
end
end
return newInstance
end
return BufferedConnection