forked from loicortola/nodemcu-espress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_response_sendfile.lua
55 lines (52 loc) · 1.37 KB
/
http_response_sendfile.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
------------------------------------------------------------------------------
-- send file
------------------------------------------------------------------------------
return function(res, filename, status)
local buffersize = 1024
local offset = 0
local buf
local more
if not file.open(filename, "r") then
local f = loadfile("http_not_found.lc")
f()(res)
f = nil
else
buf = "HTTP/1.1 " .. tostring(status or res.statuscode) .. " " .. dofile('http-' .. tostring(status or res.statuscode)) .. "\r\n"
-- Write response headers
res:addheader("Server", "NodeMCU")
res:addheader("Transfer-Encoding", "chunked")
for key, value in pairs(res.headers) do
-- send header
buf = buf .. key .. ": " .. value .. "\r\n"
end
buf = buf .. "\r\n"
res.conn:send(buf)
more = true
-- Send file body
local function sendnextchunk()
collectgarbage("collect")
file.seek("set", offset)
buf = file.read(buffersize)
res.conn:send(("%X\r\n"):format(#buf) .. buf .. "\r\n")
more = (#buf == buffersize)
if more then
offset = offset + buffersize
end
end
res.conn:on("sent", function(conn)
if (more) then
sendnextchunk()
else
-- Manually free resources for gc
buf = nil
buffersize = nil
offset = nil
sendnextchunk = nil
-- Close connection
conn:send("0\r\n\r\n")
file.close()
conn:close()
end
end)
end
end