Commits

Robert Brewer  committed 17a1553

Fix for #525 (Support Deleting Sessions).

  • Participants
  • Parent commits b20f4ec

Comments (0)

Files changed (2)

File cherrypy/lib/sessions.py

                                                    self.clean_cycle)
             t.start()
     
+    def delete(self):
+        """Delete stored session data."""
+        self._delete()
+    
     def __getitem__(self, key):
         if not self.loaded: self.load()
         return self._data[key]
     def _save(self, expiration_time):
         self.cache[self.id] = (self._data, expiration_time)
     
+    def _delete(self):
+        del self.cache[self.id]
+    
     def acquire_lock(self):
         self.locked = True
         self.locks.setdefault(self.id, threading.Semaphore()).acquire()
         finally:
             f.close()
     
+    def _delete(self):
+        os.unlink(self._get_file_path())
+    
     def acquire_lock(self, path=None):
         if path is None:
             path = self._get_file_path()
         return data, expiration_time
     
     def _save(self, expiration_time):
+        pickled_data = pickle.dumps(self._data)
+        self.cursor.execute('update session set data = %s, '
+                            'expiration_time = %s where id = %s',
+                            (pickled_data, expiration_time, self.id))
+    
+    def _delete(self):
         self.cursor.execute('delete from session where id=%s', (self.id,))
-        pickled_data = pickle.dumps(self._data)
-        self.cursor.execute(
-            'insert into session (id, data, expiration_time) values (%s, %s, %s)',
-            (self.id, pickled_data, expiration_time))
-    
+   
     def acquire_lock(self):
         # We use the "for update" clause to lock the row
+        self.locked = True
         self.cursor.execute('select id from session where id=%s for update',
                             (self.id,))
     
         # We just close the cursor and that will remove the lock
         #   introduced by the "for update" clause
         self.cursor.close()
+        self.locked = False
     
     def clean_up(self):
         """Clean up expired sessions."""
     if secure:
         cookie[name]['secure'] = 1
 
+def expire():
+    """Expire the current session cookie."""
+    name = cherrypy.request.config.get('tools.sessions.name', 'session_id')
+    one_year = 60 * 60 * 24 * 365
+    exp = time.gmtime(time.time() - one_year)
+    t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", exp)
+    cherrypy.response.simple_cookie[name]['expires'] = t
+

File cherrypy/test/test_session.py

 import time
 
 import cherrypy
+from cherrypy.lib import sessions
 
 
 def setup_server():
             sess['counter'] = c
             return str(c)
         index.exposed = True
+        
+        def delete(self):
+            cherrypy.session.delete()
+            sessions.expire()
+            return "done"
+        delete.exposed = True
     
     cherrypy.tree.mount(Root())
     cherrypy.config.update({'environment': 'test_suite'})
         self.assertBody('2')
         self.getPage('/testStr', self.cookies)
         self.assertBody('3')
+        
         self.getPage('/setsessiontype/file')
         self.getPage('/testStr')
         self.assertBody('1')
         self.getPage('/')
         self.assertBody('1')
         
-        # Wait for the cleanup thread to delete session files
+        # Test session delete
+        self.getPage('/delete', self.cookies)
+        self.assertBody("done")
+        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
+        self.assertEqual(f(), [])
+        
+        # Wait for the cleanup thread to delete remaining session files
+        self.getPage('/')
         f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
         self.assertNotEqual(f(), [])
         time.sleep(2)