Changeset 940
- Timestamp:
- 01/26/06 05:59:08
- Files:
-
- trunk/cherrypy/filters/sessionfilter.py (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/filters/sessionfilter.py
r938 r940 6 6 7 7 Variables used to store config options: 8 - sess.session Timeout: timeout delay for the session9 - sess.session Locking: mechanism used to lock the session ('implicit' or 'explicit')8 - sess.session_timeout: timeout delay for the session 9 - sess.session_locking: mechanism used to lock the session ('implicit' or 'explicit') 10 10 11 11 Variables used to store temporary variables: … … 15 15 Variables used to store the session for the current request: 16 16 - sess.session_data: dictionary containing the actual session data 17 - sess.session ID: current session ID17 - sess.session_id: current session ID 18 18 - sess.expiration_time: date/time when the current session will expire 19 19 20 20 Global variables (RAM backend only): 21 - cherrypy._session_lock_dict: dictionary containing the locks for all session IDs22 - cherrypy._session Holder: dictionary containing the data for all sessions21 - cherrypy._session_lock_dict: dictionary containing the locks for all session_id 22 - cherrypy._session_data_holder: dictionary containing the data for all sessions 23 23 24 24 """ … … 64 64 65 65 sess = cherrypy.request._session 66 now = datetime.datetime.now()67 66 # Dont enable session if session_filter is off or if this is a 68 67 # request for static data … … 71 70 sess.session_storage = None 72 71 return 73 72 74 73 sess.locked = False # Not locked by default 74 sess.to_be_loaded = True 75 75 76 76 # Read config options 77 sess.session Timeout = conf('session_filter.timeout', 60)78 sess.session Locking = conf('session_filter.locking', 'explicit')79 sess.on CreateSession = conf('session_filter.on_create_session',77 sess.session_timeout = conf('session_filter.timeout', 60) 78 sess.session_locking = conf('session_filter.locking', 'explicit') 79 sess.on_create_session = conf('session_filter.on_create_session', 80 80 lambda data: None) 81 sess.on DeleteSession = conf('session_filter.on_delete_session',81 sess.on_delete_session = conf('session_filter.on_delete_session', 82 82 lambda data: None) 83 83 sess.generate_session_id = conf('session_filter.on_delete_session', 84 84 generate_session_id) 85 85 86 clean UpDelay = conf('session_filter.clean_up_delay', 5)87 clean UpDelay = datetime.timedelta(seconds = cleanUpDelay * 60)88 89 cookie Name = conf('session_filter.cookie_name', 'sessionID')90 cookie Domain = conf('session_filter.cookie_domain', None)91 cookie Secure = conf('session_filter.cookie_secure', False)92 cookie Path = conf('session_filter.cookie_path', None)93 94 if cookie Path is None:95 cookie PathHeader = conf('session_filter.cookie_path_from_header', None)96 if cookie PathHeader is not None:97 cookie Path = cherrypy.request.headerMap.get(cookiePathHeader, None)98 if cookie Path is None:99 cookie Path = '/'100 101 sess.deadlock Timeout = conf('session_filter.deadlock_timeout', 30)86 clean_up_delay = conf('session_filter.clean_up_delay', 5) 87 clean_up_delay = datetime.timedelta(seconds = clean_up_delay * 60) 88 89 cookie_name = conf('session_filter.cookie_name', 'session_id') 90 cookie_domain = conf('session_filter.cookie_domain', None) 91 cookie_secure = conf('session_filter.cookie_secure', False) 92 cookie_path = conf('session_filter.cookie_path', None) 93 94 if cookie_path is None: 95 cookie_path_header = conf('session_filter.cookie_path_from_header', None) 96 if cookie_path_header is not None: 97 cookie_path = cherrypy.request.headerMap.get(cookie_path_header, None) 98 if cookie_path is None: 99 cookie_path = '/' 100 101 sess.deadlock_timeout = conf('session_filter.deadlock_timeout', 30) 102 102 103 103 storage = conf('session_filter.storage_type', 'Ram') … … 113 113 114 114 # Check if we need to clean up old sessions 115 if cherrypy._session_last_clean_up_time + cleanUpDelay < now: 115 if cherrypy._session_last_clean_up_time + clean_up_delay < \ 116 datetime.datetime.now(): 116 117 sess.session_storage.clean_up() 117 118 118 119 # Check if request came with a session ID 119 if cookie Name in cherrypy.request.simpleCookie:120 # It did: we try to load the session data121 sess.session ID = cherrypy.request.simpleCookie[cookieName].value120 if cookie_name in cherrypy.request.simpleCookie: 121 # It did: we mark the data as needing to be loaded 122 sess.session_id = cherrypy.request.simpleCookie[cookie_name].value 122 123 123 124 # If using implicit locking, acquire lock 124 if sess.session Locking == 'implicit':125 sess.session_data = {'_id': sess.session ID}125 if sess.session_locking == 'implicit': 126 sess.session_data = {'_id': sess.session_id} 126 127 sess.session_storage.acquire_lock() 127 128 128 data = sess.session_storage.load(sess.sessionID) 129 # data is either None or a tuple (session_data, expiration_time) 130 if data is None or data[1] < now: 131 # Expired session: 132 # flush session data (but keep the same sessionID) 133 sess.session_data = {'_id': sess.sessionID} 134 else: 135 sess.session_data = data[0] 129 sess.to_be_loaded = True 130 136 131 else: 137 # No session IDyet138 sess.session ID= sess.generate_session_id()139 sess.session_data = {'_id': sess.session ID}140 sess.on CreateSession(sess.session_data)132 # No session_id yet 133 sess.session_id = sess.generate_session_id() 134 sess.session_data = {'_id': sess.session_id} 135 sess.on_create_session(sess.session_data) 141 136 # Set response cookie 142 137 cookie = cherrypy.response.simpleCookie 143 cookie[cookie Name] = sess.sessionID144 cookie[cookie Name]['path'] = cookiePath145 cookie[cookie Name]['max-age'] = sess.sessionTimeout * 60146 cookie[cookie Name]['version'] = 1147 if cookie Domain is not None:148 cookie[cookie Name]['domain'] = cookieDomain149 if cookie Secure is True:150 cookie[cookie Name]['secure'] = 1138 cookie[cookie_name] = sess.session_id 139 cookie[cookie_name]['path'] = cookie_path 140 cookie[cookie_name]['max-age'] = sess.session_timeout * 60 141 cookie[cookie_name]['version'] = 1 142 if cookie_domain is not None: 143 cookie[cookie_name]['domain'] = cookie_domain 144 if cookie_secure is True: 145 cookie[cookie_name]['secure'] = 1 151 146 152 147 def before_finalize(self): … … 159 154 160 155 # Save session data 161 t = datetime.timedelta(seconds = sess.sessionTimeout * 60) 162 expiration_time = datetime.datetime.now() + t 163 sess.session_storage.save(sess.sessionID, sess.session_data, 164 expiration_time) 156 if sess.to_be_loaded is False: 157 t = datetime.timedelta(seconds = sess.session_timeout * 60) 158 expiration_time = datetime.datetime.now() + t 159 sess.session_storage.save(sess.session_id, 160 sess.session_data, expiration_time) 161 else: 162 # If session data has never been loaded then it's never been 163 # accesses: not need to delete it 164 pass 165 165 if sess.locked: 166 166 # Always release the lock if the user didn't release it … … 205 205 def acquire_lock(self): 206 206 sess = cherrypy.request._session 207 id = cherrypy.session ['_id']207 id = cherrypy.session.id 208 208 lock = cherrypy._session_lock_dict.get(id) 209 209 if lock is None: … … 214 214 if lock.acquire(False): 215 215 break 216 if time.time() - startTime > sess.deadlock Timeout:216 if time.time() - startTime > sess.deadlock_timeout: 217 217 raise SessionDeadlockError() 218 218 time.sleep(0.5) … … 236 236 deleted_session = cherrypy._session_data_holder[id] 237 237 del cherrypy._session_data_holder[id] 238 sess.on DeleteSession(deleted_session)238 sess.on_delete_session(deleted_session) 239 239 except KeyError: 240 240 # The session probably got deleted by a concurrent thread … … 267 267 def acquire_lock(self): 268 268 sess = cherrypy.request._session 269 filePath = self._getFilePath(cherrypy.session ['_id'])269 filePath = self._getFilePath(cherrypy.session.id) 270 270 lockFilePath = filePath + self.LOCK_SUFFIX 271 271 self._lockFile(lockFilePath) … … 274 274 def release_lock(self): 275 275 sess = cherrypy.request._session 276 filePath = self._getFilePath(cherrypy.session ['_id'])276 filePath = self._getFilePath(cherrypy.session.id) 277 277 lockFilePath = filePath + self.LOCK_SUFFIX 278 278 self._unlockFile(lockFilePath) … … 298 298 # Session expired: deleting it 299 299 id = fname[len(self.SESSION_PREFIX):] 300 sess.on DeleteSession(data)300 sess.on_delete_session(data) 301 301 os.unlink(filePath) 302 302 except: … … 317 317 lockfd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL) 318 318 except OSError: 319 if time.time() - startTime > sess.deadlock Timeout:319 if time.time() - startTime > sess.deadlock_timeout: 320 320 raise SessionDeadlockError() 321 321 time.sleep(0.5) … … 377 377 self.cursor.execute( 378 378 'select id from session where id=%s for update', 379 (cherrypy.session ['_id'],))379 (cherrypy.session.id,)) 380 380 381 381 def release_lock(self): … … 393 393 rows = self.cursor.fetchall() 394 394 for row in rows: 395 sess.on DeleteSession(row[0])395 sess.on_delete_session(row[0]) 396 396 self.cursor.execute( 397 397 'delete from session where expiration_time < %s', … … 400 400 401 401 def generate_session_id(): 402 """ Return a new session ID"""402 """ Return a new session_id """ 403 403 return sha.new('%s' % random.random()).hexdigest() 404 404 … … 415 415 raise SessionNotEnabledError() 416 416 # Create thread-specific dictionary if needed 417 sess.session_data = getattr(sess, 'session_data', {}) 417 session_data = getattr(sess, 'session_data', None) 418 if session_data is None: 419 sess.session_data = {} 418 420 if name == 'acquire_lock': 419 421 return sess.session_storage.acquire_lock 420 422 elif name == 'release_lock': 421 423 return sess.session_storage.release_lock 424 elif name == 'id': 425 return sess.session_id 426 427 if sess.to_be_loaded: 428 data = sess.session_storage.load(sess.session_id) 429 # data is either None or a tuple (session_data, expiration_time) 430 if data is None or data[1] < datetime.datetime.now(): 431 # Expired session: 432 # flush session data (but keep the same session_id) 433 sess.session_data = {'_id': sess.session_id} 434 else: 435 sess.session_data = data[0] 436 sess.to_be_loaded = False 437 422 438 return getattr(sess.session_data, name) 423 439

