From 4ed9528d36cc49ce95073f65f20d2e4efd1897db Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 25 Mar 2021 19:29:16 +0100 Subject: [PATCH] 5x faster reply on 1st req on new conns --- copyparty/httpcli.py | 30 +++++++++++++++++------------- copyparty/httpconn.py | 25 ------------------------- copyparty/httpsrv.py | 25 +++++++++++++++++++++++++ copyparty/up2k.py | 3 ++- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index e01e748a..3598f914 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -52,6 +52,10 @@ class HttpCli(object): if rem.startswith("/") or rem.startswith("../") or "/../" in rem: raise Exception("that was close") + def j2(self, name, **kwargs): + tpl = self.conn.hsrv.j2[name] + return tpl.render(**kwargs) if kwargs else tpl + def run(self): """returns true if connection can be reused""" self.keepalive = False @@ -580,7 +584,7 @@ class HttpCli(object): pwd = "x" # nosec h = {"Set-Cookie": "cppwd={}; Path=/; SameSite=Lax".format(pwd)} - html = self.conn.tpl_msg.render(h1=msg, h2='ack', redir="/") + html = self.j2("msg", h1=msg, h2='ack', redir="/") self.reply(html.encode("utf-8"), headers=h) return True @@ -611,7 +615,8 @@ class HttpCli(object): vpath = "{}/{}".format(self.vpath, sanitized).lstrip("/") esc_paths = [quotep(vpath), html_escape(vpath)] - html = self.conn.tpl_msg.render( + html = self.j2( + "msg", h2='go to /{}'.format(*esc_paths), pre="aight", click=True, @@ -643,7 +648,8 @@ class HttpCli(object): f.write(b"`GRUNNUR`\n") vpath = "{}/{}".format(self.vpath, sanitized).lstrip("/") - html = self.conn.tpl_msg.render( + html = self.j2( + "msg", h2='go to /{}?edit'.format( quotep(vpath), html_escape(vpath) ), @@ -749,7 +755,8 @@ class HttpCli(object): ).encode("utf-8") ) - html = self.conn.tpl_msg.render( + html = self.j2( + "msg", h2='return to /{}'.format( quotep(self.vpath), html_escape(self.vpath) ), @@ -1039,14 +1046,10 @@ class HttpCli(object): def tx_md(self, fs_path): logmsg = "{:4} {} ".format("", self.req) - if "edit2" in self.uparam: - html_path = "web/mde.html" - template = self.conn.tpl_mde - else: - html_path = "web/md.html" - template = self.conn.tpl_md - html_path = os.path.join(E.mod, html_path) + tpl = "mde" if "edit2" in self.uparam else "md" + html_path = os.path.join(E.mod, "web", "{}.html".format(tpl)) + template = self.j2(tpl) st = os.stat(fsenc(fs_path)) # sz_md = st.st_size @@ -1098,7 +1101,7 @@ class HttpCli(object): def tx_mounts(self): rvol = [x + "/" if x else x for x in self.rvol] wvol = [x + "/" if x else x for x in self.wvol] - html = self.conn.tpl_mounts.render(this=self, rvol=rvol, wvol=wvol) + html = self.j2("splash", this=self, rvol=rvol, wvol=wvol) self.reply(html.encode("utf-8")) return True @@ -1372,7 +1375,8 @@ class HttpCli(object): dirs.extend(files) - html = self.conn.tpl_browser.render( + html = self.j2( + "browser", vdir=quotep(self.vpath), vpnodes=vpnodes, files=dirs, diff --git a/copyparty/httpconn.py b/copyparty/httpconn.py index 95c9595b..71ef3728 100644 --- a/copyparty/httpconn.py +++ b/copyparty/httpconn.py @@ -12,23 +12,6 @@ try: except: HAVE_SSL = False -try: - import jinja2 -except ImportError: - print( - """\033[1;31m - you do not have jinja2 installed,\033[33m - choose one of these:\033[0m - * apt install python-jinja2 - * {} -m pip install --user jinja2 - * (try another python version, if you have one) - * (try copyparty.sfx instead) -""".format( - os.path.basename(sys.executable) - ) - ) - sys.exit(1) - from .__init__ import E from .util import Unrecv from .httpcli import HttpCli @@ -57,14 +40,6 @@ class HttpConn(object): self.log_func = hsrv.log self.set_rproxy() - env = jinja2.Environment() - env.loader = jinja2.FileSystemLoader(os.path.join(E.mod, "web")) - self.tpl_mounts = env.get_template("splash.html") - self.tpl_browser = env.get_template("browser.html") - self.tpl_msg = env.get_template("msg.html") - self.tpl_md = env.get_template("md.html") - self.tpl_mde = env.get_template("mde.html") - def set_rproxy(self, ip=None): if ip is None: color = 36 diff --git a/copyparty/httpsrv.py b/copyparty/httpsrv.py index c83e4172..c43dbdd1 100644 --- a/copyparty/httpsrv.py +++ b/copyparty/httpsrv.py @@ -2,10 +2,28 @@ from __future__ import print_function, unicode_literals import os +import sys import time import socket import threading +try: + import jinja2 +except ImportError: + print( + """\033[1;31m + you do not have jinja2 installed,\033[33m + choose one of these:\033[0m + * apt install python-jinja2 + * {} -m pip install --user jinja2 + * (try another python version, if you have one) + * (try copyparty.sfx instead) +""".format( + os.path.basename(sys.executable) + ) + ) + sys.exit(1) + from .__init__ import E, MACOS from .httpconn import HttpConn from .authsrv import AuthSrv @@ -30,6 +48,13 @@ class HttpSrv(object): self.workload_thr_alive = False self.auth = AuthSrv(self.args, self.log) + env = jinja2.Environment() + env.loader = jinja2.FileSystemLoader(os.path.join(E.mod, "web")) + self.j2 = { + x: env.get_template(x + ".html") + for x in ["splash", "browser", "msg", "md", "mde"] + } + cert_path = os.path.join(E.cfg, "cert.pem") if os.path.exists(cert_path): self.cert_path = cert_path diff --git a/copyparty/up2k.py b/copyparty/up2k.py index daef233d..2732fead 100644 --- a/copyparty/up2k.py +++ b/copyparty/up2k.py @@ -232,7 +232,8 @@ class Up2k(object): (ft if v is True else ff if v is False else fv).format(k, str(v)) for k, v in flags.items() ] - self.log(" ".join(sorted(a)) + "\033[0m") + if a: + self.log(" ".join(sorted(a)) + "\033[0m") reg = {} path = os.path.join(ptop, ".hist", "up2k.snap")