-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_util.lua
46 lines (41 loc) · 1.2 KB
/
conn_util.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
function sendfile(conn, filename, header)
function send(sent, amt)
file.open(filename)
file.seek('set', sent)
local s = file.read(amt)
file.close()
conn:send(s)
logmsg('sent ' .. s:len() .. ' bytes')
return s:len() < amt
end
file.open(filename)
local file_len = file.seek('end', 0)
file.close()
local sent = 0
conn:on('sent', function()
local done = send(sent, 256)
if done then
conn:close()
conn = nil
else
sent = sent + 256
end
end)
conn:send("HTTP/1.1 200 OK\r\nserver: nodemcu\r\n" ..
"Content-Length: " .. tostring(file_len) .. '\r\n' .. header .. "\r\n")
logmsg('sending ' .. filename)
end
function sendstring(conn, str, header)
if header == nil then header = '' end
conn:on('sent', function()
conn:send(string.sub(str, 1, 256))
if str:len() < 257 then
conn:close()
conn = nil
else
str = string.sub(str, 257)
end
end)
conn:send("HTTP/1.1 200 OK\r\nserver: nodemcu\r\n" ..
"Content-Length: " .. tostring(str:len()) .. '\r\n' .. header .. "\r\n")
end