web/request.py
changeset 3653 ef71abb1e77b
parent 3293 69c0ba095536
child 3890 d7a270f50f54
equal deleted inserted replaced
3652:9fba30110377 3653:ef71abb1e77b
    10 import Cookie
    10 import Cookie
    11 import sha
    11 import sha
    12 import time
    12 import time
    13 import random
    13 import random
    14 import base64
    14 import base64
       
    15 from datetime import date
    15 from urlparse import urlsplit
    16 from urlparse import urlsplit
    16 from itertools import count
    17 from itertools import count
    17 
    18 
    18 from simplejson import dumps
    19 from simplejson import dumps
    19 
    20 
   440         try:
   441         try:
   441             return Cookie.SimpleCookie(self.get_header('Cookie'))
   442             return Cookie.SimpleCookie(self.get_header('Cookie'))
   442         except KeyError:
   443         except KeyError:
   443             return Cookie.SimpleCookie()
   444             return Cookie.SimpleCookie()
   444 
   445 
   445     def set_cookie(self, cookie, key, maxage=300):
   446     def set_cookie(self, cookie, key, maxage=300, expires=None):
   446         """set / update a cookie key
   447         """set / update a cookie key
   447 
   448 
   448         by default, cookie will be available for the next 5 minutes.
   449         by default, cookie will be available for the next 5 minutes.
   449         Give maxage = None to have a "session" cookie expiring when the
   450         Give maxage = None to have a "session" cookie expiring when the
   450         client close its browser
   451         client close its browser
   451         """
   452         """
   452         morsel = cookie[key]
   453         morsel = cookie[key]
   453         if maxage is not None:
   454         if maxage is not None:
   454             morsel['Max-Age'] = maxage
   455             morsel['Max-Age'] = maxage
       
   456         if expires:
       
   457             morsel['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S %z')
   455         # make sure cookie is set on the correct path
   458         # make sure cookie is set on the correct path
   456         morsel['path'] = self.base_url_path()
   459         morsel['path'] = self.base_url_path()
   457         self.add_header('Set-Cookie', morsel.OutputString())
   460         self.add_header('Set-Cookie', morsel.OutputString())
   458 
   461 
   459     def remove_cookie(self, cookie, key):
   462     def remove_cookie(self, cookie, key):
   460         """remove a cookie by expiring it"""
   463         """remove a cookie by expiring it"""
   461         morsel = cookie[key]
   464         self.set_cookie(cookie, key, maxage=0, expires=date(1970, 1, 1))
   462         morsel['Max-Age'] = 0
       
   463         # The only way to set up cookie age for IE is to use an old "expired"
       
   464         # syntax. IE doesn't support Max-Age there is no library support for
       
   465         # managing
       
   466         # ===> Do _NOT_ comment this line :
       
   467         morsel['expires'] = 'Thu, 01-Jan-1970 00:00:00 GMT'
       
   468         self.add_header('Set-Cookie', morsel.OutputString())
       
   469 
   465 
   470     def set_content_type(self, content_type, filename=None, encoding=None):
   466     def set_content_type(self, content_type, filename=None, encoding=None):
   471         """set output content type for this request. An optional filename
   467         """set output content type for this request. An optional filename
   472         may be given
   468         may be given
   473         """
   469         """