Skip to content

Commit

Permalink
decode and log request URLs; closes #125
Browse files Browse the repository at this point in the history
as processing of a HTTP request begins (GET, HEAD, PUT, POST, ...),
the original query line is printed in its encoded form. This makes
debugging easier, since there is no ambiguity in how the client
phrased its request.

however, this results in very opaque logs for non-ascii languages;
basically a wall of percent-encoded characters. Avoid this issue
by printing an additional log-message if the URL contains `%`,
immediately below the original url-encoded entry.

also fix tests on macos, and an unrelated bad logmsg in up2k
  • Loading branch information
9001 committed Dec 15, 2024
1 parent 18c6559 commit 73f7249
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
22 changes: 21 additions & 1 deletion copyparty/httpcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,8 @@ def handle_get(self) -> bool:
logmsg += " [\033[36m" + rval + "\033[0m]"

self.log(logmsg)
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

# "embedded" resources
if self.vpath.startswith(".cpr"):
Expand Down Expand Up @@ -1381,6 +1383,8 @@ def tx_rss(self) -> bool:
def handle_propfind(self) -> bool:
if self.do_log:
self.log("PFIND %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

if self.args.no_dav:
raise Pebkac(405, "WebDAV is disabled in server config")
Expand Down Expand Up @@ -1562,6 +1566,8 @@ def handle_propfind(self) -> bool:
def handle_proppatch(self) -> bool:
if self.do_log:
self.log("PPATCH %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

if self.args.no_dav:
raise Pebkac(405, "WebDAV is disabled in server config")
Expand Down Expand Up @@ -1617,6 +1623,8 @@ def handle_proppatch(self) -> bool:
def handle_lock(self) -> bool:
if self.do_log:
self.log("LOCK %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

if self.args.no_dav:
raise Pebkac(405, "WebDAV is disabled in server config")
Expand Down Expand Up @@ -1682,6 +1690,8 @@ def handle_lock(self) -> bool:
def handle_unlock(self) -> bool:
if self.do_log:
self.log("UNLOCK %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

if self.args.no_dav:
raise Pebkac(405, "WebDAV is disabled in server config")
Expand All @@ -1699,6 +1709,8 @@ def handle_mkcol(self) -> bool:

if self.do_log:
self.log("MKCOL %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

try:
return self._mkdir(self.vpath, True)
Expand Down Expand Up @@ -1750,6 +1762,8 @@ def send_chunk(self, txt: str, enc: str, bmax: int) -> str:
def handle_options(self) -> bool:
if self.do_log:
self.log("OPTIONS %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

oh = self.out_headers
oh["Allow"] = ", ".join(self.conn.hsrv.mallow)
Expand All @@ -1765,10 +1779,14 @@ def handle_options(self) -> bool:

def handle_delete(self) -> bool:
self.log("DELETE %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))
return self.handle_rm([])

def handle_put(self) -> bool:
self.log("PUT %s @%s" % (self.req, self.uname))
self.log("PUT %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

if not self.can_write:
t = "user %s does not have write-access under /%s"
Expand All @@ -1787,6 +1805,8 @@ def handle_put(self) -> bool:

def handle_post(self) -> bool:
self.log("POST %s @%s" % (self.req, self.uname))
if "%" in self.req:
self.log(" `-- %r" % (self.vpath,))

if self.headers.get("expect", "").lower() == "100-continue":
try:
Expand Down
2 changes: 1 addition & 1 deletion copyparty/up2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -3937,7 +3937,7 @@ def _handle_rm(
if jrem == rem:
if job["ptop"] != ptop:
t = "job.ptop [%s] != vol.ptop [%s] ??"
raise Exception(t % (job["ptop"] != ptop))
raise Exception(t % (job["ptop"], ptop))
partial = vn.canonical(vjoin(job["prel"], job["tnam"]))
break
if partial:
Expand Down
8 changes: 0 additions & 8 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ def eprint(*a, **ka):
sys.stderr.flush()


if MACOS:
import posixpath

posixpath.islink = nah
os.path.islink = nah
# 25% faster; until any tests do symlink stuff


from copyparty.__main__ import init_E
from copyparty.broker_thr import BrokerThr
from copyparty.ico import Ico
Expand Down

0 comments on commit 73f7249

Please sign in to comment.