forked from devyte/nodemcu-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver-static.lua
28 lines (27 loc) · 933 Bytes
/
httpserver-static.lua
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
-- 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.gzipped)
-- Send file in little chunks
local continue = true
local bytesSent = 0
while continue do
collectgarbage()
-- NodeMCU file API lets you open 1 file at a time.
-- So we need to open, seek, close each time in order
-- to support multiple simultaneous clients.
file.open(args.file)
file.seek("set", bytesSent)
local chunk = file.read(256)
file.close()
if chunk == nil then
continue = false
else
connection:send(chunk)
bytesSent = bytesSent + #chunk
chunk = nil
tmr.wdclr() -- loop can take a while for long files. tmr.wdclr() prevent watchdog to restart module
end
end
end