diff --git a/httpserver-connection.lua b/httpserver-connection.lua index 7026482..52aee69 100644 --- a/httpserver-connection.lua +++ b/httpserver-connection.lua @@ -5,7 +5,7 @@ -- flush() and for closing the connection. -- Author: Philip Gladstone, Marcos Kirsch -BufferedConnection = {} +local BufferedConnection = {} -- parameter is the nodemcu-firmware connection function BufferedConnection:new(connection) diff --git a/httpserver-static.lua b/httpserver-static.lua index 10f2e70..dff6105 100644 --- a/httpserver-static.lua +++ b/httpserver-static.lua @@ -4,6 +4,8 @@ return function (connection, req, args) dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped) + connection:flush() + coroutine.yield() -- Send file in little chunks local bytesRemaining = file.list()[args.file] -- Chunks larger than 1024 don't work. @@ -13,9 +15,9 @@ return function (connection, req, args) while bytesRemaining > 0 do local bytesToRead = 0 if bytesRemaining > chunkSize then bytesToRead = chunkSize else bytesToRead = bytesRemaining end - local chunk = fileHandle:read(bytesToRead) - connection:send(chunk) - bytesRemaining = bytesRemaining - #chunk + connection.connection:send(fileHandle:read(bytesToRead)) + coroutine.yield() + bytesRemaining = bytesRemaining - bytesToRead --print(args.file .. ": Sent "..#chunk.. " bytes, " .. bytesRemaining .. " to go.") chunk = nil collectgarbage() diff --git a/httpserver.lua b/httpserver.lua index 65b4bc8..54715c4 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -60,6 +60,7 @@ return function (port) -- nodemcu-firmware cannot handle long filenames. uri.args = {code = 400, errorString = "Bad Request", logFunction = log} fileServeFunction = dofile("httpserver-error.lc") + req=nil else local fileExists = false @@ -79,15 +80,18 @@ return function (port) if not fileExists then uri.args = {code = 404, errorString = "Not Found", logFunction = log} fileServeFunction = dofile("httpserver-error.lc") + req=nil elseif uri.isScript then fileServeFunction = dofile(uri.file) else if allowStatic[method] then uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped} fileServeFunction = dofile("httpserver-static.lc") + req=nil else uri.args = {code = 405, errorString = "Method not supported", logFunction = log} fileServeFunction = dofile("httpserver-error.lc") + req=nil end end end