Commits

bbangert  committed c0e3cbe

[svn] * Changed session to only send the cookie header if its a new session and
save() was called. Also only creates the session file under these
conditions.

  • Participants
  • Parent commits 38748af

Comments (0)

Files changed (4)

 Beaker Changelog
 =================
 
+0.7.3 (**svn**)
+* Changed session to only send the cookie header if its a new session and
+  save() was called. Also only creates the session file under these 
+  conditions.
+
 0.7.2 (05/19/07)
 * Added deprecation warning for middleware move, relocated middleware to cache
   and session modules for backwards compatibility.

File beaker/cache.py

         
         self.app = app
         config = config or {}
-
+        
         # Load up the default params
         self.options= dict(type='memory', data_dir=None, timeout=None, 
                            log_file=None)

File beaker/session.py

             self._create_id()
         else:
             self.is_new = False
-
-        try:
-            self.load()
-        except:
-            if invalidate_corrupt:
-                self.invalidate()
-            else:
-                raise
+        
+        if not self.is_new:
+            try:
+                self.load()
+            except:
+                if invalidate_corrupt:
+                    self.invalidate()
+                else:
+                    raise
+        else:
+            self.dict = {}
         
     def _create_id(self):
         self.id = md5.new(
                 self.cookie[self.key]['expires'] = \
                     expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT" )
             self.request['cookie_out'] = self.cookie[self.key].output(header='')
-		
-
+            self.request['set_cookie'] = False
+    
     created = property(lambda self: self.dict['_creation_time'])
 
     def delete(self):
                                               digest_filenames=False, **self.kwargs)
         
         namespace = self.namespace
+        self.request['set_cookie'] = True
         
         namespace.acquire_write_lock()
         try:
     
     def save(self):
         "saves the data for this session to persistent storage"
+        if not hasattr(self, 'namespace'):
+            curdict = self.dict
+            self.load()
+            self.dict = curdict
         
         self.namespace.acquire_write_lock()
         try:
             self.namespace['_accessed_time'] = time.time()
         finally:
             self.namespace.release_write_lock()
+        if self.is_new:
+            self.request['set_cookie'] = True
     
-
     def lock(self):
         """locks this session against other processes/threads.  this is 
         automatic when load/save is called.
         
         def session_start_response(status, headers, exc_info = None):
             if session.__dict__['_sess'] is not None:
-                cookie = session.__dict__['_headers']['cookie_out']
-                if cookie:
-                    headers.append(('Set-cookie', cookie))
+                if session.__dict__['_headers']['set_cookie']:
+                    cookie = session.__dict__['_headers']['cookie_out']
+                    if cookie:
+                        headers.append(('Set-cookie', cookie))
             return start_response(status, headers, exc_info)
         try:
             response = self.wrap_app(environ, session_start_response)

File tests/test_increment.py

     if not session.has_key('value'):
         session['value'] = 0
     session['value'] += 1
-    session.save()
+    if not environ['PATH_INFO'].startswith('/nosave'):
+        session.save()
     start_response('200 OK', [('Content-type', 'text/plain')])
     return ['The current value is: %d' % session['value']]
 
     assert 'current value is: 2' in res2
     assert 'current value is: 4' in res
 
+def test_nosave():
+    app = TestApp(SessionMiddleware(simple_app))
+    res = app.get('/nosave')
+    assert 'current value is: 1' in res
+    assert [] == res.all_headers('Set-Cookie')
+    res = app.get('/nosave')
+    assert 'current value is: 1' in res
+    
+    res = app.get('/')
+    assert 'current value is: 1' in res
+    assert len(res.all_headers('Set-Cookie')) > 0
+    res = app.get('/')
+    assert [] == res.all_headers('Set-Cookie')
+    assert 'current value is: 2' in res
+
 
 if __name__ == '__main__':
     from paste import httpserver