quixote session.py,1.57,1.58

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]>
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Update of /home/cvs/quixote
In directory hewson:/tmp/cvs-serv12856

Modified Files:
	session.py 
Log Message:
Defer the generation of session IDs until it is decided that the session
should be maintained.  Thanks to Jonathan Corbet for the patch.


Index: session.py
===================================================================
RCS file: /home/cvs/quixote/session.py,v
retrieving revision 1.57
retrieving revision 1.58
diff -u -d -r1.57 -r1.58
--- session.py	9 Oct 2002 18:38:57 -0000	1.57
+++ session.py	18 Oct 2002 21:16:23 -0000	1.58
@@ -182,6 +182,7 @@
         if not isinstance(session, self.session_class):
             raise TypeError("session not an instance of %r: %r"
                             % (self.session_class, session))
+        assert session.id is not None, "session ID not set"
         assert session_id == session.id, "session ID mismatch"
         self.sessions[session_id] = session
 
@@ -248,18 +249,19 @@
         else:
             return id
             
-    def _create_session (self, request):
+    def _make_session_id (self):
         # Generate a session ID, which is just the value of the session
         # cookie we are about to drop on the user.  (It's also the key
         # used with the session manager mapping interface.)
         id = None
         while id is None or self.has_session(id):
             id = "%016X" % randlong(8)  # 64-bit random number
+        return id
 
-        # Create a session object which will be looked up the next
-        # time this user comes back carrying the session cookie
-        # with the session ID just generated.
-        return self.new_session(request, id)
+    def _create_session (self, request):
+        # Create a new session object, with no ID for now - one will
+        # be assigned later if we save the session.
+        return self.new_session(request, None)
 
     def get_session (self, request):
         """get_session(request : HTTPRequest) -> Session
@@ -322,14 +324,15 @@
             # Session has no useful info -- forget it.  If it previously
             # had useful information and no longer does, we have to
             # explicitly forget it.
-            if self.has_session(session.id):
+            if session.id and self.has_session(session.id):
                 del self[session.id]
                 self.revoke_session_cookie(request)
             return
 
-        if not self.has_session(session.id):
+        if session.id is None:
             # This is the first time this session has had useful
             # info -- store it and set the session cookie.
+            session.id = self._make_session_id()
             self[session.id] = session
             self.set_session_cookie(request, session.id)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.