Fixed our SimpleCookie overriding and use to be compatible with a (potential) stdlib SimpleCookie that fixes http://bugs.python.org/issue2193

The previous code tested the stdlib in a way that would always fail. It then
used an overridden SimpleCookie.load method that wouldn't work for the
stdlib. And it did some completely unnecessary monkey patching.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16485 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2011-06-29 15:12:48 +00:00
parent b8f0346c11
commit 89e0e8b6bc
1 changed files with 12 additions and 12 deletions

View File

@ -28,8 +28,11 @@ _morsel_supports_httponly = Cookie.Morsel._reserved.has_key('httponly')
_cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', '"\\073"') _cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', '"\\073"')
# See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256 # See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256
_tc = Cookie.SimpleCookie() _tc = Cookie.SimpleCookie()
_tc.load('f:oo') try:
_cookie_allows_colon_in_names = 'Set-Cookie: f:oo=' in _tc.output() _tc.load('foo:bar=1')
_cookie_allows_colon_in_names = True
except Cookie.CookieError:
_cookie_allows_colon_in_names = False
if _morsel_supports_httponly and _cookie_encodes_correctly and _cookie_allows_colon_in_names: if _morsel_supports_httponly and _cookie_encodes_correctly and _cookie_allows_colon_in_names:
SimpleCookie = Cookie.SimpleCookie SimpleCookie = Cookie.SimpleCookie
@ -89,19 +92,16 @@ else:
return val, encoded return val, encoded
if not _cookie_allows_colon_in_names: if not _cookie_allows_colon_in_names:
def load(self, rawdata, ignore_parse_errors=False): def load(self, rawdata):
if ignore_parse_errors: self.bad_cookies = set()
self.bad_cookies = set()
self._BaseCookie__set = self._loose_set
super(SimpleCookie, self).load(rawdata) super(SimpleCookie, self).load(rawdata)
if ignore_parse_errors: for key in self.bad_cookies:
self._BaseCookie__set = self._strict_set del self[key]
for key in self.bad_cookies:
del self[key]
_strict_set = Cookie.BaseCookie._BaseCookie__set _strict_set = Cookie.BaseCookie._BaseCookie__set
def _loose_set(self, key, real_value, coded_value): # override private __set() method:
def _BaseCookie__set(self, key, real_value, coded_value):
try: try:
self._strict_set(key, real_value, coded_value) self._strict_set(key, real_value, coded_value)
except Cookie.CookieError: except Cookie.CookieError:
@ -519,7 +519,7 @@ def parse_cookie(cookie):
if not isinstance(cookie, Cookie.BaseCookie): if not isinstance(cookie, Cookie.BaseCookie):
try: try:
c = SimpleCookie() c = SimpleCookie()
c.load(cookie, ignore_parse_errors=True) c.load(cookie)
except Cookie.CookieError: except Cookie.CookieError:
# Invalid cookie # Invalid cookie
return {} return {}