1.1 --- a/WebStack/ModPython.py Sun Sep 05 17:43:56 2004 +0000
1.2 +++ b/WebStack/ModPython.py Sun Sep 05 17:44:19 2004 +0000
1.3 @@ -10,11 +10,14 @@
1.4 from mod_python.util import parse_qs, FieldStorage
1.5 from mod_python import apache
1.6
1.7 -# NOTE: Provide an alternative implementation for the cookie support.
1.8 -# NOTE: The alternative session support requires cookie support.
1.9 +# Provide alternative implementations.
1.10 +# The alternative session support requires cookie support of some kind.
1.11
1.12 -try: from mod_python import Cookie
1.13 -except ImportError: Cookie = None
1.14 +try:
1.15 + from mod_python import Cookie
1.16 +except ImportError:
1.17 + from Cookie import SimpleCookie
1.18 + Cookie = None
1.19 try:
1.20 from mod_python import Session
1.21 except ImportError:
1.22 @@ -37,6 +40,14 @@
1.23 self.user = None
1.24 self.content_type = None
1.25
1.26 + # Support non-framework cookies.
1.27 +
1.28 + if Cookie is None:
1.29 +
1.30 + # Define the incoming cookies.
1.31 +
1.32 + self.cookies_in = SimpleCookie(self.get_headers().get("cookie"))
1.33 +
1.34 # Cached information.
1.35
1.36 self.storage_body = None
1.37 @@ -232,7 +243,7 @@
1.38 if Cookie:
1.39 return Cookie.get_cookies(self.trans)
1.40 else:
1.41 - return None
1.42 + return self.cookies_in
1.43
1.44 def get_cookie(self, cookie_name):
1.45
1.46 @@ -309,8 +320,12 @@
1.47 if Cookie:
1.48 Cookie.add_cookie(self.trans, cookie)
1.49 else:
1.50 - # NOTE: Should raise an exception or provide an implementation.
1.51 - pass
1.52 + # NOTE: If multiple cookies of the same name could be specified, this
1.53 + # NOTE: could need changing.
1.54 +
1.55 + cookie_out = SimpleCookie()
1.56 + cookie_out[cookie.name] = cookie.value
1.57 + self._write_cookie(cookie_out)
1.58
1.59 def set_cookie_value(self, name, value, path=None, expires=None):
1.60
1.61 @@ -332,8 +347,13 @@
1.62 cookie.path = path
1.63 Cookie.add_cookie(self.trans, cookie)
1.64 else:
1.65 - # NOTE: Should raise an exception or provide an implementation.
1.66 - pass
1.67 + cookie_out = SimpleCookie()
1.68 + cookie_out[name] = value
1.69 + if path is not None:
1.70 + cookie_out[name]["path"] = path
1.71 + if expires is not None:
1.72 + cookie_out[name]["expires"] = expires
1.73 + self._write_cookie(cookie_out)
1.74
1.75 def delete_cookie(self, cookie_name):
1.76
1.77 @@ -353,8 +373,21 @@
1.78 cookie.max_age = 0
1.79 Cookie.add_cookie(self.trans, cookie)
1.80 else:
1.81 - # NOTE: Should raise an exception or provide an implementation.
1.82 - pass
1.83 + cookie_out = SimpleCookie()
1.84 + cookie_out[cookie_name] = ""
1.85 + cookie_out[cookie_name]["path"] = "/"
1.86 + cookie_out[cookie_name]["expires"] = 0
1.87 + cookie_out[cookie_name]["max-age"] = 0
1.88 + self._write_cookie(cookie_out)
1.89 +
1.90 + def _write_cookie(self, cookie):
1.91 +
1.92 + "An internal method adding the given 'cookie' to the headers."
1.93 +
1.94 + # NOTE: May not be using the appropriate method.
1.95 +
1.96 + for morsel in cookie.values():
1.97 + self.set_header_value("Set-Cookie", morsel.OutputString())
1.98
1.99 # Session-related methods.
1.100