Got responses working!

This commit is contained in:
sloumdrone 2019-01-02 18:34:29 -08:00
parent 045a40ba56
commit 14a2f2cf6e
1 changed files with 35 additions and 18 deletions

View File

@ -2,6 +2,7 @@
local socket = require("socket")
local urlparser = require("socket.url")
local separator = '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
table.split = function(inputstr, sep)
if sep == nil then
@ -21,20 +22,35 @@ function request_gopher(host, port, query)
local tcp = assert(socket.tcp())
local response = {}
tcp:connect(host, port);
tcp:send(query.."\n");
while true do
local s, status, partial = tcp:receive()
if status == "closed" then break end
table.insert(response, (s or partial))
tcp:settimeout(2000)
local connection = tcp:connect(host, port);
local request
if connection then
request = tcp:send(query.."\n");
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
function getch_unix()
function getch()
-- 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")
local key = io.read(1)
@ -45,9 +61,8 @@ end
function mainloop()
while true do
print('(G)o to url, (B)ack, (#) Visit link, (Q)uit')
io.write('> ')
local key = string.lower(getch_unix())
print('\n\27[7m (G)o to url, (B)ack, (#) Visit link, (Q)uit \27[0m')
local key = string.lower(getch())
io.write('\n')
if key == 'q' then
os.exit(0)
@ -56,13 +71,15 @@ function mainloop()
local url = io.read()
url = parse_url(url)
if not url.host or url.scheme ~= 'gopher' then
print('Invalid url')
print('\nInvalid url\n')
else
print(url.scheme)
print(url.host)
print(url.port)
print(url.gophertype)
print(url.path)
filedata = request_gopher(url.host, url.port, url.path)
print(separator)
for i, v in ipairs(filedata) do
print(v)
end
print(separator)
end
end
end