Got responses working!
This commit is contained in:
parent
045a40ba56
commit
14a2f2cf6e
53
main.lua
53
main.lua
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
local socket = require("socket")
|
local socket = require("socket")
|
||||||
local urlparser = require("socket.url")
|
local urlparser = require("socket.url")
|
||||||
|
local separator = '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
|
||||||
|
|
||||||
table.split = function(inputstr, sep)
|
table.split = function(inputstr, sep)
|
||||||
if sep == nil then
|
if sep == nil then
|
||||||
|
@ -21,20 +22,35 @@ function request_gopher(host, port, query)
|
||||||
local tcp = assert(socket.tcp())
|
local tcp = assert(socket.tcp())
|
||||||
local response = {}
|
local response = {}
|
||||||
|
|
||||||
tcp:connect(host, port);
|
tcp:settimeout(2000)
|
||||||
tcp:send(query.."\n");
|
local connection = tcp:connect(host, port);
|
||||||
|
local request
|
||||||
while true do
|
if connection then
|
||||||
local s, status, partial = tcp:receive()
|
request = tcp:send(query.."\n");
|
||||||
if status == "closed" then break end
|
|
||||||
table.insert(response, (s or partial))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
tcp:close()
|
if request then
|
||||||
|
while true do
|
||||||
|
local s, status, partial = tcp:receive()
|
||||||
|
if status == "closed" then break end
|
||||||
|
if s ~= '.' then
|
||||||
|
table.insert(response, (s or partial))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
tcp:close()
|
||||||
|
elseif connection then
|
||||||
|
response[1] = 'Connection error. Bad request.'
|
||||||
|
tcp:close()
|
||||||
|
else
|
||||||
|
response[1] = 'Connection error. Bad host.'
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function getch_unix()
|
function getch()
|
||||||
-- taken from: http://lua.2524044.n2.nabble.com/How-to-get-one-keystroke-without-hitting-Enter-td5858614.html
|
-- taken from: http://lua.2524044.n2.nabble.com/How-to-get-one-keystroke-without-hitting-Enter-td5858614.html
|
||||||
os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
|
os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
|
||||||
local key = io.read(1)
|
local key = io.read(1)
|
||||||
|
@ -45,9 +61,8 @@ end
|
||||||
|
|
||||||
function mainloop()
|
function mainloop()
|
||||||
while true do
|
while true do
|
||||||
print('(G)o to url, (B)ack, (#) Visit link, (Q)uit')
|
print('\n\27[7m (G)o to url, (B)ack, (#) Visit link, (Q)uit \27[0m')
|
||||||
io.write('> ')
|
local key = string.lower(getch())
|
||||||
local key = string.lower(getch_unix())
|
|
||||||
io.write('\n')
|
io.write('\n')
|
||||||
if key == 'q' then
|
if key == 'q' then
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
|
@ -56,13 +71,15 @@ function mainloop()
|
||||||
local url = io.read()
|
local url = io.read()
|
||||||
url = parse_url(url)
|
url = parse_url(url)
|
||||||
if not url.host or url.scheme ~= 'gopher' then
|
if not url.host or url.scheme ~= 'gopher' then
|
||||||
print('Invalid url')
|
print('\nInvalid url\n')
|
||||||
else
|
else
|
||||||
print(url.scheme)
|
|
||||||
print(url.host)
|
filedata = request_gopher(url.host, url.port, url.path)
|
||||||
print(url.port)
|
print(separator)
|
||||||
print(url.gophertype)
|
for i, v in ipairs(filedata) do
|
||||||
print(url.path)
|
print(v)
|
||||||
|
end
|
||||||
|
print(separator)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue