1

我有一个带有 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())
4

1 回答 1

0

我为节目编辑的登录名,但显然这不会是一个错误。扬科普

        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())
于 2016-11-19T10:43:01.287 回答