r46924 - Merge http-session-id-type-8215: In twisted.web use bytes for session id.

adiroiban-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: adiroiban
Date: Sat Mar  5 07:43:01 2016
New Revision: 46924

Added:
   trunk/twisted/topfiles/8215.bugfix
Modified:
   trunk/twisted/web/server.py
   trunk/twisted/web/test/test_web.py

Log:
Merge http-session-id-type-8215: In twisted.web use bytes for session id.

Authors: SamyCookie, adiroiban
Reviewers: adiroiban, hawkowl
Fixes: #8215

Modified: trunk/twisted/web/server.py
==============================================================================
--- trunk/twisted/web/server.py	(original)
+++ trunk/twisted/web/server.py	Sat Mar  5 07:43:01 2016
@@ -544,7 +544,9 @@
     This utility class contains no functionality, but is used to
     represent a session.
 
-    @ivar uid: A unique identifier for the session, C{bytes}.
+    @ivar uid: A unique identifier for the session.
+    @type uid: L{bytes}
+
     @ivar _reactor: An object providing L{IReactorTime} to use for scheduling
         expiration.
     @ivar sessionTimeout: timeout of a session, in seconds.
@@ -664,12 +666,13 @@
         """
         (internal) Generate an opaque, unique ID for a user's session.
         """
+        from binascii import hexlify
         from hashlib import md5
         import random
         self.counter = self.counter + 1
-        return md5(networkString(
+        return hexlify(md5(networkString(
                 "%s_%s" % (str(random.random()), str(self.counter)))
-                   ).hexdigest()
+                   ).digest())
 
 
     def makeSession(self):
@@ -684,8 +687,12 @@
 
     def getSession(self, uid):
         """
-        Get a previously generated session, by its unique ID.
-        This raises a KeyError if the session is not found.
+        Get a previously generated session.
+
+        @param uid: Unique ID of the session.
+        @type uid: L{bytes}.
+
+        @raise: L{KeyError} if the session is not found.
         """
         return self.sessions[uid]
 

Modified: trunk/twisted/web/test/test_web.py
==============================================================================
--- trunk/twisted/web/test/test_web.py	(original)
+++ trunk/twisted/web/test/test_web.py	Sat Mar  5 07:43:01 2016
@@ -60,6 +60,22 @@
     Unit tests for L{server.Site}.
     """
 
+    def getAutoExpiringSession(self, site):
+        """
+        Create a new session which auto expires at cleanup.
+
+        @param site: The site on which the session is created.
+        @type site: L{server.Site}
+
+        @return: A newly created session.
+        @rtype: L{server.Session}
+        """
+        session = site.makeSession()
+        # Clean delayed calls from session expiration.
+        self.addCleanup(session.expire)
+        return session
+
+
     def test_simplestSite(self):
         """
         L{Site.getResourceFor} returns the C{b""} child of the root resource it
@@ -108,6 +124,39 @@
         self.assertIs(site.requestFactory, channel.requestFactory)
 
 
+    def test_makeSession(self):
+        """
+        L{site.getSession} generates a new C{Session} instance with an uid of
+        type L{bytes}.
+        """
+        site = server.Site(resource.Resource())
+        session = self.getAutoExpiringSession(site)
+
+        self.assertIsInstance(session, server.Session)
+        self.assertIsInstance(session.uid, bytes)
+
+
+    def test_getSessionExistent(self):
+        """
+        L{site.getSession} gets a previously generated session, by its unique
+        ID.
+        """
+        site = server.Site(resource.Resource())
+        createdSession = self.getAutoExpiringSession(site)
+
+        retrievedSession = site.getSession(createdSession.uid)
+
+        self.assertIs(createdSession, retrievedSession)
+
+
+    def test_getSessionNonExistent(self):
+        """
+        L{site.getSession} raises a L{KeyError} if the session is not found.
+        """
+        site = server.Site(resource.Resource())
+
+        self.assertRaises(KeyError, site.getSession, b'no-such-uid')
+
 
 class SessionTests(unittest.TestCase):
     """
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.