-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathhttpserver-static.lua
More file actions
29 lines (28 loc) · 1.09 KB
/
httpserver-static.lua
File metadata and controls
29 lines (28 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- httpserver-static.lua
-- Part of nodemcu-httpserver, handles sending static files to client.
-- Author: Marcos Kirsch
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.
-- https://github.com/nodemcu/nodemcu-firmware/issues/1075
local chunkSize = 1024
local fileHandle = file.open(args.file)
while bytesRemaining > 0 do
local bytesToRead = 0
if bytesRemaining > chunkSize then bytesToRead = chunkSize else bytesToRead = bytesRemaining end
connection.connection:send(fileHandle:read(bytesToRead))
coroutine.yield()
bytesRemaining = bytesRemaining - bytesToRead
--print(args.file .. ": Sent "..#chunk.. " bytes, " .. bytesRemaining .. " to go.")
chunk = nil
collectgarbage()
end
-- print("Finished sending: ", args.file)
fileHandle:close()
fileHandle = nil
collectgarbage()
end