<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14401] CalendarServer/trunk/twistedcaldav</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14401">14401</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2015-02-10 14:49:34 -0800 (Tue, 10 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Sync token can optionally include a component derived from configuration, especially so that APNS push topic changes can be detected by clients</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavconfigpy">CalendarServer/trunk/twistedcaldav/config.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavmethodreport_sync_collectionpy">CalendarServer/trunk/twistedcaldav/method/report_sync_collection.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavresourcepy">CalendarServer/trunk/twistedcaldav/resource.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavstdconfigpy">CalendarServer/trunk/twistedcaldav/stdconfig.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavtesttest_configpy">CalendarServer/trunk/twistedcaldav/test/test_config.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavconfigpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/config.py (14400 => 14401)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/config.py        2015-02-10 21:54:08 UTC (rev 14400)
+++ CalendarServer/trunk/twistedcaldav/config.py        2015-02-10 22:49:34 UTC (rev 14401)
</span><span class="lines">@@ -22,8 +22,9 @@
</span><span class="cx">     &quot;config&quot;,
</span><span class="cx"> ]
</span><span class="cx"> 
</span><ins>+import copy
+import hashlib
</ins><span class="cx"> import os
</span><del>-import copy
</del><span class="cx"> 
</span><span class="cx"> class ConfigurationError(RuntimeError):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="lines">@@ -262,6 +263,7 @@
</span><span class="cx"> 
</span><span class="cx">         self._updating = False
</span><span class="cx">         self._dirty = False
</span><ins>+        self._cachedSyncToken = None
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def load(self, configFile):
</span><span class="lines">@@ -297,9 +299,78 @@
</span><span class="cx">     def reset(self):
</span><span class="cx">         self._data = ConfigDict(copy.deepcopy(self._provider.getDefaults()))
</span><span class="cx">         self._dirty = True
</span><ins>+        self._syncTokenKeys = []
+        self._cachedSyncToken = None
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def getKeyPath(self, keyPath):
+        &quot;&quot;&quot;
+        Allows the getting of arbitrary nested dictionary keys via a single
+        dot-separated string.  For example, getKeyPath(self, &quot;foo.bar.baz&quot;)
+        would fetch parent[&quot;foo&quot;][&quot;bar&quot;][&quot;baz&quot;].  If any of the keys don't
+        exist, None is returned instead.
+
+        @param keyPath: a dot-delimited string specifying the path of keys to
+            traverse
+        @type keyPath: C{str}
+        @return: the value at keyPath
+        &quot;&quot;&quot;
+        parent = self
+        parts = keyPath.split(&quot;.&quot;)
+        for part in parts[:-1]:
+            child = parent.get(part, None)
+            if child is None:
+                return None
+            parent = child
+        return parent.get(parts[-1], None)
+
+
+    def addSyncTokenKey(self, keyPath):
+        &quot;&quot;&quot;
+        Indicates the specified key should be taken into account when generating
+        the sync token.  Also invalidates the (possibly) cached syncToken.
+
+        @param keyPath: a dot-delimited string specifying the path of keys to
+            traverse
+        @type keyPath: C{str}
+        &quot;&quot;&quot;
+        if keyPath not in self._syncTokenKeys:
+            self._syncTokenKeys.append(keyPath)
+        self._cachedSyncToken = None
+
+
+    def syncToken(self):
+        &quot;&quot;&quot;
+        Iterates the previously registered keys (sorted, so the order in which
+        the keys were registered doesn't affect the hash) and generates an MD5
+        hash of the combined values.  The hash is cached, and is invalidated
+        during a reload or if invalidateSyncToken is called.o
+
+        @return: the sync token
+        @rtype: C{str}
+        &quot;&quot;&quot;
+        if self._cachedSyncToken is None:
+            pieces = []
+            self._syncTokenKeys.sort()
+            for key in self._syncTokenKeys:
+                value = self.getKeyPath(key)
+                if value is None:
+                    value = &quot;&quot;
+                pieces.append(key + &quot;:&quot; + str(value))
+            whole = &quot;|&quot;.join(pieces)
+            self._cachedSyncToken = hashlib.md5(whole).hexdigest()
+        return self._cachedSyncToken
+
+
+    def invalidateSyncToken(self):
+        &quot;&quot;&quot;
+        Invalidates the cached copy of the sync token.
+        &quot;&quot;&quot;
+        self._cachedSyncToken = None
+
+
+
</ins><span class="cx"> def mergeData(oldData, newData):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Merge two ConfigDict objects; oldData will be updated with all the keys
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavmethodreport_sync_collectionpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/method/report_sync_collection.py (14400 => 14401)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/method/report_sync_collection.py        2015-02-10 21:54:08 UTC (rev 14400)
+++ CalendarServer/trunk/twistedcaldav/method/report_sync_collection.py        2015-02-10 22:49:34 UTC (rev 14401)
</span><span class="lines">@@ -87,7 +87,7 @@
</span><span class="cx">     # the child resource loop and supply those to the checkPrivileges on each child.
</span><span class="cx">     filteredaces = (yield self.inheritedACEsforChildren(request))
</span><span class="cx"> 
</span><del>-    changed, removed, notallowed, newtoken = yield self.whatchanged(sync_collection.sync_token, depth)
</del><ins>+    changed, removed, notallowed, newtoken, resourceChanged = yield self.whatchanged(sync_collection.sync_token, depth)
</ins><span class="cx"> 
</span><span class="cx">     # Now determine which valid resources are readable and which are not
</span><span class="cx">     ok_resources = []
</span><span class="lines">@@ -105,6 +105,9 @@
</span><span class="cx">             inherited_aces=filteredaces
</span><span class="cx">         )
</span><span class="cx"> 
</span><ins>+    if resourceChanged:
+        ok_resources.append((self, request.uri))
+
</ins><span class="cx">     for child, child_uri in ok_resources:
</span><span class="cx">         href = element.HRef.fromString(child_uri)
</span><span class="cx">         try:
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavresourcepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/resource.py (14400 => 14401)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/resource.py        2015-02-10 21:54:08 UTC (rev 14400)
+++ CalendarServer/trunk/twistedcaldav/resource.py        2015-02-10 22:49:34 UTC (rev 14401)
</span><span class="lines">@@ -1183,15 +1183,25 @@
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def whatchanged(self, client_token, depth):
</span><ins>+
+        client_data_token = None
+        client_config_token = None
+
+        if client_token:
+            if &quot;/&quot; in client_token:
+                client_data_token, client_config_token = client_token.split(&quot;/&quot;)
+            else:
+                client_data_token = client_token
+
</ins><span class="cx">         current_token = (yield self.getSyncToken())
</span><span class="cx">         current_uuid, current_revision = current_token[6:].split(&quot;_&quot;, 1)
</span><span class="cx">         current_revision = int(current_revision)
</span><span class="cx"> 
</span><del>-        if client_token:
</del><ins>+        if client_data_token:
</ins><span class="cx">             try:
</span><del>-                if not client_token.startswith(&quot;data:,&quot;):
</del><ins>+                if not client_data_token.startswith(&quot;data:,&quot;):
</ins><span class="cx">                     raise ValueError
</span><del>-                caluuid, revision = client_token[6:].split(&quot;_&quot;, 1)
</del><ins>+                caluuid, revision = client_data_token[6:].split(&quot;_&quot;, 1)
</ins><span class="cx">                 revision = int(revision)
</span><span class="cx"> 
</span><span class="cx">                 # Check client token validity
</span><span class="lines">@@ -1217,9 +1227,19 @@
</span><span class="cx">                 &quot;Sync token not recognized&quot;,
</span><span class="cx">             ))
</span><span class="cx"> 
</span><del>-        returnValue((changed, removed, notallowed, current_token))
</del><ins>+        if config.EnableConfigSyncToken:
+            # Append the app-level portion of sync token (e.g. derived from config)
+            newConfigToken = config.syncToken()
+            current_token = &quot;{}/{}&quot;.format(current_token, newConfigToken)
</ins><span class="cx"> 
</span><ins>+            # If the config token changed, note that in the returned tuple
+            resourceChanged = (newConfigToken != client_config_token)
+        else:
+            resourceChanged = False
</ins><span class="cx"> 
</span><ins>+        returnValue((changed, removed, notallowed, current_token, resourceChanged))
+
+
</ins><span class="cx">     def _indexWhatChanged(self, revision, depth):
</span><span class="cx">         # Now handled directly by newstore
</span><span class="cx">         raise NotImplementedError
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavstdconfigpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py (14400 => 14401)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/stdconfig.py        2015-02-10 21:54:08 UTC (rev 14400)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py        2015-02-10 22:49:34 UTC (rev 14401)
</span><span class="lines">@@ -475,6 +475,7 @@
</span><span class="cx">     &quot;EnableAddMember&quot;             : True, # POST ;add-member extension
</span><span class="cx">     &quot;EnableSyncReport&quot;            : True, # REPORT collection-sync
</span><span class="cx">     &quot;EnableSyncReportHome&quot;        : True, # REPORT collection-sync on home collections
</span><ins>+    &quot;EnableConfigSyncToken&quot;       : False, # Sync token includes config component
</ins><span class="cx">     &quot;EnableWellKnown&quot;             : True, # /.well-known resource
</span><span class="cx">     &quot;EnableCalendarQueryExtended&quot; : True, # Extended calendar-query REPORT
</span><span class="cx"> 
</span><span class="lines">@@ -1554,6 +1555,12 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> def _updateNotifications(configDict, reloading=False):
</span><ins>+
+    # These three keys go into producing the config sync token
+    config.addSyncTokenKey(&quot;Notifications.Services.APNS.Enabled&quot;)
+    config.addSyncTokenKey(&quot;Notifications.Services.APNS.CalDAV.Topic&quot;)
+    config.addSyncTokenKey(&quot;Notifications.Services.APNS.CardDAV.Topic&quot;)
+
</ins><span class="cx">     # Reloading not supported -- requires process running as root
</span><span class="cx">     if reloading:
</span><span class="cx">         return
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavtesttest_configpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/test/test_config.py (14400 => 14401)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/test/test_config.py        2015-02-10 21:54:08 UTC (rev 14400)
+++ CalendarServer/trunk/twistedcaldav/test/test_config.py        2015-02-10 22:49:34 UTC (rev 14401)
</span><span class="lines">@@ -66,6 +66,60 @@
</span><span class="cx">     &lt;/dict&gt;
</span><span class="cx">   &lt;/dict&gt;
</span><span class="cx"> 
</span><ins>+    &lt;key&gt;Notifications&lt;/key&gt;
+    &lt;dict&gt;
+
+      &lt;key&gt;Services&lt;/key&gt;
+      &lt;dict&gt;
+
+        &lt;key&gt;AMP&lt;/key&gt;
+        &lt;dict&gt;
+          &lt;key&gt;Enabled&lt;/key&gt;
+          &lt;true/&gt;
+          &lt;key&gt;Port&lt;/key&gt;
+          &lt;integer&gt;62311&lt;/integer&gt;
+          &lt;key&gt;EnableStaggering&lt;/key&gt;
+          &lt;false/&gt;
+          &lt;key&gt;StaggerSeconds&lt;/key&gt;
+          &lt;integer&gt;3&lt;/integer&gt;
+        &lt;/dict&gt;
+
+        &lt;key&gt;APNS&lt;/key&gt;
+        &lt;dict&gt;
+          &lt;key&gt;CalDAV&lt;/key&gt;
+          &lt;dict&gt;
+            &lt;key&gt;AuthorityChainPath&lt;/key&gt;
+            &lt;string&gt;com.apple.calendar.chain.pem&lt;/string&gt;
+            &lt;key&gt;CertificatePath&lt;/key&gt;
+            &lt;string&gt;com.apple.calendar.cert.pem&lt;/string&gt;
+            &lt;key&gt;PrivateKeyPath&lt;/key&gt;
+            &lt;string&gt;com.apple.calendar.key.pem&lt;/string&gt;
+            &lt;key&gt;Topic&lt;/key&gt;
+            &lt;string&gt;calendar-topic&lt;/string&gt;
+            &lt;key&gt;Passphrase&lt;/key&gt;
+            &lt;string&gt;password&lt;/string&gt;
+          &lt;/dict&gt;
+          &lt;key&gt;CardDAV&lt;/key&gt;
+          &lt;dict&gt;
+            &lt;key&gt;AuthorityChainPath&lt;/key&gt;
+            &lt;string&gt;com.apple.contact.chain.pem&lt;/string&gt;
+            &lt;key&gt;CertificatePath&lt;/key&gt;
+            &lt;string&gt;com.apple.contact.cert.pem&lt;/string&gt;
+            &lt;key&gt;PrivateKeyPath&lt;/key&gt;
+            &lt;string&gt;com.apple.contact.key.pem&lt;/string&gt;
+            &lt;key&gt;Topic&lt;/key&gt;
+            &lt;string&gt;contact-topic&lt;/string&gt;
+            &lt;key&gt;Passphrase&lt;/key&gt;
+            &lt;string&gt;password&lt;/string&gt;
+          &lt;/dict&gt;
+          &lt;key&gt;Enabled&lt;/key&gt;
+          &lt;true/&gt;
+        &lt;/dict&gt;
+
+      &lt;/dict&gt;
+
+    &lt;/dict&gt;
+
</ins><span class="cx"> &lt;/dict&gt;
</span><span class="cx"> &lt;/plist&gt;
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="lines">@@ -635,3 +689,35 @@
</span><span class="cx">         config.load(self.testMaster)
</span><span class="cx">         self.assertEquals(config.HTTPPort, 9008)
</span><span class="cx">         self.assertEquals(config.SSLPort, 8443)
</span><ins>+
+
+    def testSyncToken(self):
+        config.load(self.testConfig)
+
+        # no sync token keys specified; need to empty this array here because
+        # stdconfig is registering keys automatically
+        config._syncTokenKeys = []
+        self.assertEquals(&quot;d41d8cd98f00b204e9800998ecf8427e&quot;, config.syncToken())
+
+        # add sync token keys (some with multiple levels)
+        config.addSyncTokenKey(&quot;DefaultLogLevel&quot;)
+        config.addSyncTokenKey(&quot;Notifications.Services.APNS.Enabled&quot;)
+        config.addSyncTokenKey(&quot;Notifications.Services.APNS.CalDAV.Topic&quot;)
+        config.addSyncTokenKey(&quot;Notifications.Services.APNS.CardDAV.Topic&quot;)
+        self.assertEquals(&quot;7473205187d7a6ff0c61a2b6b04053c5&quot;, config.syncToken())
+
+        # modify a sync token key value
+        config.Notifications.Services.APNS.CalDAV.Topic = &quot;changed&quot;
+        # direct manipulation of config requires explicit invalidation
+        self.assertEquals(&quot;7473205187d7a6ff0c61a2b6b04053c5&quot;, config.syncToken())
+        config.invalidateSyncToken()
+        self.assertEquals(&quot;4cdbb3841625d001dc768439f5a88cba&quot;, config.syncToken())
+
+        # add a non existent key (not an error because it could exist later)
+        config.addSyncTokenKey(&quot;Notifications.Services.APNS.CalDAV.NonExistent&quot;)
+        config.invalidateSyncToken()
+        self.assertEquals(&quot;2ffb128cee5a4b217cef82fd31ae7767&quot;, config.syncToken())
+
+        # reload automatically invalidates
+        config.reload()
+        self.assertEquals(&quot;a1c46c5aff1899658dac033ee8520b07&quot;, config.syncToken())
</ins></span></pre>
</div>
</div>

</body>
</html>