我有一个带有nodeMCU的ESP8266芯片,这是一个开源固件,可以让我在芯片内部编写Lua脚本。
我将它用作强制网络门户,我只想从用户名 - 密码表单中登录txt文件数据。我写了代码但是当我连接到wifi并且我重定向到门户页面时,我有无限的加载,主页永远不会加载。
我无法弄清楚为什么,这里是我的lua代码
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
local rnrn=0
local Status = 0
local DataToGet = 0
local method=""
local url=""
local vars=""
conn:on("receive",function(conn,payload)
if Status==0 then
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
-- print(method, url, vars)
end
if url==nil then
url="index.htm"
end
if url=="" then
url="index.htm"
end
-- some ugly magic for Apple IOS Devices
if string.find(url, "/") ~= nil then
--print ("Slash found")
local invurl=string.reverse(url)
local a,b=string.find(invurl, "/", 1)
url=string.sub(url, string.len(url)-(a-2))
--print ("Neue URL= " .. url)
end
if string.len(url)>= 25 then
url = string.sub (url,1,25)
-- print ("cut down URL")
end
DataToGet = -1
if url == "favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
return
end
conn:send("HTTP/1.1 200 OK\r\n\r\n")
if url=="login.htm" then
loginPage()
conn:close()
return
end
if url=="logData" then
loginPage()
-- save data in a file
file.open("log.txt", "w+")
file.write(vars)
file.flush()
file.close()
conn:close()
return
end
local foundmatch = 0
local a = {'wumpus.htm','index.htm','about.htm','ttt.htm','instruct.htm','status.htm','kg-small.png','talk.htm','hovercraft.mp3','test.pdf'}
for _,v in pairs(a) do
if v == url then
foundmatch=1
-- print ("Found " .. v)
break
end
end
if foundmatch == 0 then
-- print ("Found no match, setting index")
url="index.htm"
end
end)
end)
function loginPage()
conn:send("<html><body><h1>Login</h1>")
conn:send("<form action='logData' method='post'><label for='un'>Username:</label><input type='text' name='un' /><label for='pw'>Password:</label><input type='password' name='pw' /><input type='submit' name='submit' value='Submit' /></form>")
conn:send("</body></html>")
end
print("HTTP Server is now listening. Free Heap:", node.heap())
答案 0 :(得分:0)
我为节目编辑的登录信息,但显然不会是一个错误。 JankoP
function loginPage(conn)
conn:send('HTTP/1.1 200 OK\r\n\r\n<html><body><h1>Login</h1>'..
'<form action="logData" method="post"><label for="un">Username:</label>'..
'<input type="text" name="un" /><label for="pw">Password:</label>'..
'<input type="password" name="pw" /><input type="submit" name="submit" value="Submit" /></form>'..
'</body></html>')
end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
local rnrn=0
local Status = 0
local DataToGet = 0
local method=""
local url=""
local vars=""
conn:on("receive",function(conn,payload)
if Status==0 then
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
print(method, url, vars)
end
if url==nil then
url="index.htm"
end
if url=="" then
url="index.htm"
end
-- some ugly magic for Apple IOS Devices
if string.find(url, "/") ~= nil then
--print ("Slash found")
local invurl=string.reverse(url)
local a,b=string.find(invurl, "/", 1)
url=string.sub(url, string.len(url)-(a-2))
--print ("Neue URL= " .. url)
end
if string.len(url)>= 25 then
url = string.sub (url,1,25)
-- print ("cut down URL")
end
DataToGet = -1
if url == "favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
return
end
if url=="login.htm" then
loginPage(conn)
conn:on("sent", function(conn) conn:close() end)
return
end
if url=="logData" then
loginPage(conn)
-- save data in a file
file.open("log.txt", "w+")
file.write(vars)
file.flush()
file.close()
conn:on("sent", function(conn) conn:close() end)
return
end
local foundmatch = 0
local a = {'wumpus.htm','index.htm','about.htm','ttt.htm','instruct.htm','status.htm','kg-small.png','talk.htm','hovercraft.mp3','test.pdf'}
for _,v in pairs(a) do
if v == url then
foundmatch=1
-- print ("Found " .. v)
break
end
end
if foundmatch == 0 then
-- print ("Found no match, setting index")
url="index.htm"
end
end)
end)
print("HTTP Server is now listening. Free Heap:", node.heap())