Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Get to website with lua

Tags:

lua

luasocket

i'm having a trouble with lua.

I need send a request to a website by GET and get the response from website.

Atm all i have is this:

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()

What should i do to get the response from server ?

I want to send some info to this website and get the result of page.

Any ideas ?

like image 408
Gmlyra Avatar asked Dec 26 '22 07:12

Gmlyra


2 Answers

This probably won't work as *a will be reading until the connection is closed, but in this case the client doesn't know how much to read. What you need to do is to read line-by-line and parse the headers to find Content-Length and then after you see two line ends you read the specified number of bytes (as set in Content-Length).

Instead of doing it all yourself (reading and parsing headers, handling redirects, 100 continue, and all that), socket.http will take care of all that complexity for you. Try something like this:

local http = require("socket.http")
local body, code, headers, status = http.request("https://www.google.com")
print(code, status, #body)
like image 157
Paul Kulchenko Avatar answered Jan 15 '23 23:01

Paul Kulchenko


I solved the problem passing the header

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\nHost: example.com\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()
like image 41
Gmlyra Avatar answered Jan 16 '23 01:01

Gmlyra