# HG changeset patch # User paulb # Date 1082826032 0 # Node ID 6d07b9f97c0c2d3ffd3aab89cab018c05ca0293d # Parent 0915c78d37950a769e10ddbf7093c6b6bb5503ea [project @ 2004-04-24 17:00:32 by paulb] Added a get_cookie method to existing implementations. Added cookie support for Twisted. diff -r 0915c78d3795 -r 6d07b9f97c0c WebStack/Generic.py --- a/WebStack/Generic.py Sat Apr 24 16:59:49 2004 +0000 +++ b/WebStack/Generic.py Sat Apr 24 17:00:32 2004 +0000 @@ -257,6 +257,18 @@ raise NotImplementedError, "get_cookies" + def get_cookie(self, cookie_name): + + """ + A framework-specific method which obtains cookie information from the + request. + + Returns a cookie object for the given 'cookie_name' or None if no such + cookie exists. + """ + + raise NotImplementedError, "get_cookie" + # Response-related methods. def get_response_stream(self): diff -r 0915c78d3795 -r 6d07b9f97c0c WebStack/ModPython.py --- a/WebStack/ModPython.py Sat Apr 24 16:59:49 2004 +0000 +++ b/WebStack/ModPython.py Sat Apr 24 17:00:32 2004 +0000 @@ -180,6 +180,18 @@ return Cookie.get_cookies(self.trans) + def get_cookie(self, cookie_name): + + """ + A framework-specific method which obtains cookie information from the + request. + + Returns a cookie object for the given 'cookie_name' or None if no such + cookie exists. + """ + + return self.get_cookies().get(cookie_name) + # Response-related methods. def get_response_stream(self): @@ -250,6 +262,8 @@ time.time function, and indicates the expiry date/time of the cookie. """ + # NOTE: We just hope that Cookie converts Unicode arguments to US-ASCII. + if Cookie: cookie = Cookie.Cookie(name, value) if expires is not None: diff -r 0915c78d3795 -r 6d07b9f97c0c WebStack/Twisted.py --- a/WebStack/Twisted.py Sat Apr 24 16:59:49 2004 +0000 +++ b/WebStack/Twisted.py Sat Apr 24 17:00:32 2004 +0000 @@ -6,6 +6,7 @@ import Generic from Helpers.Auth import UserInfo +from Helpers.Request import Cookie from cgi import parse_qs class Transaction(Generic.Transaction): @@ -164,6 +165,38 @@ else: return None + 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: Twisted does not seem to support this operation via methods. Thus, + NOTE: direct access has been employed to get the dictionary. + NOTE: Twisted also returns a plain string - a Cookie object is therefore + NOTE: introduced. + """ + + cookies = {} + for name, value in self.trans.received_cookies.items(): + cookies[name] = Cookie(name, value) + return cookies + + def get_cookie(self, cookie_name): + + """ + A framework-specific method which obtains cookie information from the + request. + + Returns a cookie object for the given 'cookie_name' or None if no such + cookie exists. + NOTE: Twisted also returns a plain string - a Cookie object is therefore + NOTE: introduced. + """ + + return Cookie(cookie_name, self.trans.getCookie(cookie_name)) + # Response-related methods. def get_response_stream(self): @@ -212,4 +245,41 @@ self.trans.setHeader("Content-Type", self.format_content_type(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. + """ + + self.trans.addCookie(cookie.name, cookie.value, expires=cookie.expires, path=cookie.path) + + 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. + """ + + self.trans.addCookie(self.format_header_value(name), + self.format_header_value(value), expires=expires, path=path) + + 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. + + NOTE: Not supported yet. + """ + + pass + # vim: tabstop=4 expandtab shiftwidth=4