shithub: scripts

Download patch

ref: 2c27c3c16d0d97e89f2d121f3fa78b9ffe8cd969
parent: 2ebee720a8af84e6f1a4d4e390f723526553b5e0
author: kitzman <kitzman@disroot.org>
date: Wed Nov 22 02:53:00 EST 2023

net fixes

--- a/lib/lua/net.lua
+++ b/lib/lua/net.lua
@@ -27,7 +27,7 @@
   -- dial (needs more checks)
   dial = function(self, raddr)
     local cs_fd, clone_fd, data_fd, ctl_fd, proto
-    local data_fid, conn_param, clone_path, addr
+    local data_fid, conn_param, clone_path, data_path, addr
 
     if not(self.clone_path) and not(self.addr) then
       cs_fd = io.open("/net/cs", "r+")
@@ -49,7 +49,7 @@
     addr = self.addr
 
     clone_fd = io.open(clone_path, "r+")
-    if not(cs_fd) then
+    if not(clone_fd) then
       return false, "unable to open clone file"
     end
 
@@ -58,9 +58,12 @@
     local n = clone_fd:write("connect "..addr.."\n")
     clone_fd:flush()
 
-    data_fdin = io.open("/net/"..proto.."/"..data_fid.."/data", "r+")
-    data_fdout = io.open("/net/"..proto.."/"..data_fid.."/data", "a+")
+    data_path = string.match(clone_path, "([a-zA-Z0-9/.]+)/clone")
+    data_path = data_path.."/"..data_fid.."/data"
 
+    data_fdin = io.open(data_path, "r+")
+    data_fdout = io.open(data_path, "a+")
+
     if not(data_fdin) then
       return false, "unable to open data"
     end
@@ -164,7 +167,7 @@
   end,
 
   read_body = function(self)
-    local body_fd, status, buf
+    local body_fd, buf
 
     body_fd = io.open(self.mtpt.."/"..self.web_fid.."/body", "r")
     if not(body_fd) then
@@ -171,14 +174,18 @@
        return false, "body cannot be opened"
     end
 
-    status, buf = body_fd:read("a")
+    buf = body_fd:read("a")
     body_fd:close()
 
-    return status, buf
+    if not(buf) then
+      return false, "unable to read body"
+    end
+
+    return true, buf
   end,
 
   post_body = function(self, body)
-    local body_fd, status, buf
+    local body_fd, ofd
 
     body_fd = io.open(self.mtpt.."/"..self.web_fid.."/postbody", "w")
     if not(body_fd) then
@@ -185,10 +192,14 @@
        return false, "body cannot be opened"
     end
 
-    status, buf = body_fd:write("a")
+    ofd = body_fd:write(body)
     body_fd:close()
 
-    return status, buf
+    if not(ofd) then
+      return false, "unable to write to body fd"
+    end
+
+    return true, nil
   end,
 
   close = function(self)
@@ -212,6 +223,9 @@
 
   get = function(self, url, headers)
     local request, status, data
+    if not(url) then
+      return false, "no url"
+    end
     if not(headers) then
       headers = {}
     end
@@ -234,6 +248,9 @@
 
   post = function(self, url, body, contenttype, headers)
     local request, status, data
+    if not(url) then
+      return false, "no url"
+    end
     if not(headers) then
       headers = {}
     end