diff --git a/twisted/web/server.py b/twisted/web/server.py index a48f2ea..1a66d32 100644 --- a/twisted/web/server.py +++ b/twisted/web/server.py @@ -101,9 +101,12 @@ class Request(Copyable, http.Request, components.Componentized): @ivar defaultContentType: A C{bytes} giving the default I{Content-Type} value to send in responses if no other value is set. C{None} disables the default. + @ivar sessionCookieBaseName: A C{bytes} giving the base name when creating + new session cookies. """ defaultContentType = b"text/html" + sessionCookieBaseName = b'TWISTED_SESSION' site = None appRootURL = None @@ -384,7 +387,8 @@ class Request(Copyable, http.Request, components.Componentized): def getSession(self, sessionInterface = None): # Session management if not self.session: - cookiename = b"_".join([b'TWISTED_SESSION'] + self.sitepath) + cookiename = b"_".join( + [self.sessionCookieBaseName] + self.sitepath) sessionCookie = self.getCookie(cookiename) if sessionCookie: try: diff --git a/twisted/web/test/test_web.py b/twisted/web/test/test_web.py index 46b66f2..5ce9eeb 100644 --- a/twisted/web/test/test_web.py +++ b/twisted/web/test/test_web.py @@ -466,6 +466,22 @@ class RequestTests(unittest.TestCase): request.requestReceived(b'GET', b'/foo%2Fbar', b'HTTP/1.0') self.assertEqual(request.prePathURL(), b'http://example.com/foo%2Fbar') + def test_getSessionCustomCookieName(self): + """ + When creating new session cookies it will use + I{Request.sessionCookieBaseName} as the base name for the new cookie. + """ + baseName = b'CUSTON_NAME' + channel = DummyChannel() + request = server.Request(channel, 1) + request.sitepath = [] + request.site = channel.site + request.sessionCookieBaseName = baseName + + session = request.getSession() + self.addCleanup(lambda: session.expire()) + + self.assertTrue(request.cookies[0].startswith(baseName)) class GzipEncoderTests(unittest.TestCase): diff --git a/twisted/web/topfiles/6933.feature b/twisted/web/topfiles/6933.feature new file mode 100644 index 0000000..b097862 --- /dev/null +++ b/twisted/web/topfiles/6933.feature @@ -0,0 +1 @@ +twisted.web.server.Request allows specifying a custom name for session cookie. \ No newline at end of file