Ticket #13007: 13007.diff

File 13007.diff, 2.2 KB (added by Ubercore, 12 years ago)

Patch based on Trac's solution to the problem

  • django/http/__init__.py

     
    366366
    367367        return val, encoded
    368368
     369    def load(self, rawdata, ignore_parse_errors=False):
     370        if ignore_parse_errors:
     371            self.bad_cookies = []
     372            self._BaseCookie__set = self._loose_set
     373        SimpleCookie.load(self, rawdata)
     374        if ignore_parse_errors:
     375            self._BaseCookie__set = self._strict_set
     376            for key in self.bad_cookies:
     377                del self[key]
     378
     379    _strict_set = BaseCookie._BaseCookie__set
     380
     381    def _loose_set(self, key, real_value, coded_value):
     382        try:
     383            self._strict_set(key, real_value, coded_value)
     384        except CookieError:
     385            self.bad_cookies.append(key)
     386            dict.__setitem__(self, key, None)
     387
     388
    369389def parse_cookie(cookie):
    370390    if cookie == '':
    371391        return {}
    372392    if not isinstance(cookie, BaseCookie):
    373393        try:
    374394            c = CompatCookie()
    375             c.load(cookie)
     395            c.load(cookie, ignore_parse_errors=True)
    376396        except CookieError:
    377397            # Invalid cookie
    378398            return {}
  • tests/regressiontests/httpwrappers/tests.py

     
    11import copy
    22import pickle
    33
    4 from django.http import QueryDict, HttpResponse, CompatCookie, BadHeaderError
     4from django.http import QueryDict, HttpResponse, CompatCookie, BadHeaderError, \
     5                        parse_cookie
    56from django.utils import unittest
    67
    78class QueryDictTests(unittest.TestCase):
     
    264265        c2 = CompatCookie()
    265266        c2.load(c.output())
    266267        self.assertEqual(c['test'].value, c2['test'].value)
     268   
     269    def test_nonstandard_keys(self):
     270        """
     271        Test that a single non-standard cookie name doesn't affect all cookies.
     272        """
     273        self.assert_('good_cookie' in parse_cookie('good_cookie=yes;bad:cookie=yes').keys())
Back to Top