1.1 --- a/WebStack/BaseHTTPRequestHandler.py Thu Sep 16 22:18:33 2004 +0000
1.2 +++ b/WebStack/BaseHTTPRequestHandler.py Thu Sep 16 22:19:45 2004 +0000
1.3 @@ -5,12 +5,12 @@
1.4 """
1.5
1.6 import Generic
1.7 -from Helpers.Request import MessageBodyStream, get_body_fields, get_storage_items
1.8 +from Helpers.Request import MessageBodyStream, get_body_fields, get_storage_items, Cookie
1.9 from Helpers.Response import ConvertingStream
1.10 from Helpers.Auth import UserInfo
1.11 from Helpers.Session import SessionStore
1.12 from cgi import parse_qs, FieldStorage
1.13 -import Cookie
1.14 +from Cookie import SimpleCookie
1.15 from StringIO import StringIO
1.16
1.17 class Transaction(Generic.Transaction):
1.18 @@ -34,12 +34,12 @@
1.19 self.response_code = 200
1.20 self.content = StringIO()
1.21 self.headers_out = {}
1.22 - self.cookies_out = Cookie.SimpleCookie()
1.23 + self.cookies_out = SimpleCookie()
1.24 self.user = None
1.25
1.26 # Define the incoming cookies.
1.27
1.28 - self.cookies_in = Cookie.SimpleCookie(self.get_headers().get("cookie"))
1.29 + self.cookies_in = SimpleCookie(self.get_headers().get("cookie"))
1.30
1.31 # Cached information.
1.32
1.33 @@ -304,7 +304,7 @@
1.34 Returns a dictionary mapping cookie names to cookie objects.
1.35 """
1.36
1.37 - return self.cookies_in
1.38 + return self.process_cookies(self.cookies_in)
1.39
1.40 def get_cookie(self, cookie_name):
1.41
1.42 @@ -315,7 +315,11 @@
1.43 cookie exists.
1.44 """
1.45
1.46 - return self.cookies_in.get(cookie_name)
1.47 + cookie = self.cookies_in.get(self.encode_cookie_value(cookie_name))
1.48 + if cookie is not None:
1.49 + return Cookie(cookie_name, self.decode_cookie_value(cookie.value))
1.50 + else:
1.51 + return None
1.52
1.53 # Response-related methods.
1.54
1.55 @@ -395,7 +399,7 @@
1.56 # NOTE: If multiple cookies of the same name could be specified, this
1.57 # NOTE: could need changing.
1.58
1.59 - self.cookies_out[cookie.name] = cookie.value
1.60 + self.set_cookie_value(cookie.name, cookie.value)
1.61
1.62 def set_cookie_value(self, name, value, path=None, expires=None):
1.63
1.64 @@ -407,7 +411,8 @@
1.65 time.time function, and indicates the expiry date/time of the cookie.
1.66 """
1.67
1.68 - self.cookies_out[name] = value
1.69 + name = self.encode_cookie_value(name)
1.70 + self.cookies_out[name] = self.encode_cookie_value(value)
1.71 if path is not None:
1.72 self.cookies_out[name]["path"] = path
1.73 if expires is not None:
1.74 @@ -424,10 +429,11 @@
1.75 # has been sent the cookie or not.
1.76 # NOTE: Magic discovered in Webware.
1.77
1.78 - self.cookies_out[cookie_name] = ""
1.79 - self.cookies_out[cookie_name]["path"] = "/"
1.80 - self.cookies_out[cookie_name]["expires"] = 0
1.81 - self.cookies_out[cookie_name]["max-age"] = 0
1.82 + name = self.encode_cookie_value(cookie_name)
1.83 + self.cookies_out[name] = ""
1.84 + self.cookies_out[name]["path"] = "/"
1.85 + self.cookies_out[name]["expires"] = 0
1.86 + self.cookies_out[name]["max-age"] = 0
1.87
1.88 # Session-related methods.
1.89
2.1 --- a/WebStack/CGI.py Thu Sep 16 22:18:33 2004 +0000
2.2 +++ b/WebStack/CGI.py Thu Sep 16 22:19:45 2004 +0000
2.3 @@ -6,13 +6,13 @@
2.4
2.5 import Generic
2.6 import os, sys
2.7 -from Helpers.Request import MessageBodyStream, get_body_fields, get_storage_items
2.8 +from Helpers.Request import MessageBodyStream, get_body_fields, get_storage_items, Cookie
2.9 from Helpers.Response import ConvertingStream
2.10 from Helpers.Auth import UserInfo
2.11 from Helpers.Session import SessionStore
2.12 from Helpers import Environment
2.13 from cgi import parse_qs, FieldStorage
2.14 -import Cookie
2.15 +from Cookie import SimpleCookie
2.16 from StringIO import StringIO
2.17
2.18 class Transaction(Generic.Transaction):
2.19 @@ -39,12 +39,12 @@
2.20 self.response_code = 200
2.21 self.content = StringIO()
2.22 self.headers_out = {}
2.23 - self.cookies_out = Cookie.SimpleCookie()
2.24 + self.cookies_out = SimpleCookie()
2.25 self.user = None
2.26
2.27 # Define the incoming cookies.
2.28
2.29 - self.cookies_in = Cookie.SimpleCookie(self.env.get("HTTP_COOKIE"))
2.30 + self.cookies_in = SimpleCookie(self.env.get("HTTP_COOKIE"))
2.31
2.32 # Cached information.
2.33
2.34 @@ -294,7 +294,7 @@
2.35 Returns a dictionary mapping cookie names to cookie objects.
2.36 """
2.37
2.38 - return self.cookies_in
2.39 + return self.process_cookies(self.cookies_in)
2.40
2.41 def get_cookie(self, cookie_name):
2.42
2.43 @@ -305,7 +305,11 @@
2.44 cookie exists.
2.45 """
2.46
2.47 - return self.cookies_in.get(cookie_name)
2.48 + cookie = self.cookies_in.get(self.encode_cookie_value(cookie_name))
2.49 + if cookie is not None:
2.50 + return Cookie(cookie_name, self.decode_cookie_value(cookie.value))
2.51 + else:
2.52 + return None
2.53
2.54 # Response-related methods.
2.55
2.56 @@ -385,7 +389,7 @@
2.57 # NOTE: If multiple cookies of the same name could be specified, this
2.58 # NOTE: could need changing.
2.59
2.60 - self.cookies_out[cookie.name] = cookie.value
2.61 + self.set_cookie_value(cookie.name, cookie.value)
2.62
2.63 def set_cookie_value(self, name, value, path=None, expires=None):
2.64
2.65 @@ -397,7 +401,8 @@
2.66 time.time function, and indicates the expiry date/time of the cookie.
2.67 """
2.68
2.69 - self.cookies_out[name] = value
2.70 + name = self.encode_cookie_value(name)
2.71 + self.cookies_out[name] = self.encode_cookie_value(value)
2.72 if path is not None:
2.73 self.cookies_out[name]["path"] = path
2.74 if expires is not None:
2.75 @@ -414,10 +419,11 @@
2.76 # has been sent the cookie or not.
2.77 # NOTE: Magic discovered in Webware.
2.78
2.79 - self.cookies_out[cookie_name] = ""
2.80 - self.cookies_out[cookie_name]["path"] = "/"
2.81 - self.cookies_out[cookie_name]["expires"] = 0
2.82 - self.cookies_out[cookie_name]["max-age"] = 0
2.83 + name = self.encode_cookie_value(cookie_name)
2.84 + self.cookies_out[name] = ""
2.85 + self.cookies_out[name]["path"] = "/"
2.86 + self.cookies_out[name]["expires"] = 0
2.87 + self.cookies_out[name]["max-age"] = 0
2.88
2.89 # Session-related methods.
2.90
3.1 --- a/WebStack/Generic.py Thu Sep 16 22:18:33 2004 +0000
3.2 +++ b/WebStack/Generic.py Thu Sep 16 22:19:45 2004 +0000
3.3 @@ -15,6 +15,9 @@
3.4 directors.
3.5 """
3.6
3.7 +import urllib
3.8 +from WebStack.Helpers.Request import Cookie
3.9 +
3.10 class HeaderValue:
3.11
3.12 "A container for header information."
3.13 @@ -140,6 +143,46 @@
3.14
3.15 return value.encode("US-ASCII")
3.16
3.17 + def encode_cookie_value(self, value):
3.18 +
3.19 + """
3.20 + Encode the given cookie 'value'. This ensures the usage of US-ASCII
3.21 + through the encoding of Unicode objects as URL-encoded UTF-8 text.
3.22 + """
3.23 +
3.24 + return urllib.quote(value.encode("UTF-8")).encode("US-ASCII")
3.25 +
3.26 + def decode_cookie_value(self, value):
3.27 +
3.28 + """
3.29 + Decode the given cookie 'value'.
3.30 + """
3.31 +
3.32 + return unicode(urllib.unquote(value), "UTF-8")
3.33 +
3.34 + def process_cookies(self, cookie_dict, using_strings=0):
3.35 +
3.36 + """
3.37 + Process the given 'cookie_dict', returning a dictionary mapping cookie names
3.38 + to cookie objects where the names and values have been decoded from the form
3.39 + used in the cookies retrieved from the request.
3.40 +
3.41 + The optional 'using_strings', if set to 1, treats the 'cookie_dict' as a
3.42 + mapping of cookie names to values.
3.43 + """
3.44 +
3.45 + cookies = {}
3.46 + for name in cookie_dict.keys():
3.47 + if using_strings:
3.48 + value = cookie_dict[name]
3.49 + else:
3.50 + cookie = cookie_dict[name]
3.51 + value = cookie.value
3.52 + cookie_name = self.decode_cookie_value(name)
3.53 + cookie_value = self.decode_cookie_value(value)
3.54 + cookies[cookie_name] = Cookie(cookie_name, cookie_value)
3.55 + return cookies
3.56 +
3.57 def parse_content_preferences(self, accept_preference):
3.58
3.59 """
4.1 --- a/WebStack/JavaServlet.py Thu Sep 16 22:18:33 2004 +0000
4.2 +++ b/WebStack/JavaServlet.py Thu Sep 16 22:19:45 2004 +0000
4.3 @@ -74,8 +74,8 @@
4.4
4.5 self.cookies_in = {}
4.6 for cookie in self.request.getCookies() or []:
4.7 - cookie_name = cookie.getName()
4.8 - self.cookies_in[cookie_name] = Cookie(cookie_name, cookie.getValue())
4.9 + cookie_name = self.decode_cookie_value(cookie.getName())
4.10 + self.cookies_in[cookie_name] = Cookie(cookie_name, self.decode_cookie_value(cookie.getValue()))
4.11
4.12 # Cached information.
4.13
4.14 @@ -416,8 +416,7 @@
4.15 Stores the given 'cookie' object in the response.
4.16 """
4.17
4.18 - new_cookie = javax.servlet.http.Cookie(cookie.name, cookie.value)
4.19 - self.response.addCookie(new_cookie)
4.20 + self.set_cookie_value(cookie.name, cookie.value)
4.21
4.22 def set_cookie_value(self, name, value, path=None, expires=None):
4.23
4.24 @@ -429,7 +428,8 @@
4.25 time.time function, and indicates the expiry date/time of the cookie.
4.26 """
4.27
4.28 - cookie = javax.servlet.http.Cookie(name, value)
4.29 + cookie = javax.servlet.http.Cookie(self.encode_cookie_value(name),
4.30 + self.encode_cookie_value(value))
4.31 if path is not None:
4.32 cookie.setPath(path)
4.33
4.34 @@ -448,7 +448,7 @@
4.35 # has been sent the cookie or not.
4.36 # NOTE: Magic discovered in Webware.
4.37
4.38 - cookie = javax.servlet.http.Cookie(cookie_name, "")
4.39 + cookie = javax.servlet.http.Cookie(self.encode_cookie_value(cookie_name), "")
4.40 cookie.setPath("/")
4.41 cookie.setMaxAge(0)
4.42 self.response.addCookie(cookie)
5.1 --- a/WebStack/ModPython.py Thu Sep 16 22:18:33 2004 +0000
5.2 +++ b/WebStack/ModPython.py Thu Sep 16 22:19:45 2004 +0000
5.3 @@ -5,7 +5,7 @@
5.4 """
5.5
5.6 import Generic
5.7 -from Helpers.Request import get_body_field, filter_fields
5.8 +from Helpers.Request import get_body_field, filter_fields, Cookie
5.9 from Helpers.Response import ConvertingStream
5.10 from mod_python.util import parse_qs, FieldStorage
5.11 from mod_python import apache
5.12 @@ -14,10 +14,11 @@
5.13 # The alternative session support requires cookie support of some kind.
5.14
5.15 try:
5.16 - from mod_python import Cookie
5.17 + from mod_python.Cookie import get_cookies, add_cookie, Cookie as SimpleCookie
5.18 + have_cookies = 1
5.19 except ImportError:
5.20 from Cookie import SimpleCookie
5.21 - Cookie = None
5.22 + have_cookies = 0
5.23 try:
5.24 from mod_python import Session
5.25 except ImportError:
5.26 @@ -42,7 +43,7 @@
5.27
5.28 # Support non-framework cookies.
5.29
5.30 - if Cookie is None:
5.31 + if not have_cookies:
5.32
5.33 # Define the incoming cookies.
5.34
5.35 @@ -273,10 +274,11 @@
5.36 NOTE: support for enhanced cookies in mod_python.
5.37 """
5.38
5.39 - if Cookie:
5.40 - return Cookie.get_cookies(self.trans)
5.41 + if have_cookies:
5.42 + found_cookies = get_cookies(self.trans)
5.43 else:
5.44 - return self.cookies_in
5.45 + found_cookies = self.cookies_in
5.46 + return self.process_cookies(found_cookies)
5.47
5.48 def get_cookie(self, cookie_name):
5.49
5.50 @@ -287,7 +289,11 @@
5.51 cookie exists.
5.52 """
5.53
5.54 - return self.get_cookies().get(cookie_name)
5.55 + cookie = self.get_cookies().get(self.encode_cookie_value(cookie_name))
5.56 + if cookie is not None:
5.57 + return Cookie(cookie_name, self.decode_cookie_value(cookie.value))
5.58 + else:
5.59 + return None
5.60
5.61 # Response-related methods.
5.62
5.63 @@ -360,15 +366,10 @@
5.64 Stores the given 'cookie' object in the response.
5.65 """
5.66
5.67 - if Cookie:
5.68 - Cookie.add_cookie(self.trans, cookie)
5.69 - else:
5.70 - # NOTE: If multiple cookies of the same name could be specified, this
5.71 - # NOTE: could need changing.
5.72 + # NOTE: If multiple cookies of the same name could be specified, this
5.73 + # NOTE: could need changing.
5.74
5.75 - cookie_out = SimpleCookie()
5.76 - cookie_out[cookie.name] = cookie.value
5.77 - self._write_cookie(cookie_out)
5.78 + self.set_cookie_value(cookie.name, cookie.value)
5.79
5.80 def set_cookie_value(self, name, value, path=None, expires=None):
5.81
5.82 @@ -380,18 +381,18 @@
5.83 time.time function, and indicates the expiry date/time of the cookie.
5.84 """
5.85
5.86 - # NOTE: We just hope that Cookie converts Unicode arguments to US-ASCII.
5.87 + name = self.encode_cookie_value(name)
5.88
5.89 - if Cookie:
5.90 - cookie = Cookie.Cookie(name, value)
5.91 + if have_cookies:
5.92 + cookie = SimpleCookie(name, self.encode_cookie_value(value))
5.93 if expires is not None:
5.94 cookie.expires = expires
5.95 if path is not None:
5.96 cookie.path = path
5.97 - Cookie.add_cookie(self.trans, cookie)
5.98 + add_cookie(self.trans, cookie)
5.99 else:
5.100 cookie_out = SimpleCookie()
5.101 - cookie_out[name] = value
5.102 + cookie_out[name] = self.encode_cookie_value(value)
5.103 if path is not None:
5.104 cookie_out[name]["path"] = path
5.105 if expires is not None:
5.106 @@ -409,18 +410,20 @@
5.107 # has been sent the cookie or not.
5.108 # NOTE: Magic discovered in Webware.
5.109
5.110 - if Cookie:
5.111 - cookie = Cookie.Cookie(cookie_name, "")
5.112 + name = self.encode_cookie_value(cookie_name)
5.113 +
5.114 + if have_cookies:
5.115 + cookie = SimpleCookie(name, "")
5.116 cookie.path = "/"
5.117 cookie.expires = 0
5.118 cookie.max_age = 0
5.119 - Cookie.add_cookie(self.trans, cookie)
5.120 + add_cookie(self.trans, cookie)
5.121 else:
5.122 cookie_out = SimpleCookie()
5.123 - cookie_out[cookie_name] = ""
5.124 - cookie_out[cookie_name]["path"] = "/"
5.125 - cookie_out[cookie_name]["expires"] = 0
5.126 - cookie_out[cookie_name]["max-age"] = 0
5.127 + cookie_out[name] = ""
5.128 + cookie_out[name]["path"] = "/"
5.129 + cookie_out[name]["expires"] = 0
5.130 + cookie_out[name]["max-age"] = 0
5.131 self._write_cookie(cookie_out)
5.132
5.133 def _write_cookie(self, cookie):
6.1 --- a/WebStack/Twisted.py Thu Sep 16 22:18:33 2004 +0000
6.2 +++ b/WebStack/Twisted.py Thu Sep 16 22:19:45 2004 +0000
6.3 @@ -264,10 +264,7 @@
6.4 NOTE: introduced.
6.5 """
6.6
6.7 - cookies = {}
6.8 - for name, value in self.trans.received_cookies.items():
6.9 - cookies[name] = Cookie(name, value)
6.10 - return cookies
6.11 + return self.process_cookies(self.trans.received_cookies, using_strings=1)
6.12
6.13 def get_cookie(self, cookie_name):
6.14
6.15 @@ -280,7 +277,11 @@
6.16 NOTE: introduced.
6.17 """
6.18
6.19 - return Cookie(cookie_name, self.trans.getCookie(cookie_name))
6.20 + value = self.trans.getCookie(self.encode_cookie_value(cookie_name))
6.21 + if value is not None:
6.22 + return Cookie(cookie_name, self.decode_cookie_value(value))
6.23 + else:
6.24 + return None
6.25
6.26 # Response-related methods.
6.27
6.28 @@ -355,7 +356,7 @@
6.29 Stores the given 'cookie' object in the response.
6.30 """
6.31
6.32 - self.trans.addCookie(cookie.name, cookie.value, expires=cookie.expires, path=cookie.path)
6.33 + self.set_cookie_value(cookie.name, cookie.value, path=cookie.path, expires=cookie.expires)
6.34
6.35 def set_cookie_value(self, name, value, path=None, expires=None):
6.36
6.37 @@ -367,8 +368,8 @@
6.38 time.time function, and indicates the expiry date/time of the cookie.
6.39 """
6.40
6.41 - self.trans.addCookie(self.format_header_value(name),
6.42 - self.format_header_value(value), expires=expires, path=path)
6.43 + self.trans.addCookie(self.encode_cookie_value(name),
6.44 + self.encode_cookie_value(value), expires=expires, path=path)
6.45
6.46 def delete_cookie(self, cookie_name):
6.47
6.48 @@ -381,7 +382,7 @@
6.49 # has been sent the cookie or not.
6.50 # NOTE: Magic discovered in Webware.
6.51
6.52 - self.trans.addCookie(cookie_name, "", expires=0, path="/", max_age=0)
6.53 + self.trans.addCookie(self.encode_cookie_value(cookie_name), "", expires=0, path="/", max_age=0)
6.54
6.55 # Session-related methods.
6.56
7.1 --- a/WebStack/Webware.py Thu Sep 16 22:18:33 2004 +0000
7.2 +++ b/WebStack/Webware.py Thu Sep 16 22:19:45 2004 +0000
7.3 @@ -254,10 +254,7 @@
7.4 Returns a dictionary mapping cookie names to cookie objects.
7.5 """
7.6
7.7 - cookies = {}
7.8 - for name, value in self.trans.request().cookies().items():
7.9 - cookies[name] = Cookie(name, value)
7.10 - return cookies
7.11 + return self.process_cookies(self.trans.request().cookies(), using_strings=1)
7.12
7.13 def get_cookie(self, cookie_name):
7.14
7.15 @@ -269,7 +266,8 @@
7.16 """
7.17
7.18 try:
7.19 - return Cookie(cookie_name, self.trans.request().cookie(cookie_name))
7.20 + value = self.trans.request().cookie(self.encode_cookie_value(cookie_name))
7.21 + return Cookie(cookie_name, self.decode_cookie_value(value))
7.22 except KeyError:
7.23 return None
7.24
7.25 @@ -353,7 +351,8 @@
7.26 Stores the given 'cookie' object in the response.
7.27 """
7.28
7.29 - self.trans.response().addCookie(cookie)
7.30 + self.set_cookie_value(cookie.name, cookie.value)
7.31 + #self.trans.response().addCookie(cookie)
7.32
7.33 def set_cookie_value(self, name, value, path=None, expires=None):
7.34
7.35 @@ -365,7 +364,8 @@
7.36 time.time function, and indicates the expiry date/time of the cookie.
7.37 """
7.38
7.39 - self.trans.response().setCookie(name, value, path, expires)
7.40 + self.trans.response().setCookie(self.encode_cookie_value(name),
7.41 + self.encode_cookie_value(value), path, expires)
7.42
7.43 def delete_cookie(self, cookie_name):
7.44
7.45 @@ -374,7 +374,7 @@
7.46 'cookie_name' be deleted/discarded by the client.
7.47 """
7.48
7.49 - self.trans.response().delCookie(cookie_name)
7.50 + self.trans.response().delCookie(self.encode_cookie_value(cookie_name))
7.51
7.52 # Session-related methods.
7.53
8.1 --- a/WebStack/Zope.py Thu Sep 16 22:18:33 2004 +0000
8.2 +++ b/WebStack/Zope.py Thu Sep 16 22:19:45 2004 +0000
8.3 @@ -255,10 +255,7 @@
8.4 Returns a dictionary mapping cookie names to cookie objects.
8.5 """
8.6
8.7 - cookies = {}
8.8 - for name, value in self.request.cookies.items():
8.9 - cookies[name] = Cookie(name, value)
8.10 - return cookies
8.11 + return self.process_cookies(self.request.cookies, using_strings=1)
8.12
8.13 def get_cookie(self, cookie_name):
8.14
8.15 @@ -269,7 +266,11 @@
8.16 cookie exists.
8.17 """
8.18
8.19 - return Cookie(cookie_name, self.request.cookies.get(cookie_name))
8.20 + value = self.request.cookies.get(self.encode_cookie_value(cookie_name))
8.21 + if value is not None:
8.22 + return Cookie(cookie_name, value)
8.23 + else:
8.24 + return None
8.25
8.26 # Response-related methods.
8.27
8.28 @@ -352,7 +353,7 @@
8.29 time.time function, and indicates the expiry date/time of the cookie.
8.30 """
8.31
8.32 - self.response.setCookie(name, value)
8.33 + self.response.setCookie(self.encode_cookie_value(name), self.encode_cookie_value(value))
8.34
8.35 def delete_cookie(self, cookie_name):
8.36
8.37 @@ -361,7 +362,7 @@
8.38 'cookie_name' be deleted/discarded by the client.
8.39 """
8.40
8.41 - self.response.expireCookie(cookie_name)
8.42 + self.response.expireCookie(self.encode_cookie_value(cookie_name))
8.43
8.44 # Session-related methods.
8.45