Commits

Sylvain Hellegouarch committed 569e842

Backport to 2.2 of #525

Comments (0)

Files changed (2)

cherrypy/filters/sessionfilter.py

         
         clean_up_delay = conf('session_filter.clean_up_delay', 5)
         clean_up_delay = datetime.timedelta(seconds = clean_up_delay * 60)
-        
+
         cookie_name = conf('session_filter.cookie_name', 'session_id')
         cookie_domain = conf('session_filter.cookie_domain', None)
         cookie_secure = conf('session_filter.cookie_secure', False)
     
     def save(self, id, data, expiration_time):
         cherrypy._session_data_holder[id] = (data, expiration_time)
-    
+
+    def delete(self, id=None):
+        if id is None:
+            id = cherrypy.session.id
+        del cherrypy._session_data_holder[id]
+
     def acquire_lock(self):
         sess = cherrypy.request._session
         id = cherrypy.session.id
         pickle.dump((data, expiration_time), f)
         f.close()
     
+    def delete(self, id=None):
+        if id is None:
+            id = cherrypy.session.id
+        file_path = self._get_file_path(id)
+        try:
+            os.unlink(file_path)
+        except:
+            pass
+        
     def acquire_lock(self):
         sess = cherrypy.request._session
         if not sess.locked:
                     if expiration_time < now:
                         # Session expired: deleting it
                         id = fname[len(self.SESSION_PREFIX):]
+                        print file_path
                         sess.on_delete_session(data)
                         os.unlink(file_path)
                 except:
         # Unpickle data
         data = pickle.loads(pickled_data)
         return (data, expiration_time)
+
+    def delete(self, id=None):
+        if id is None:
+            id = cherrypy.session.id
+        self.cursor.execute('delete from session where id=%s', (id,)) 
     
     def save(self, id, data, expiration_time):
         # Try to delete session if it was already there
             return sess.session_storage.release_lock
         elif name == 'id':
             return sess.session_id
+        elif name == 'delete':
+            return sess.session_storage.delete
 
         if sess.to_be_loaded:
             data = sess.session_storage.load(sess.session_id)
 
         return getattr(sess.session_data, name)
 
+def expire():
+    """Expire the current session cookie."""
+    name = cherrypy.config.get('session_filter.cookie_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

cherrypy/test/test_session_filter.py

 import test
 test.prefer_parent_path()
 
-import cherrypy, os
+import cherrypy, os, time
+from cherrypy.filters import sessionfilter
 
+localDir = os.path.dirname(__file__)
 
 def setup_server():
     class Root:
         def setsessiontype(self, newtype):
             cherrypy.config.update({'session_filter.storage_type': newtype})
         setsessiontype.exposed = True
+
+        def delete(self):
+            cherrypy.session.delete() 
+            sessionfilter.expire() 
+            return "done" 
+ 	delete.exposed = True
         
     cherrypy.root = Root()
     cherrypy.config.update({
             'server.environment': 'production',
             'session_filter.on': True,
             'session_filter.storage_type' : 'file',
-            'session_filter.storage_path' : '.',
+            'session_filter.storage_path' : localDir,
+            'session_filter.timeout': 0.017,
+            'session_filter.clean_up_delay': 0.017,
     })
 
 import helper
         self.assertBody('2')
         self.getPage('/testStr', self.cookies)
         self.assertBody('3')
+
+        self.getPage('/delete', self.cookies)
+        self.assertBody("done")
+
+        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')] 
+        self.assertEqual(f(), [])
+        
         self.getPage('/setsessiontype/file')
         self.getPage('/testStr')
         self.assertBody('1')
         self.getPage('/testStr', self.cookies)
         self.assertBody('3')
 
+        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
+        self.assertNotEqual(f(), [])
+        
+        self.getPage('/delete', self.cookies)
+        self.assertBody("done")
+
+        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')] 
+        self.assertEqual(f(), [])
+
+        self.getPage('/testStr')
+        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
+        self.assertNotEqual(f(), [])
+
         # Clean up session files
-        for fname in os.listdir('.'):
+        for fname in os.listdir(localDir):
             if fname.startswith('session-'):
-                os.unlink(fname)
+                os.unlink(os.path.join(localDir, fname))
 
 if __name__ == "__main__":
     setup_server()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.