From 46b9a8c655434a03f339152816e9aeceb052a4a3 Mon Sep 17 00:00:00 2001 From: Izaya Date: Mon, 19 Feb 2018 21:04:22 +1100 Subject: [PATCH] added the frequest stuff, will document protocol later but it's basically gopher minus hypertext --- FRequest/OpenOS/etc/rc.d/fserv.lua | 68 ++++++++++++++++++++++++++++++ FRequest/OpenOS/usr/bin/fget.lua | 20 +++++++++ 2 files changed, 88 insertions(+) create mode 100644 FRequest/OpenOS/etc/rc.d/fserv.lua create mode 100644 FRequest/OpenOS/usr/bin/fget.lua diff --git a/FRequest/OpenOS/etc/rc.d/fserv.lua b/FRequest/OpenOS/etc/rc.d/fserv.lua new file mode 100644 index 0000000..4e76657 --- /dev/null +++ b/FRequest/OpenOS/etc/rc.d/fserv.lua @@ -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 diff --git a/FRequest/OpenOS/usr/bin/fget.lua b/FRequest/OpenOS/usr/bin/fget.lua new file mode 100644 index 0000000..1edc610 --- /dev/null +++ b/FRequest/OpenOS/usr/bin/fget.lua @@ -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 == ""