[Twisted-Python] Basic cookie support

Itamar Shtull-Trauring twisted at itamarst.org
Mon Dec 17 12:14:56 EST 2001


Unless someone has a problem with it, this goes in tommorow. I'm willing to 
add a comment "don't use cookies, use sessions instead" ;)


RCS file: /cvs/Twisted/twisted/web/server.py,v
retrieving revision 1.31
diff -c -r1.31 server.py
*** twisted/web/server.py	15 Dec 2001 03:24:10 -0000	1.31
--- twisted/web/server.py	17 Dec 2001 17:13:54 -0000
***************
*** 170,176 ****
           self.args = {}
           self.stack = []
           self.headers = {}
!
           self.method, self.uri = command, path
           self.clientproto = version
           self.content = content
--- 170,177 ----
           self.args = {}
           self.stack = []
           self.headers = {}
!         self.cookies = []
!
           self.method, self.uri = command, path
           self.clientproto = version
           self.content = content
***************
*** 337,342 ****
--- 338,345 ----
                   self.sendStatus(self.code, message)
                   for name, value in self.headers.items():
                       self.sendHeader(name, value)
+                 for cookie in self.cookies:
+                     self.sendHeader("Set-Cookie", cookie)
                   self.endHeaders()

               # if this is a "HEAD" request, we shouldn't return any data
***************
*** 360,365 ****
--- 363,391 ----
           """
           self.finish()

+     def addCookie(self, k, v, expires=None, domain=None, path=None, 
max_age=None, comment=None, secure=None):
+         """Set an outgoing HTTP cookie.
+         """
+         cookie = '%s="%s"' % (k, v)
+         if not (expires is None):
+             cookie = cookie +"; Expires=%s" % expires
+         if not (domain is None):
+             cookie = cookie +"; Domain=%s" % domain
+         if not (path is None):
+             cookie = cookie +"; Path=%s" % path
+         if not (max_age is None):
+             cookie = cookie +"; Max-Age=%s" % max_age
+         if not (comment is None):
+             cookie = cookie +"; Comment=%s" % comment
+         if not (secure is None) and secure:
+             cookie = cookie +"; Secure"
+         self.cookies.append(cookie)
+
+     def view_addCookie(self, k, v, **kwargs):
+         """Remote version of addCookie; same interface.
+         """
+         apply(self.addCookie, (k, v), kwargs)
+
       def setHeader(self, k, v):
           """Set an outgoing HTTP header.
           """
***************
*** 418,425 ****
               # if it still hasn't been set, fix it up.
               if not self.session:
                   self.session = self.site.makeSession()
!                 self.setHeader('Set-Cookie',
!                                '%s=%s' % (cookiename, self.session.uid))
           self.session.touch()
           return self.session

--- 444,450 ----
               # if it still hasn't been set, fix it up.
               if not self.session:
                   self.session = self.site.makeSession()
!                 self.addCookie(cookiename, self.session.uid)
           self.session.touch()
           return self.session





More information about the Twisted-Python mailing list