@@ -13,6 +13,7 @@ return function (port)
1313 -- We do it in a separate thread because we need to send in little chunks and wait for the onSent event
1414 -- before we can send more, or we risk overflowing the mcu's buffer.
1515 local connectionThread
16+ local fileInfo
1617
1718 local allowStatic = {GET = true , HEAD = true , POST = false , PUT = false , DELETE = false , TRACE = false , OPTIONS = false , CONNECT = false , PATCH = false }
1819
@@ -26,6 +27,10 @@ return function (port)
2627 end
2728 end
2829
30+ local function startServingStatic (connection , req , args )
31+ fileInfo = dofile (" httpserver-static.lc" )(connection , req , args )
32+ end
33+
2934 local function startServing (fileServeFunction , connection , req , args )
3035 connectionThread = coroutine.create (function (fileServeFunction , bufferedConnection , req , args )
3136 fileServeFunction (bufferedConnection , req , args )
@@ -40,6 +45,7 @@ return function (port)
4045
4146 local BufferedConnectionClass = dofile (" httpserver-connection.lc" )
4247 local bufferedConnection = BufferedConnectionClass :new (connection )
48+ BufferedConnectionClass = nil
4349 local status , err = coroutine.resume (connectionThread , fileServeFunction , bufferedConnection , req , args )
4450 if not status then
4551 log (connection , " Error: " .. err )
@@ -50,7 +56,7 @@ return function (port)
5056 end
5157 end
5258
53- local function handleRequest (connection , req )
59+ local function handleRequest (connection , req , handleError )
5460 collectgarbage ()
5561 local method = req .method
5662 local uri = req .uri
@@ -84,7 +90,8 @@ return function (port)
8490 else
8591 if allowStatic [method ] then
8692 uri .args = {file = uri .file , ext = uri .ext , isGzipped = uri .isGzipped }
87- fileServeFunction = dofile (" httpserver-static.lc" )
93+ startServingStatic (connection , req , uri .args )
94+ return
8895 else
8996 uri .args = {code = 405 , errorString = " Method not supported" , logFunction = log }
9097 fileServeFunction = dofile (" httpserver-error.lc" )
@@ -95,7 +102,7 @@ return function (port)
95102 end
96103
97104 local function onReceive (connection , payload )
98- collectgarbage ()
105+ -- collectgarbage()
99106 local conf = dofile (" httpserver-conf.lc" )
100107 local auth
101108 local user = " Anonymous"
@@ -162,6 +169,27 @@ return function (port)
162169 connectionThread = nil
163170 collectgarbage ()
164171 end
172+ elseif fileInfo then
173+ local fileSize = file .list ()[fileInfo .file ]
174+ -- Chunks larger than 1024 don't work.
175+ -- https://github.com/nodemcu/nodemcu-firmware/issues/1075
176+ local chunkSize = 512
177+ local fileHandle = file .open (fileInfo .file )
178+ if fileSize > fileInfo .sent then
179+ fileHandle :seek (" set" , fileInfo .sent )
180+ local chunk = fileHandle :read (chunkSize )
181+ fileHandle :close ()
182+ fileHandle = nil
183+ fileInfo .sent = fileInfo .sent + # chunk
184+ connection :send (chunk )
185+ -- print(fileInfo.file .. ": Sent "..#chunk.. " bytes, " .. fileSize - fileInfo.sent .. " to go.")
186+ chunk = nil
187+ else
188+ log (connection , " closing connetion" , " Finished sending: " .. fileInfo .file )
189+ connection :close ()
190+ fileInfo = nil
191+ end
192+ collectgarbage ()
165193 end
166194 end
167195
@@ -172,6 +200,10 @@ return function (port)
172200 connectionThread = nil
173201 collectgarbage ()
174202 end
203+ if fileInfo then
204+ fileInfo = nil
205+ collectgarbage ()
206+ end
175207 end
176208
177209 connection :on (" receive" , onReceive )
0 commit comments