Skip to content

Commit 02aebdb

Browse files
author
Alexander Smishlajev
committed
support for multiple cookie headers in single http response;
added utility method .add_cookie(name, value, [expire, [path]]); set 'roundup_charset' cookie only if '@charset' is in request parameters; request parameter '@charset=none' removes 'roundup_charset' cookie.
1 parent d475773 commit 02aebdb

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

roundup/cgi/client.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.206 2004-11-22 07:10:24 a1s Exp $
1+
# $Id: client.py,v 1.207 2004-11-22 09:12:02 a1s Exp $
22

33
"""WWW request handler (also used in the stand-alone server).
44
"""
@@ -125,6 +125,9 @@ def __init__(self, instance, request, env, form=None, translator=None):
125125
self.cookie_path = urlparse.urlparse(self.base)[2]
126126
self.cookie_name = 'roundup_session_' + re.sub('[^a-zA-Z]', '',
127127
self.instance.config.TRACKER_NAME)
128+
# cookies to set in http responce
129+
# {(path, name): (value, expire)}
130+
self.add_cookies = {}
128131

129132
# see if we need to re-parse the environment for the form (eg Zope)
130133
if form is None:
@@ -316,8 +319,12 @@ def determine_charset(self):
316319
self.charset = self.STORAGE_CHARSET
317320

318321
# look for client charset
322+
charset_parameter = 0
319323
if self.form.has_key('@charset'):
320324
charset = self.form['@charset'].value
325+
if charset.lower() == "none":
326+
charset = ""
327+
charset_parameter = 1
321328
elif self.cookie.has_key('roundup_charset'):
322329
charset = self.cookie['roundup_charset'].value
323330
else:
@@ -329,8 +336,15 @@ def determine_charset(self):
329336
except LookupError:
330337
self.error_message.append(self._('Unrecognized charset: %r')
331338
% charset)
339+
charset_parameter = 0
332340
else:
333341
self.charset = charset.lower()
342+
# If we've got a character set in request parameters,
343+
# set the browser cookie to keep the preference.
344+
# This is done after codecs.lookup to make sure
345+
# that we aren't keeping a wrong value.
346+
if charset_parameter:
347+
self.add_cookie('roundup_charset', charset)
334348

335349
# if client charset is different from the storage charset,
336350
# recode form fields
@@ -750,17 +764,6 @@ def write_html(self, content):
750764
# at this point, we are sure about Content-Type
751765
self.additional_headers['Content-Type'] = \
752766
'text/html; charset=%s' % self.charset
753-
# set the charset cookie
754-
# Note: we want to preserve the session cookie
755-
# set by LoginAction or ConfRegoAction.
756-
# i think that's ok: user does not perform
757-
# two actions (login and charset toggle) simultaneously.
758-
if not self.additional_headers.has_key('Set-Cookie'):
759-
# the charset is remembered for a year
760-
expire = Cookie._getdate(86400*365)
761-
self.additional_headers['Set-Cookie'] = \
762-
'roundup_charset=%s; expires=%s; Path=%s;' % (
763-
self.charset, expire, self.cookie_path)
764767
self.header()
765768

766769
if self.env['REQUEST_METHOD'] == 'HEAD':
@@ -796,11 +799,39 @@ def header(self, headers=None, response=None):
796799
self.request.send_response(response)
797800
for entry in headers.items():
798801
self.request.send_header(*entry)
802+
for ((path, name), (value, expire)) in self.add_cookies.items():
803+
self.request.send_header('Set-Cookie',
804+
"%s=%s; expires=%s; Path=%s;"
805+
% (name, value, Cookie._getdate(expire), path))
799806
self.request.end_headers()
800807
self.headers_done = 1
801808
if self.debug:
802809
self.headers_sent = headers
803810

811+
def add_cookie(self, name, value, expire=86400*365, path=None):
812+
"""Set a cookie value to be sent in HTTP headers
813+
814+
Parameters:
815+
name:
816+
cookie name
817+
value:
818+
cookie value
819+
expire:
820+
cookie expiration time (seconds).
821+
If value is empty (meaning "delete cookie"),
822+
expiration time is forced in the past
823+
and this argument is ignored.
824+
If omitted, the cookie will be kept for a year.
825+
path:
826+
cookie path (optional)
827+
828+
"""
829+
if path is None:
830+
path = self.cookie_path
831+
if not value:
832+
expire = -1
833+
self.add_cookies[(path, name)] = (value, expire)
834+
804835
def set_cookie(self, user):
805836
"""Set up a session cookie for the user.
806837
@@ -827,13 +858,8 @@ def set_cookie(self, user):
827858
sessions.set(self.session, user=user)
828859
self.db.commit()
829860

830-
# expire us in a long, long time
831-
expire = Cookie._getdate(86400*365)
832-
833-
# generate the cookie path - make sure it has a trailing '/'
834-
self.additional_headers['Set-Cookie'] = \
835-
'%s=%s; expires=%s; Path=%s;'%(self.cookie_name, self.session,
836-
expire, self.cookie_path)
861+
# add session cookie
862+
self.add_cookie(self.cookie_name, self.session)
837863

838864
def make_user_anonymous(self):
839865
''' Make us anonymous

0 commit comments

Comments
 (0)