<!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>[14111] CalendarServer/trunk</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/14111">14111</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-10-27 19:23:27 -0700 (Mon, 27 Oct 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make sure delegate cache values are str's not unicode's. Fix in-memory cache to reject attempts to use
unicode for the key or value to match what the real memcacheclient does.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolstesttest_gatewaypy">CalendarServer/trunk/calendarserver/tools/test/test_gateway.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavmemcacherpy">CalendarServer/trunk/twistedcaldav/memcacher.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavtesttest_memcacherpy">CalendarServer/trunk/twistedcaldav/test/test_memcacher.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavtestutilpy">CalendarServer/trunk/twistedcaldav/test/util.py</a></li>
<li><a href="#CalendarServertrunktxdavcaldavdatastoreschedulingtesttest_processingpy">CalendarServer/trunk/txdav/caldav/datastore/scheduling/test/test_processing.py</a></li>
<li><a href="#CalendarServertrunktxdavwhodelegatespy">CalendarServer/trunk/txdav/who/delegates.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolstesttest_gatewaypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/test/test_gateway.py (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/test/test_gateway.py        2014-10-27 19:08:31 UTC (rev 14110)
+++ CalendarServer/trunk/calendarserver/tools/test/test_gateway.py        2014-10-28 02:23:27 UTC (rev 14111)
</span><span class="lines">@@ -99,7 +99,7 @@
</span><span class="cx">         config.Memcached.Pools.Default.ServerEnabled = False
</span><span class="cx">         ClientFactory.allowTestCache = True
</span><span class="cx">         memcacher.Memcacher.allowTestCache = True
</span><del>-        memcacher.Memcacher.memoryCacheInstance = None
</del><ins>+        memcacher.Memcacher.reset()
</ins><span class="cx">         config.DirectoryAddressBook.Enabled = False
</span><span class="cx">         config.UsePackageTimezones = True
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavmemcacherpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/memcacher.py (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/memcacher.py        2014-10-27 19:08:31 UTC (rev 14110)
+++ CalendarServer/trunk/twistedcaldav/memcacher.py        2014-10-28 02:23:27 UTC (rev 14111)
</span><span class="lines">@@ -38,7 +38,10 @@
</span><span class="cx">     keyNormalizeTranslateTable = string.maketrans(&quot;&quot;.join([chr(i) for i in range(33)]) + chr(0x7F), &quot;_&quot; * 33 + &quot;_&quot;)
</span><span class="cx"> 
</span><span class="cx">     allowTestCache = False
</span><del>-    memoryCacheInstance = None
</del><ins>+    memoryCacheInstance = {
+        True: None,
+        False: None,
+    }
</ins><span class="cx"> 
</span><span class="cx">     class memoryCacher():
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -48,11 +51,23 @@
</span><span class="cx">         memcached may not be running.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        def __init__(self):
</del><ins>+        def __init__(self, pickle=False):
</ins><span class="cx">             self._cache = {} # (value, expireTime, check-and-set identifier)
</span><span class="cx">             self._clock = 0
</span><ins>+            self._pickle = pickle
</ins><span class="cx"> 
</span><ins>+        def _check_key(self, key):
+            if not isinstance(key, str):
+                raise ValueError(&quot;memcache keys must be str type&quot;)
+
+        def _check_value(self, value):
+            if not self._pickle and not isinstance(value, str):
+                raise ValueError(&quot;memcache values must be str type&quot;)
+
</ins><span class="cx">         def add(self, key, value, expireTime=0):
</span><ins>+            self._check_key(key)
+            self._check_value(value)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT or len(str(value)) &gt; Memcacher.MEMCACHE_VALUE_LIMIT:
</span><span class="cx">                 return succeed(False)
</span><span class="cx">             if key not in self._cache:
</span><span class="lines">@@ -64,6 +79,9 @@
</span><span class="cx">                 return succeed(False)
</span><span class="cx"> 
</span><span class="cx">         def set(self, key, value, expireTime=0):
</span><ins>+            self._check_key(key)
+            self._check_value(value)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT or len(str(value)) &gt; Memcacher.MEMCACHE_VALUE_LIMIT:
</span><span class="cx">                 return succeed(False)
</span><span class="cx">             if not expireTime:
</span><span class="lines">@@ -77,6 +95,9 @@
</span><span class="cx">             return succeed(True)
</span><span class="cx"> 
</span><span class="cx">         def checkAndSet(self, key, value, cas, flags=0, expireTime=0):
</span><ins>+            self._check_key(key)
+            self._check_value(value)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT or len(str(value)) &gt; Memcacher.MEMCACHE_VALUE_LIMIT:
</span><span class="cx">                 return succeed(False)
</span><span class="cx">             if not expireTime:
</span><span class="lines">@@ -92,6 +113,8 @@
</span><span class="cx">             return succeed(True)
</span><span class="cx"> 
</span><span class="cx">         def get(self, key, withIdentifier=False):
</span><ins>+            self._check_key(key)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT:
</span><span class="cx">                 value, expires, identifier = (None, 0, &quot;&quot;)
</span><span class="cx">             else:
</span><span class="lines">@@ -106,6 +129,8 @@
</span><span class="cx">                 return succeed((0, value,))
</span><span class="cx"> 
</span><span class="cx">         def delete(self, key):
</span><ins>+            self._check_key(key)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT:
</span><span class="cx">                 return succeed(False)
</span><span class="cx">             try:
</span><span class="lines">@@ -115,6 +140,8 @@
</span><span class="cx">                 return succeed(False)
</span><span class="cx"> 
</span><span class="cx">         def incr(self, key, delta=1):
</span><ins>+            self._check_key(key)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT:
</span><span class="cx">                 return succeed(False)
</span><span class="cx">             value = self._cache.get(key, None)
</span><span class="lines">@@ -130,6 +157,8 @@
</span><span class="cx">             return succeed(value)
</span><span class="cx"> 
</span><span class="cx">         def decr(self, key, delta=1):
</span><ins>+            self._check_key(key)
+
</ins><span class="cx">             if len(key) &gt; Memcacher.MEMCACHE_KEY_LIMIT:
</span><span class="cx">                 return succeed(False)
</span><span class="cx">             value = self._cache.get(key, None)
</span><span class="lines">@@ -224,11 +253,11 @@
</span><span class="cx">             self._memcacheProtocol = self.getCachePool()
</span><span class="cx"> 
</span><span class="cx">         elif config.ProcessType == &quot;Single&quot; or self._noInvalidation or self.allowTestCache:
</span><del>-            # NB no need to pickle the memory cacher as it handles python types natively
-            if Memcacher.memoryCacheInstance is None:
-                Memcacher.memoryCacheInstance = Memcacher.memoryCacher()
-            self._memcacheProtocol = Memcacher.memoryCacheInstance
-            self._pickle = False
</del><ins>+            # The memory cacher handles python types natively, but we need to treat non-str types as an error
+            # if pickling is off, so we use two global memory cachers for each pickle state
+            if Memcacher.memoryCacheInstance[self._pickle] is None:
+                Memcacher.memoryCacheInstance[self._pickle] = Memcacher.memoryCacher(self._pickle)
+            self._memcacheProtocol = Memcacher.memoryCacheInstance[self._pickle]
</ins><span class="cx"> 
</span><span class="cx">         else:
</span><span class="cx">             # NB no need to pickle the null cacher as it handles python types natively
</span><span class="lines">@@ -321,3 +350,11 @@
</span><span class="cx">     def flushAll(self):
</span><span class="cx">         self.log.debug(&quot;Flushing All Cache Tokens&quot;)
</span><span class="cx">         return self._getMemcacheProtocol().flushAll()
</span><ins>+
+
+    @classmethod
+    def reset(cls):
+        &quot;&quot;&quot;
+        Reset the memory cachers
+        &quot;&quot;&quot;
+        cls.memoryCacheInstance = {True: None, False: None}
</ins></span></pre></div>
<a id="CalendarServertrunktwistedcaldavtesttest_memcacherpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/test/test_memcacher.py (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/test/test_memcacher.py        2014-10-27 19:08:31 UTC (rev 14110)
+++ CalendarServer/trunk/twistedcaldav/test/test_memcacher.py        2014-10-28 02:23:27 UTC (rev 14111)
</span><span class="lines">@@ -100,7 +100,7 @@
</span><span class="cx">         for processType in (&quot;Single&quot;, &quot;Combined&quot;,):
</span><span class="cx">             config.ProcessType = processType
</span><span class="cx"> 
</span><del>-            cacher = Memcacher(&quot;testing&quot;, no_invalidation=True)
</del><ins>+            cacher = Memcacher(&quot;testing&quot;, pickle=True, no_invalidation=True)
</ins><span class="cx"> 
</span><span class="cx">             result = yield cacher.set(&quot;akey&quot;, [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, ])
</span><span class="cx">             self.assertTrue(result)
</span><span class="lines">@@ -132,6 +132,15 @@
</span><span class="cx">             self.assertTrue(&quot;\r&quot; not in key)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def test_key_value_str(self):
+
+        config.ProcessType = &quot;Single&quot;
+
+        cacher = Memcacher(&quot;testing&quot;, pickle=False)
+        self.failUnlessRaises(ValueError, cacher.set, &quot;akey&quot;, [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, ])
+        self.failUnlessRaises(ValueError, cacher.set, &quot;akey&quot;, u&quot;abc&quot;)
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_expiration(self):
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavtestutilpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/test/util.py (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/test/util.py        2014-10-27 19:08:31 UTC (rev 14110)
+++ CalendarServer/trunk/twistedcaldav/test/util.py        2014-10-28 02:23:27 UTC (rev 14111)
</span><span class="lines">@@ -136,7 +136,7 @@
</span><span class="cx">         config.Memcached.Pools.Default.ServerEnabled = False
</span><span class="cx">         ClientFactory.allowTestCache = True
</span><span class="cx">         memcacher.Memcacher.allowTestCache = True
</span><del>-        memcacher.Memcacher.memoryCacheInstance = None
</del><ins>+        memcacher.Memcacher.reset()
</ins><span class="cx">         config.DirectoryAddressBook.Enabled = False
</span><span class="cx">         config.UsePackageTimezones = True
</span><span class="cx"> 
</span><span class="lines">@@ -352,7 +352,7 @@
</span><span class="cx">         config.Memcached.Pools.Default.ServerEnabled = False
</span><span class="cx">         ClientFactory.allowTestCache = True
</span><span class="cx">         memcacher.Memcacher.allowTestCache = True
</span><del>-        memcacher.Memcacher.memoryCacheInstance = None
</del><ins>+        memcacher.Memcacher.reset()
</ins><span class="cx">         config.DirectoryAddressBook.Enabled = False
</span><span class="cx">         config.UsePackageTimezones = True
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavcaldavdatastoreschedulingtesttest_processingpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/scheduling/test/test_processing.py (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/scheduling/test/test_processing.py        2014-10-27 19:08:31 UTC (rev 14110)
+++ CalendarServer/trunk/txdav/caldav/datastore/scheduling/test/test_processing.py        2014-10-28 02:23:27 UTC (rev 14111)
</span><span class="lines">@@ -83,7 +83,7 @@
</span><span class="cx">         config.Memcached.Pools.Default.ClientEnabled = False
</span><span class="cx">         config.Memcached.Pools.Default.ServerEnabled = False
</span><span class="cx">         memcacher.Memcacher.allowTestCache = True
</span><del>-        memcacher.Memcacher.memoryCacheInstance = None
</del><ins>+        memcacher.Memcacher.reset()
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhodelegatespy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/delegates.py (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/delegates.py        2014-10-27 19:08:31 UTC (rev 14110)
+++ CalendarServer/trunk/txdav/who/delegates.py        2014-10-28 02:23:27 UTC (rev 14111)
</span><span class="lines">@@ -242,7 +242,7 @@
</span><span class="cx">             return &quot;{}{}:{}#{}&quot;.format(
</span><span class="cx">                 keyname,
</span><span class="cx">                 &quot;-expanded&quot; if expanded else &quot;&quot;,
</span><del>-                uid,
</del><ins>+                uid.encode(&quot;utf-8&quot;),
</ins><span class="cx">                 &quot;write&quot; if readWrite else &quot;read&quot;,
</span><span class="cx">             )
</span><span class="cx"> 
</span><span class="lines">@@ -255,19 +255,19 @@
</span><span class="cx">         def setMembers(self, uid, readWrite, members, expanded):
</span><span class="cx">             return self.set(
</span><span class="cx">                 self._membersKey(uid, readWrite, expanded),
</span><del>-                &quot;,&quot;.join(members),
</del><ins>+                &quot;,&quot;.join(members).encode(&quot;utf-8&quot;),
</ins><span class="cx">             )
</span><span class="cx"> 
</span><span class="cx">         def setMemberships(self, uid, readWrite, memberships):
</span><span class="cx">             return self.set(
</span><span class="cx">                 self._membershipsKey(uid, readWrite),
</span><del>-                &quot;,&quot;.join(memberships),
</del><ins>+                &quot;,&quot;.join(memberships).encode(&quot;utf-8&quot;),
</ins><span class="cx">             )
</span><span class="cx"> 
</span><span class="cx">         @staticmethod
</span><span class="cx">         def _value_decode(value):
</span><span class="cx">             if value:
</span><del>-                return set(value.split(&quot;,&quot;))
</del><ins>+                return set(value.decode(&quot;utf-8&quot;).split(&quot;,&quot;))
</ins><span class="cx">             elif value is None:
</span><span class="cx">                 return None
</span><span class="cx">             else:
</span></span></pre>
</div>
</div>

</body>
</html>