From bac301ed66ffc733054a70ab26dd61a4848aa191 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 12 Jul 2021 00:15:13 +0200 Subject: [PATCH] get rid of iffy default-args --- bin/copyparty-fuse-streaming.py | 14 +++++++------- bin/copyparty-fuse.py | 21 ++++++++++++--------- copyparty/authsrv.py | 8 ++++---- copyparty/httpcli.py | 19 ++++++++++--------- copyparty/szip.py | 2 +- copyparty/th_srv.py | 2 +- copyparty/up2k.py | 4 ++-- copyparty/util.py | 2 +- tests/test_httpcli.py | 6 +++--- tests/test_vfs.py | 4 ++-- tests/util.py | 1 - 11 files changed, 43 insertions(+), 40 deletions(-) diff --git a/bin/copyparty-fuse-streaming.py b/bin/copyparty-fuse-streaming.py index 266cdc71..a3ff6846 100755 --- a/bin/copyparty-fuse-streaming.py +++ b/bin/copyparty-fuse-streaming.py @@ -345,7 +345,7 @@ class Gateway(object): except: pass - def sendreq(self, *args, headers={}, **kwargs): + def sendreq(self, meth, path, headers, **kwargs): if self.password: headers["Cookie"] = "=".join(["cppwd", self.password]) @@ -354,21 +354,21 @@ class Gateway(object): if c.rx_path: raise Exception() - c.request(*list(args), headers=headers, **kwargs) + c.request(meth, path, headers=headers, **kwargs) c.rx = c.getresponse() return c except: tid = threading.current_thread().ident dbg( - "\033[1;37;44mbad conn {:x}\n {}\n {}\033[0m".format( - tid, " ".join(str(x) for x in args), c.rx_path if c else "(null)" + "\033[1;37;44mbad conn {:x}\n {} {}\n {}\033[0m".format( + tid, meth, path, c.rx_path if c else "(null)" ) ) self.closeconn(c) c = self.getconn() try: - c.request(*list(args), headers=headers, **kwargs) + c.request(meth, path, headers=headers, **kwargs) c.rx = c.getresponse() return c except: @@ -386,7 +386,7 @@ class Gateway(object): path = dewin(path) web_path = self.quotep("/" + "/".join([self.web_root, path])) + "?dots" - c = self.sendreq("GET", web_path) + c = self.sendreq("GET", web_path, {}) if c.rx.status != 200: self.closeconn(c) log( @@ -440,7 +440,7 @@ class Gateway(object): ) ) - c = self.sendreq("GET", web_path, headers={"Range": hdr_range}) + c = self.sendreq("GET", web_path, {"Range": hdr_range}) if c.rx.status != http.client.PARTIAL_CONTENT: self.closeconn(c) raise Exception( diff --git a/bin/copyparty-fuse.py b/bin/copyparty-fuse.py index 7d3fba36..c05b90ee 100755 --- a/bin/copyparty-fuse.py +++ b/bin/copyparty-fuse.py @@ -54,10 +54,13 @@ MACOS = platform.system() == "Darwin" info = log = dbg = None -print("{} v{} @ {}".format( - platform.python_implementation(), - ".".join([str(x) for x in sys.version_info]), - sys.executable)) +print( + "{} v{} @ {}".format( + platform.python_implementation(), + ".".join([str(x) for x in sys.version_info]), + sys.executable, + ) +) try: @@ -299,14 +302,14 @@ class Gateway(object): except: pass - def sendreq(self, *args, headers={}, **kwargs): + def sendreq(self, meth, path, headers, **kwargs): tid = get_tid() if self.password: headers["Cookie"] = "=".join(["cppwd", self.password]) try: c = self.getconn(tid) - c.request(*list(args), headers=headers, **kwargs) + c.request(meth, path, headers=headers, **kwargs) return c.getresponse() except: dbg("bad conn") @@ -314,7 +317,7 @@ class Gateway(object): self.closeconn(tid) try: c = self.getconn(tid) - c.request(*list(args), headers=headers, **kwargs) + c.request(meth, path, headers=headers, **kwargs) return c.getresponse() except: info("http connection failed:\n" + traceback.format_exc()) @@ -331,7 +334,7 @@ class Gateway(object): path = dewin(path) web_path = self.quotep("/" + "/".join([self.web_root, path])) + "?dots&ls" - r = self.sendreq("GET", web_path) + r = self.sendreq("GET", web_path, {}) if r.status != 200: self.closeconn() log( @@ -368,7 +371,7 @@ class Gateway(object): ) ) - r = self.sendreq("GET", web_path, headers={"Range": hdr_range}) + r = self.sendreq("GET", web_path, {"Range": hdr_range}) if r.status != http.client.PARTIAL_CONTENT: self.closeconn() raise Exception( diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index ef6bebb6..9021e57a 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -16,7 +16,7 @@ from .util import IMPLICATIONS, uncyg, undot, Pebkac, fsdec, fsenc, statdir class VFS(object): """single level in the virtual fs""" - def __init__(self, log, realpath, vpath, uread=[], uwrite=[], uadm=[], flags={}): + def __init__(self, log, realpath, vpath, uread, uwrite, uadm, flags): self.log = log self.realpath = realpath # absolute path on host filesystem self.vpath = vpath # absolute path in the virtual filesystem @@ -81,7 +81,7 @@ class VFS(object): # leaf does not exist; create and keep permissions blank vp = "{}/{}".format(self.vpath, dst).lstrip("/") - vn = VFS(self.log, src, vp) + vn = VFS(self.log, src, vp, [], [], [], {}) vn.dbv = self.dbv or self self.nodes[dst] = vn return vn @@ -497,10 +497,10 @@ class AuthSrv(object): if not mount: # -h says our defaults are CWD at root and read/write for everyone - vfs = VFS(self.log_func, os.path.abspath("."), "", ["*"], ["*"]) + vfs = VFS(self.log_func, os.path.abspath("."), "", ["*"], ["*"], ["*"], {}) elif "" not in mount: # there's volumes but no root; make root inaccessible - vfs = VFS(self.log_func, None, "") + vfs = VFS(self.log_func, None, "", [], [], [], {}) vfs.flags["d2d"] = True maxdepth = 0 diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 8d015829..cc7a83cc 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -227,7 +227,7 @@ class HttpCli(object): except Pebkac: return False - def send_headers(self, length, status=200, mime=None, headers={}): + def send_headers(self, length, status=200, mime=None, headers=None): response = ["{} {} {}".format(self.http_ver, status, HTTPCODE[status])] if length is not None: @@ -237,7 +237,8 @@ class HttpCli(object): response.append("Connection: " + ("Keep-Alive" if self.keepalive else "Close")) # headers{} overrides anything set previously - self.out_headers.update(headers) + if headers: + self.out_headers.update(headers) # default to utf8 html if no content-type is set if not mime: @@ -254,7 +255,7 @@ class HttpCli(object): except: raise Pebkac(400, "client d/c while replying headers") - def reply(self, body, status=200, mime=None, headers={}): + def reply(self, body, status=200, mime=None, headers=None): # TODO something to reply with user-supplied values safely self.send_headers(len(body), status, mime, headers) @@ -270,7 +271,7 @@ class HttpCli(object): self.log(body.rstrip()) self.reply(b"
" + body.encode("utf-8") + b"\r\n", *list(args), **kwargs)
 
-    def urlq(self, add={}, rm=[]):
+    def urlq(self, add, rm):
         """
         generates url query based on uparam (b, pw, all others)
         removing anything in rm, adding pairs in add
@@ -795,7 +796,7 @@ class HttpCli(object):
         vfs, rem = self.asrv.vfs.get(self.vpath, self.uname, False, True)
         self._assert_safe_rem(rem)
 
-        sanitized = sanitize_fn(new_dir)
+        sanitized = sanitize_fn(new_dir, "", [])
 
         if not nullwrite:
             fdir = os.path.join(vfs.realpath, rem)
@@ -832,7 +833,7 @@ class HttpCli(object):
         if not new_file.endswith(".md"):
             new_file += ".md"
 
-        sanitized = sanitize_fn(new_file)
+        sanitized = sanitize_fn(new_file, "", [])
 
         if not nullwrite:
             fdir = os.path.join(vfs.realpath, rem)
@@ -865,7 +866,7 @@ class HttpCli(object):
                 if p_file and not nullwrite:
                     fdir = os.path.join(vfs.realpath, rem)
                     fname = sanitize_fn(
-                        p_file, bad=[".prologue.html", ".epilogue.html"]
+                        p_file, "", [".prologue.html", ".epilogue.html"]
                     )
 
                     if not os.path.isdir(fsenc(fdir)):
@@ -1423,7 +1424,7 @@ class HttpCli(object):
         return True
 
     def tx_mounts(self):
-        suf = self.urlq(rm=["h"])
+        suf = self.urlq({}, ["h"])
         rvol, wvol, avol = [
             [("/" + x).rstrip("/") + "/" for x in y]
             for y in [self.rvol, self.wvol, self.avol]
@@ -1634,7 +1635,7 @@ class HttpCli(object):
         if self.writable:
             perms.append("write")
 
-        url_suf = self.urlq()
+        url_suf = self.urlq({}, [])
         is_ls = "ls" in self.uparam
 
         tpl = "browser"
diff --git a/copyparty/szip.py b/copyparty/szip.py
index 2b4c189e..b4a7afdb 100644
--- a/copyparty/szip.py
+++ b/copyparty/szip.py
@@ -89,7 +89,7 @@ def gen_hdr(h_pos, fn, sz, lastmod, utf8, crc32, pre_crc):
     ret += spack(b"