added the frequest stuff, will document protocol later but it's basically gopher minus hypertext

This commit is contained in:
Izaya 2018-02-19 21:04:22 +11:00
parent 8adb476dde
commit 46b9a8c655
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,68 @@
local net = require "net"
local fs = require "filesystem"
local event = require "event"
local prefix = "/srv"
local process = nil
local listener = nil
local dbug = false
local function dprint(...)
if dbug then
print(...)
end
end
function start()
process = net.flisten(70,function(s)
local buffer = ""
local function lf()
buffer=s:read(1024)
local nl = buffer:find("\n")
if nl then
local path=prefix .. "/" .. buffer:sub(1,nl-1)
dprint(path)
if fs.exists(path) then
if fs.isDirectory(path) then
if fs.exists(path.."/index") then
s:write(f:read("*a"))
end
local dbuffer = ""
for f in fs.list(path) do
dbuffer = dbuffer..f.."\n"
dprint(f)
end
s:write(dbuffer)
s:close()
else
local f = io.open(path,"rb")
s:write(f:read("*a"))
f:close()
s:close()
end
else
dprint("404")
s:write("file not found")
s:close()
end
end
end
listener = lf
event.listen("net_msg",lf)
end)
end
function stop()
event.ignore("net_msg",listener)
event.ignore("net_msg",process)
end
function debug()
dbug = not dbug
end
function set_path(newpath)
if fs.exists(newpath) then
prefix=newpath
print("prefix = "..newpath)
end
end

View File

@ -0,0 +1,20 @@
local net = require "net"
local event = require "event"
local tArgs = {...}
local host, path = tArgs[1], tArgs[2]
local port = 70
local sep = host:find(":")
if sep then
port=tonumber(host:sub(sep+1))
host=host:sub(1,sep-1)
end
local s = net.open(host,port)
s:write(path.."\n")
repeat
l = s:read(1024)
io.write(l)
os.sleep(0.5)
until s.state == "closed" and l == ""