# HG changeset patch # User paulb # Date 1082331548 0 # Node ID 4fabce58a98110fd911a480716fe6b7fd1e5d76e # Parent 362004761af5435997fb797e7656f72f0e40e6f8 [project @ 2004-04-18 23:39:08 by paulb] Added cookie support and an implementation for mod_python. The API needs some more description, though. diff -r 362004761af5 -r 4fabce58a981 WebStack/Generic.py --- a/WebStack/Generic.py Fri Mar 26 18:59:55 2004 +0000 +++ b/WebStack/Generic.py Sun Apr 18 23:39:08 2004 +0000 @@ -246,6 +246,17 @@ raise NotImplementedError, "get_user" + def get_cookies(self): + + """ + A framework-specific method which obtains cookie information from the + request. + + Returns a dictionary mapping cookie names to cookie objects. + """ + + raise NotImplementedError, "get_cookies" + # Response-related methods. def get_response_stream(self): @@ -292,6 +303,40 @@ raise NotImplementedError, "set_content_type" + # Higher level response-related methods. + + def set_cookie(self, cookie): + + """ + A framework-specific method which stores the given 'cookie' object in + the response. + """ + + raise NotImplementedError, "set_cookie" + + def set_cookie_value(self, name, value, path=None, expires=None): + + """ + A framework-specific method which stores a cookie with the given 'name' + and 'value' in the response. + + The optional 'path' is a string which specifies the scope of the cookie, + and the optional 'expires' parameter is a value compatible with the + time.time function, and indicates the expiry date/time of the cookie. + """ + + raise NotImplementedError, "set_cookie_value" + + def delete_cookie(self, cookie_name): + + """ + A framework-specific method which adds to the response a request that + the cookie with the given 'cookie_name' be deleted/discarded by the + client. + """ + + raise NotImplementedError, "delete_cookie" + class Resource: "A generic resource interface." diff -r 362004761af5 -r 4fabce58a981 WebStack/ModPython.py --- a/WebStack/ModPython.py Fri Mar 26 18:59:55 2004 +0000 +++ b/WebStack/ModPython.py Sun Apr 18 23:39:08 2004 +0000 @@ -7,6 +7,11 @@ import Generic from mod_python.util import parse_qs, FieldStorage from mod_python import apache +try: + from mod_python import Cookie +except ImportError: + # NOTE: Should provide an alternative implementation. + Cookie = None class Transaction(Generic.Transaction): @@ -161,6 +166,20 @@ return self.trans.user + def get_cookies(self): + + """ + A framework-specific method which obtains cookie information from the + request. + + Returns a dictionary mapping cookie names to cookie objects. + + NOTE: No additional information is passed to the underlying API despite + NOTE: support for enhanced cookies in mod_python. + """ + + return Cookie.get_cookies(self.trans) + # Response-related methods. def get_response_stream(self): @@ -207,4 +226,61 @@ self.trans.content_type = self.format_content_type(content_type) + def set_cookie(self, cookie): + + """ + A framework-specific method which stores the given 'cookie' object in + the response. + """ + + if Cookie: + Cookie.add_cookie(self.trans, cookie) + else: + # NOTE: Should raise an exception or provide an implementation. + pass + + def set_cookie_value(self, name, value, path=None, expires=None): + + """ + A framework-specific method which stores a cookie with the given 'name' + and 'value' in the response. + + The optional 'path' is a string which specifies the scope of the cookie, + and the optional 'expires' parameter is a value compatible with the + time.time function, and indicates the expiry date/time of the cookie. + """ + + if Cookie: + cookie = Cookie.Cookie(name, value) + if expires is not None: + cookie.expires = expires + if path is not None: + cookie.path = path + Cookie.add_cookie(self.trans, cookie) + else: + # NOTE: Should raise an exception or provide an implementation. + pass + + def delete_cookie(self, cookie_name): + + """ + A framework-specific method which adds to the response a request that + the cookie with the given 'cookie_name' be deleted/discarded by the + client. + """ + + # Create a special cookie, given that we do not know whether the browser + # has been sent the cookie or not. + # NOTE: Magic discovered in Webware. + + if Cookie: + cookie = Cookie.Cookie(cookie_name, "") + cookie.path = "/" + cookie.expires = 0 + cookie.max_age = 0 + Cookie.add_cookie(self.trans, cookie) + else: + # NOTE: Should raise an exception or provide an implementation. + pass + # vim: tabstop=4 expandtab shiftwidth=4