forked from benwaa/NODEMCU-LUA-OTA-ESP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.lua
executable file
·72 lines (65 loc) · 1.71 KB
/
server.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- save config file
function SaveX(vars)
file.remove("s.txt")
file.open("s.txt","w+")
for k, v in pairs(s) do
file.writeline(k .. "=" .. v)
end
file.close()
collectgarbage()
node.restart()
end
print(collectgarbage("count").." kB used")
LoadX()
wifi.setmode(wifi.SOFTAP)
cfg = {}
cfg.ssid = "WiFi_to_Config"
wifi.ap.config(cfg)
--dofile("dns-liar.lua")
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
local responseBytes = 0
local method = ""
local url = ""
local vars = ""
conn:on("receive",function(conn, payload)
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
print(method, url, vars)
print ("|"..vars.."|")
if (vars~=nil and vars~="") then
for k, v in string.gmatch(vars, "(%w+)=([^&]*)&*") do
s[k],n = string.gsub(v,"%%2F","/")
print(k .. " = " .. s[k])
end
SaveX()
end
if url == "favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
responseBytes = -1
return
end
-- Only support one sending one file
url = "config.htm"
responseBytes = 0
conn:send("HTTP/1.1 200 OK\r\n\r\n")
end)
conn:on("sent", function(conn)
if responseBytes>=0 and method=="GET" then
if file.open(url, "r") then
file.seek("set", responseBytes)
local line = file.read(512)
file.close()
if line then
conn:send(line)
responseBytes = responseBytes + 512
if (string.len(line) == 512) then
return
end
end
end
end
conn:close()
end)
end)
print("HTTP Server: Started")
print(collectgarbage("count").." kB used")