diff --git a/bin/zmq-recv.py b/bin/zmq-recv.py index cbf0b5cf..72fa24f7 100755 --- a/bin/zmq-recv.py +++ b/bin/zmq-recv.py @@ -23,6 +23,9 @@ run this script with "rep" and run copyparty with this: --xm t3,zmq:req:tcp://localhost:5555 +note: to conditionally block uploads based on message contents, +use rep_server to answer with "return 1" and run copyparty with + --xau t3,c,zmq:req:tcp://localhost:5555 """ @@ -56,7 +59,9 @@ def rep_server(): sck.bind("tcp://*:5555") while True: print("copyparty says %r" % (sck.recv_string(),)) - sck.send(b"thx") + reply = b"thx" + # reply = b"return 1" # non-zero to block an upload + sck.send(reply) mode = sys.argv[1].lower() if len(sys.argv) > 1 else "" diff --git a/copyparty/util.py b/copyparty/util.py index a0c886f0..c17c451e 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -3482,7 +3482,7 @@ def _zmq_hook( msg: str, wait: float, sp_ka: dict[str, Any], -) -> str: +) -> tuple[int, str]: import zmq try: @@ -3493,6 +3493,7 @@ def _zmq_hook( mtx = ZMQ["mtx"] ret = "" + nret = 0 t0 = time.time() if verbose and log: log("hook(%s) %r entering zmq-main-lock" % (src, cmd), 6) @@ -3554,6 +3555,10 @@ def _zmq_hook( log("hook(%s) %r awaiting ack from req" % (src, cmd), 6) try: ret = sck.recv().decode("utf-8", "replace") + if ret.startswith("return "): + m = re.search("^return ([0-9]+)", ret[:12]) + if m: + nret = int(m.group(1)) except: sck.close() del ZMQ[cmd] # bad state; must reset @@ -3567,7 +3572,7 @@ def _zmq_hook( if wait > 0: time.sleep(wait) - return ret + return nret, ret def _runhook( @@ -3614,12 +3619,9 @@ def _runhook( arg = txt or ap if acmd[0].startswith("zmq:"): - zs = "zmq-error" - try: - zs = _zmq_hook(log, verbose, src, acmd[0][4:].lower(), arg, wait, sp_ka) - except Exception as ex: - if log: - log("zeromq failed: %r" % (ex,)) + zi, zs = _zmq_hook(log, verbose, src, acmd[0][4:].lower(), arg, wait, sp_ka) + if zi: + raise Exception("zmq says %d" % (zi,)) return {"rc": 0, "stdout": zs} acmd += [arg] diff --git a/tests/util.py b/tests/util.py index 53ee9e16..1cf01f00 100644 --- a/tests/util.py +++ b/tests/util.py @@ -141,7 +141,7 @@ def __init__(self, a=None, v=None, c=None, **ka0): ex = "hash_mt hsortn safe_dedup srch_time u2abort u2j u2sz" ka.update(**{k: 1 for k in ex.split()}) - ex = "au_vol dl_list mtab_age reg_cap s_thead s_tbody th_convt" + ex = "au_vol dl_list mtab_age reg_cap s_thead s_tbody th_convt ups_who" ka.update(**{k: 9 for k in ex.split()}) ex = "db_act k304 loris no304 re_maxage rproxy rsp_jtr rsp_slp s_wr_slp snap_wri theme themes turbo"