Author: justi9
Date: 2009-03-24 13:35:17 -0400 (Tue, 24 Mar 2009)
New Revision: 3206
Modified:
mgmt/trunk/wooly/python/wooly/__init__.py
mgmt/trunk/wooly/python/wooly/server.py
Log:
Only produce Set-Cookie headers for newly set cookies.
Replace the per-cookie Session.marshal_cookie with marshal_cookies,
which returns a list and filters down to newly set cookies only.
Add doc strings!
Modified: mgmt/trunk/wooly/python/wooly/__init__.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/__init__.py 2009-03-23 21:57:31 UTC (rev 3205)
+++ mgmt/trunk/wooly/python/wooly/__init__.py 2009-03-24 17:35:17 UTC (rev 3206)
@@ -472,7 +472,7 @@
self.origin = None
self.values_by_path = dict()
- self.cookies_by_name = dict() # name => (value, expires)
+ self.cookies_by_name = dict() # name => (newly set?, value, expires)
self.headers_by_name = dict()
if self.app.debug:
@@ -507,12 +507,12 @@
def get_cookie(self, name):
try:
- return self.cookies_by_name[name][0]
+ return self.cookies_by_name[name][1]
except KeyError:
pass
def set_cookie(self, name, value, expires=None):
- self.cookies_by_name[name] = (value, expires)
+ self.cookies_by_name[name] = (True, value, expires)
def expire_cookie(self, name):
self.set_cookie(name, "", datetime(*gmtime(1)[0:6]))
@@ -632,22 +632,43 @@
except ValueError:
pass
- def marshal_cookie(self, name):
- value, expires = self.cookies_by_name[name]
+ def marshal_cookies(self):
+ """
+ Produce a list of strings representing cookies newly set on
+ this session.
- crumbs = list()
- crumbs.append("%s=%s" % (name, value))
+ The strings are formatted to serve as HTTP Set-Cookie header
+ values.
+ """
- if expires:
- when = expires.strftime(self.http_date_gmt)
- crumbs.append("expires=%s" % when)
+ headers = list()
- return "; ".join(crumbs)
+ for name, record in self.cookies_by_name.iteritems():
+ new, value, expires = record
+ if new:
+ crumbs = list()
+ crumbs.append("%s=%s" % (name, value))
+
+ if expires:
+ when = expires.strftime(self.http_date_gmt)
+ crumbs.append("expires=%s" % when)
+
+ headers.append("; ".join(crumbs))
+
+ return headers
+
def unmarshal_cookies(self, string):
+ """
+ Parse and load cookies from string.
+
+ The string must be formatted as dictated by the HTTP Cookie
+ header.
+ """
+
for crumb in string.split(";"):
name, value = crumb.split("=", 1)
- self.cookies_by_name[name.strip()] = (value.strip(), None)
+ self.cookies_by_name[name.strip()] = (False, value.strip(), None)
def set_origin(self, origin):
self.origin = self.fix_origin(origin)
Modified: mgmt/trunk/wooly/python/wooly/server.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/server.py 2009-03-23 21:57:31 UTC (rev 3205)
+++ mgmt/trunk/wooly/python/wooly/server.py 2009-03-24 17:35:17 UTC (rev 3206)
@@ -142,8 +142,8 @@
return (message,)
def send_response(self, response, headers, session, message):
- for name in session.cookies_by_name:
- headers.append(("Set-Cookie", session.marshal_cookie(name)))
+ for header in session.marshal_cookies():
+ headers.append(("Set-Cookie", header))
response(message, headers)