-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhttpDL.lua
More file actions
43 lines (38 loc) · 974 Bytes
/
httpDL.lua
File metadata and controls
43 lines (38 loc) · 974 Bytes
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
-- ESP8266-HTTP Library
-- Written 2014 by Tobias Mädel (t.maedel@alfeld.de)
-- Licensed unter MIT
local moduleName = ...
local M = {}
_G[moduleName] = M
function M.download(host, port, url, path, callback)
file.open(path, "w+")
conn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, payload)
payloadFound = false
if (payloadFound == true) then
file.write(payload)
file.flush()
else
if (string.find(payload,"\r\n\r\n") ~= nil) then
file.write(string.sub(payload,string.find(payload,"\r\n\r\n") + 4))
file.flush()
payloadFound = true
end
end
payload = nil
collectgarbage()
end)
conn:on("disconnection", function(conn)
conn = nil
file.close()
callback("ok")
end)
conn:connect(port,host)
conn:send("GET /"..url.." HTTP/1.0\r\n"..
"Host: "..host.."\r\n"..
"Connection: close\r\n"..
"Accept-Charset: utf-8\r\n"..
"Accept-Encoding: \r\n"..
"Accept: */*\r\n\r\n")
end
return M