首页 文章

如何在nodemcu上用Lua解析分块的HTTP内容?

提问于
浏览
0

我有一个脚本,它在nodemcu和我的服务器之间进行通信 . 它在我的localhost上运行良好,并在我发送GET请求时解析从我的服务器检索到的响应 . 问题是当我将所有内容上传到我的网站上时传输编码被分块 . 虽然请求合法且正确,但我无法检索内容 . 代码是用Lua编写的,我正在尝试在我的NodeMCU设备上工作 .

conn=net.createConnection(net.TCP, 0)
    conn:on("connection",function(conn, payload)
    conn:send("GET /mypath/node.php?id=1&update"..
                " HTTP/1.1\r\n".. 
                "Host: www.mydomain.com\r\n"..
                "Accept: */*\r\n"..
                "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
                "\r\n\r\n") 
            end)

    conn:on("receive", function(conn, payload)
        if string.find(payload, "UPDATE")~=nil then 
            node.restart()
        end

        conn:close()
        conn = nil

    end)
    conn:connect(80,"www.mydomain.com")
end

只是重复一遍,这个GET请求是有效的,并且是在manhost和localhost上进行测试的 . 唯一的问题是分块内容,我不知道如何解析它 .

更新:我设法通过将HTTP / 1.1更改为HTTP / 1.0来删除分块编码,但仍然有问题

使用此代码

conn:on("receive", function(conn, payload)
        print(payload)

我收到了这个回复

HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 02:34:25 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=9m226vr20r4baa634bagk8k2k3; path=/
Connection: close
Content-Type: text/html; charset=utf-8

更新2 .

我刚创建了一个文件http.php,文本包含“php” . 我已将其上传到localhost和我的域名 . 一旦我尝试从nodemcu访问我的localhost,然后再访问域 . 结果不同

这是请求

conn:send("GET /"..s.path.."/http.php"..
                " HTTP/1.0\r\n".. 
                "Host: "..s.domain.."\r\n"..
                "Accept: */*\r\n"..
                "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
                "\r\n\r\n") 
            end)

s.domain和s.path对应于localhost和我的域上的不同路径和域

域名结果

HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 03:09:28 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=UTF-8

结果在localhost上

TTP/1.1 200 OK
Date: Tue, 09 Jan 2018 03:08:48 GMT
Server: Apache/2.4.27 (Win64) PHP/7.0.23
X-Powered-By: PHP/7.0.23
Content-Length: 3
Connection: close
Content-Type: text/html; charset=UTF-8

php

如您所见,localhost显示内容“php”,域只显示 Headers . 当我输入一些不存在的文件时,域名显示的是html代码 .

1 回答

  • 1

    我还是想知道,为什么你的服务器响应缺少 Content-Length 标头 .

    conn:on("receive", function(client, payload) 
    
      -- Inspired by https://github.com/marcoskirsch/nodemcu-httpserver/blob/master/httpserver.lua
      -- Collect data packets until the size of HTTP body meets the Content-Length stated in header
      if payload:find("Content%-Length:") or bBodyMissing then
          if fullPayload then fullPayload = fullPayload .. payload else fullPayload = payload end
          if (tonumber(string.match(fullPayload, "%d+", fullPayload:find("Content%-Length:")+16)) > #fullPayload:sub(fullPayload:find("\r\n\r\n", 1, true)+4, #fullPayload)) then
              bBodyMissing = true
              return
          else
              payload = fullPayload
              fullPayload, bBodyMissing = nil
          end
      end
    
      if (bBodyMissing == nil) then
          local _, headerEnd = payload:find("\r\n\r\n")
          local body = payload:sub(headerEnd + 1)
          print (body)
      end
    end)
    

相关问题