As the title says... Chat on...

User avatar
By juanmol
#25885 Hi, i'm trying this code:
Code: Select allfunction create_server()
sv=net.createServer(net.TCP, 30)
    sv:listen(88,function(c)
      c:on("receive", function(c, pl) print(pl) end)
      end)
end

working, fine, from other pc, i do a get http://ip_esp8266:88/hello/bye/ and in the esp8266 i see:
Code: Select allGET /hello/bye/ HTTP/1.1
Host: 192.168.84.107:88
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Linux; Android 5.0.2; C6833 Build/14.5.A.0.242) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.133 Mobile Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6


My question is: how can i get variables with the string 'hello' and other with the string 'bye'? In a linux machine i do something like:
Code: Select allvar1=$(echo $pl | grep ^GET | cut -f2 -d\/)
var2=$(echo $pl | grep ^GET | cut -f3 -d\/)


Please help!
User avatar
By GeoReb
#26951 What you described is a path, not strings.

http://192.168.4.1/pages/index.html < this is a path
http://192.168.4.1/index.html?hello=1&bye=2 < this is a path with strings

You have to use patterns to manipulate the incoming requests, this code should do the trick...

Code: Select allsrv=net.createServer(net.TCP, 30)
srv:listen(80,function(conn)
   conn:on("receive", function(conn,pl)
      local _, _, method, path, vars = string.find(pl, "([A-Z]+) (.+)?(.+) HTTP");
      if(method == nil)then
          _, _, method, path = string.find(pl, "([A-Z]+) (.+) HTTP");
      end
      
      print(path) -- this is the path from the HTML GET request
      
      print(vars) -- these are the strings from the HTML GET request
      
      if (vars ~= nil) then
         for v, x in string.gmatch(vars, "(%w+)=(%w+)&*") do
            
            print("Variable "..v.." = "..x) -- this will print the variables individually, one by one
            
         end
      end
   end)
end)