[Calypso] [PATCH] Log addition user-agent info and improve commit messages

Petter Reinholdtsen pere at hungry.com
Thu Jan 28 02:09:19 PST 2016


In my set of rebased commits from chrysn, there are three commits
updating the commit messages that modify the same code and build on each
other.  I decided to merge them into one to make it easier to review and
commit.  The first two commits are from chrysn, and the last one is me
fixing a bug introduced by the "fix formatting of commit messages".

Should I submit these for review on as three commits instead, or as one
commit with a consistent commit message, or what?

log additional user-agent information

when a request comes from a web browser, the User-Agent in the request
header is the user's web browser, but what's more important in this
situation is which javascript program (ie web site) the browser had
open. the relevant information is always encoded in the Origin header
(for all modern browser's requests), and some clients set their own
version etc in the X-Client header.

fix formatting of commit messages

this fixes both erronous checks for context variables (which are always
present, just sometimes None) and superfluous line breaks (which are now
normalized away at the end).

Handle non-existing context.

Fixes previos which broke file import, where the context variables are
missing.

diff --git a/calypso/__init__.py b/calypso/__init__.py
index 38a08d7..280d879 100644
--- a/calypso/__init__.py
+++ b/calypso/__init__.py
@@ -83,7 +83,12 @@ def _check(request, function):
     # Also send UNAUTHORIZED if there's no collection. Otherwise one
     # could probe the server for (non-)existing collections.
     if request.server.acl.has_right(owner, user, password):
-        function(request, context={"user": user, "user-agent": request.headers.get("User-Agent", None)})
+        function(request, context={
+                "user": user,
+                "user-agent": request.headers.get("User-Agent", None),
+                "x-client": request.headers.get("X-client", None),
+                "origin": request.headers.get("Origin", None)
+                })
     else:
         request.send_calypso_response(client.UNAUTHORIZED, 0)
         request.send_header(
diff --git a/calypso/webdav.py b/calypso/webdav.py
index f6b84ab..f96b588 100644
--- a/calypso/webdav.py
+++ b/calypso/webdav.py
@@ -328,7 +328,7 @@ class Collection(object):
         args = ["git", "commit", "--allow-empty"]
         env = {}
 
-        message = context.get('action', 'other action')
+        message = context.get('action', 'other action') + "\n\n"
 
         if "user" in context:
             # use environment variables instead of --author to avoid git
@@ -339,10 +339,14 @@ class Collection(object):
             # information explicitly in the config file. (slicing it in after
             # the git command as position is important with git arguments)
             args[1:1] = ["-c", "advice.implicitIdentity=false"]
-        if "user-agent" in context:
-            message += u"\n\nUser-Agent: %r"%context['user-agent']
-
-        args.extend(["-m", message.encode('utf8')])
+        if 'user-agent' in context and context['user-agent']:
+            message += u"User-Agent: %r\n"%context['user-agent']
+        if 'x-client' in context and context['x-client']:
+            message += u"X-Client: %r\n"%context['x-client'] # set by web clients like carddavmate / caldavzap
+        if 'origin' in context and context['origin']:
+            message += u"Origin: %r\n"%context['origin'] # set by everything that does CORS XHR
+
+        args.extend(["-m", message.encode('utf8').rstrip("\n") + "\n"])
 
         subprocess.check_call(args, cwd=self.path, env=env)
 
--
Happy hacking
Petter Reinholdtsen


More information about the Calypso mailing list