<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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, #msg p { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul { overflow: auto; }
#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>
<title>[2186] CalendarServer/branches/release/CalendarServer-1.2-dev/twistedcaldav
 /directory/digest.py</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/calendarserver/changeset/2186">2186</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2008-03-04 13:45:09 -0800 (Tue, 04 Mar 2008)</dd>
</dl>

<h3>Log Message</h3>
<pre>Pull up r2177: Error cleaning digest credentials</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesreleaseCalendarServer12devtwistedcaldavdirectorydigestpy">CalendarServer/branches/release/CalendarServer-1.2-dev/twistedcaldav/directory/digest.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesreleaseCalendarServer12devtwistedcaldavdirectorydigestpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/release/CalendarServer-1.2-dev/twistedcaldav/directory/digest.py (2185 => 2186)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/release/CalendarServer-1.2-dev/twistedcaldav/directory/digest.py        2008-03-04 21:42:43 UTC (rev 2185)
+++ CalendarServer/branches/release/CalendarServer-1.2-dev/twistedcaldav/directory/digest.py        2008-03-04 21:45:09 UTC (rev 2186)
</span><span class="lines">@@ -90,6 +90,15 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         pass
</span><span class="cx"> 
</span><ins>+    def deleteMany(self, keys):
+        &quot;&quot;&quot;
+        Remove the records associated with the supplied keys.
+
+        @param key:        the key to remove.
+        @type key:         C{str}
+        &quot;&quot;&quot;
+        pass
+
</ins><span class="cx">     def keys(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Return all the keys currently available.
</span><span class="lines">@@ -98,6 +107,14 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         pass
</span><span class="cx">     
</span><ins>+    def items(self):
+        &quot;&quot;&quot;
+        Return all the key/value pairs currently available.
+        
+        @return:    a C{list} of C{tuple} for each key/value currently in the database.
+        &quot;&quot;&quot;
+        pass
+    
</ins><span class="cx"> class DigestCredentialsMap(object):
</span><span class="cx"> 
</span><span class="cx">     implements(IDigestCredentialsDatabase)
</span><span class="lines">@@ -133,12 +150,26 @@
</span><span class="cx">         if self.db.has_key(key):
</span><span class="cx">             del self.db[key]
</span><span class="cx"> 
</span><ins>+    def deleteMany(self, keys):
+        &quot;&quot;&quot;
+        See IDigestCredentialsDatabase.
+        &quot;&quot;&quot;
+        for key in keys:
+            if self.db.has_key(key):
+                del self.db[key]
+
</ins><span class="cx">     def keys(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         See IDigestCredentialsDatabase.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         return self.db.keys()
</span><span class="cx"> 
</span><ins>+    def items(self):
+        &quot;&quot;&quot;
+        See IDigestCredentialsDatabase.
+        &quot;&quot;&quot;
+        return self.db.items()
+
</ins><span class="cx"> class DigestCredentialsDB(AbstractSQLDatabase):
</span><span class="cx"> 
</span><span class="cx">     implements(IDigestCredentialsDatabase)
</span><span class="lines">@@ -162,7 +193,7 @@
</span><span class="cx"> 
</span><span class="cx">     def __init__(self, path):
</span><span class="cx">         db_path = os.path.join(path, DigestCredentialsDB.dbFilename)
</span><del>-        super(DigestCredentialsDB, self).__init__(db_path, autocommit=True)
</del><ins>+        super(DigestCredentialsDB, self).__init__(db_path, autocommit=False)
</ins><span class="cx">         self.exceptions = 0
</span><span class="cx">     
</span><span class="cx">     def has_key(self, key):
</span><span class="lines">@@ -192,8 +223,10 @@
</span><span class="cx">         try:
</span><span class="cx">             pvalue = pickle.dumps(value)
</span><span class="cx">             self._set_in_db(key, pvalue)
</span><ins>+            self._db_commit()
</ins><span class="cx">             self.exceptions = 0
</span><span class="cx">         except OperationalError, e:
</span><ins>+            self._db_rollback()
</ins><span class="cx">             self.exceptions += 1
</span><span class="cx">             if self.exceptions &gt;= self.exceptionLimit:
</span><span class="cx">                 self._db_close()
</span><span class="lines">@@ -227,14 +260,33 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         try:
</span><span class="cx">             self._delete_from_db(key)
</span><ins>+            self._db_commit()
</ins><span class="cx">             self.exceptions = 0
</span><span class="cx">         except OperationalError, e:
</span><ins>+            self._db_rollback()
</ins><span class="cx">             self.exceptions += 1
</span><span class="cx">             if self.exceptions &gt;= self.exceptionLimit:
</span><span class="cx">                 self._db_close()
</span><span class="cx">                 log.err(&quot;Reset digest credentials database connection: %s&quot; % (e,))
</span><span class="cx">             raise
</span><span class="cx"> 
</span><ins>+    def deleteMany(self, keys):
+        &quot;&quot;&quot;
+        See IDigestCredentialsDatabase.
+        &quot;&quot;&quot;
+        try:
+            for key in keys:
+                self._delete_from_db(key)
+            self._db_commit()
+            self.exceptions = 0
+        except OperationalError, e:
+            self._db_rollback()
+            self.exceptions += 1
+            if self.exceptions &gt;= self.exceptionLimit:
+                self._db_close()
+                log.err(&quot;Reset digest credentials database connection: %s&quot; % (e,))
+            raise
+
</ins><span class="cx">     def keys(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         See IDigestCredentialsDatabase.
</span><span class="lines">@@ -253,6 +305,24 @@
</span><span class="cx">                 log.err(&quot;Reset digest credentials database connection: %s&quot; % (e,))
</span><span class="cx">             raise
</span><span class="cx"> 
</span><ins>+    def items(self):
+        &quot;&quot;&quot;
+        See IDigestCredentialsDatabase.
+        &quot;&quot;&quot;
+        try:
+            result = []
+            for key in self._db_execute(&quot;select KEY, VALUE from DIGESTCREDENTIALS&quot;):
+                result.append((str(key[0]), pickle.loads(str(key[1])),))
+            
+            self.exceptions = 0
+            return result
+        except OperationalError, e:
+            self.exceptions += 1
+            if self.exceptions &gt;= self.exceptionLimit:
+                self._db_close()
+                log.err(&quot;Reset digest credentials database connection: %s&quot; % (e,))
+            raise
+
</ins><span class="cx">     def _set_in_db(self, key, value):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Insert the specified entry into the database, replacing any that might already exist.
</span><span class="lines">@@ -503,16 +573,16 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         This should be called at regular intervals to remove expired credentials from the cache.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        keys = self.db.keys()
</del><ins>+        items = self.db.items()
</ins><span class="cx">         oldest_allowed = time.time() - DigestCredentialFactory.CHALLENGE_LIFETIME_SECS
</span><del>-        for key in keys:
-            try:
-                value = self.db.get(key)
-                if value is not None:
-                    ignore_clientip, ignore_cnonce, db_timestamp = value
-                    if db_timestamp &lt;= oldest_allowed:
-                        self.invalidate(key)
-            except Exception, e:
-                # Clean-up errors can be logged but we should ignore them
-                log.err(&quot;Error cleaning digest credentials: %s&quot; % (e,))
-                pass
</del><ins>+        delete_keys = []
+        for key, value in items:
+            ignore_clientip, ignore_cnonce, db_timestamp = value
+            if db_timestamp &lt;= oldest_allowed:
+                delete_keys.append(key)
+
+        try:
+            self.db.deleteMany(delete_keys)
+        except Exception, e:
+            # Clean-up errors can be logged but we should ignore them
+            log.err(&quot;Failed to clean digest credentials: %s&quot; % (e,))
</ins></span></pre>
</div>
</div>

</body>
</html>