|
@@ -13,9 +13,37 @@ return function (port)
|
|
|
-- We do it in a separate thread because we need to yield when sending lots
|
|
|
-- of data in order to avoid overflowing the mcu's buffer.
|
|
|
local connectionThread
|
|
|
-
|
|
|
+
|
|
|
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
|
|
|
|
|
+ local function startServing(fileServeFunction, connection, req, args)
|
|
|
+ local bufferedConnection = {}
|
|
|
+ connectionThread = coroutine.create(function(fileServeFunction, connection, req, args)
|
|
|
+ fileServeFunction(connection, req, args)
|
|
|
+ connection:flush()
|
|
|
+ end)
|
|
|
+ function bufferedConnection:flush()
|
|
|
+ connection:send(table.concat(self.data, ""))
|
|
|
+ self.data = {}
|
|
|
+ self.size = 0
|
|
|
+ end
|
|
|
+ function bufferedConnection:send(payload)
|
|
|
+ local l = payload:len()
|
|
|
+ if l + self.size > 1400 then
|
|
|
+ self:flush()
|
|
|
+ coroutine.yield()
|
|
|
+ end
|
|
|
+ table.insert(self.data, payload)
|
|
|
+ self.size = self.size + l
|
|
|
+ end
|
|
|
+ bufferedConnection.size = 0
|
|
|
+ bufferedConnection.data = {}
|
|
|
+ local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
|
|
+ if not status then
|
|
|
+ print(err)
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
local function onRequest(connection, req)
|
|
|
collectgarbage()
|
|
|
local method = req.method
|
|
@@ -59,8 +87,7 @@ return function (port)
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
- connectionThread = coroutine.create(fileServeFunction)
|
|
|
- coroutine.resume(connectionThread, connection, req, uri.args)
|
|
|
+ startServing(fileServeFunction, connection, req, uri.args)
|
|
|
end
|
|
|
|
|
|
local function onReceive(connection, payload)
|
|
@@ -89,18 +116,20 @@ return function (port)
|
|
|
else
|
|
|
args = {code = 400, errorString = "Bad Request"}
|
|
|
end
|
|
|
- connectionThread = coroutine.create(fileServeFunction)
|
|
|
- coroutine.resume(connectionThread, connection, req, args)
|
|
|
+ startServing(fileServeFunction, connection, req, args)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
local function onSent(connection, payload)
|
|
|
collectgarbage()
|
|
|
if connectionThread then
|
|
|
- local connectionThreadStatus = coroutine.status(connectionThread)
|
|
|
+ local connectionThreadStatus = coroutine.status(connectionThread)
|
|
|
if connectionThreadStatus == "suspended" then
|
|
|
-- Not finished sending file, resume.
|
|
|
- coroutine.resume(connectionThread)
|
|
|
+ local status, err = coroutine.resume(connectionThread)
|
|
|
+ if not status then
|
|
|
+ print(err)
|
|
|
+ end
|
|
|
elseif connectionThreadStatus == "dead" then
|
|
|
-- We're done sending file.
|
|
|
connection:close()
|