Ticket #13007: 13007.1.diff

File 13007.1.diff, 3.0 KB (added by Ramiro Morales, 12 years ago)

Patch attached to trunk as of r15399

  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    a b  
    2727_morsel_supports_httponly = Cookie.Morsel._reserved.has_key('httponly')
    2828# Some versions of Python 2.7 and later won't need this encoding bug fix:
    2929_cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', '"\\073"')
     30# See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256
     31_tc = Cookie.SimpleCookie()
     32_tc.load('f:oo')
     33_cookie_allows_colon_in_names = 'Set-Cookie: f:oo=' in _tc.output()
    3034
    31 if _morsel_supports_httponly and _cookie_encodes_correctly:
     35if _morsel_supports_httponly and _cookie_encodes_correctly and _cookie_allows_colon_in_names:
    3236    SimpleCookie = Cookie.SimpleCookie
    3337else:
    3438    if not _morsel_supports_httponly:
     
    8589
    8690                return val, encoded
    8791
     92        if not _cookie_allows_colon_in_names:
     93            def load(self, rawdata, ignore_parse_errors=False):
     94                if ignore_parse_errors:
     95                    self.bad_cookies = []
     96                    self._BaseCookie__set = self._loose_set
     97                super(SimpleCookie, self).load(rawdata)
     98                if ignore_parse_errors:
     99                    self._BaseCookie__set = self._strict_set
     100                    for key in self.bad_cookies:
     101                        del self[key]
     102
     103            _strict_set = Cookie.BaseCookie._BaseCookie__set
     104
     105            def _loose_set(self, key, real_value, coded_value):
     106                try:
     107                    self._strict_set(key, real_value, coded_value)
     108                except Cookie.CookieError:
     109                    self.bad_cookies.append(key)
     110                    dict.__setitem__(self, key, None)
     111
     112
    88113class CompatCookie(SimpleCookie):
    89114    def __init__(self, *args, **kwargs):
    90115        super(CompatCookie, self).__init__(*args, **kwargs)
     
    433458    if not isinstance(cookie, Cookie.BaseCookie):
    434459        try:
    435460            c = SimpleCookie()
    436             c.load(cookie)
     461            c.load(cookie, ignore_parse_errors=True)
    437462        except Cookie.CookieError:
    438463            # Invalid cookie
    439464            return {}
  • tests/regressiontests/httpwrappers/tests.py

    diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
    a b  
    11import copy
    22import pickle
    33
    4 from django.http import QueryDict, HttpResponse, SimpleCookie, BadHeaderError
     4from django.http import (QueryDict, HttpResponse, SimpleCookie, BadHeaderError,
     5        parse_cookie)
    56from django.utils import unittest
    67
    78class QueryDictTests(unittest.TestCase):
     
    274275        c2 = SimpleCookie()
    275276        c2.load(c.output())
    276277        self.assertEqual(c['test'].value, c2['test'].value)
     278
     279    def test_nonstandard_keys(self):
     280        """
     281        Test that a single non-standard cookie name doesn't affect all cookies.
     282        """
     283        self.assertTrue('good_cookie' in parse_cookie('good_cookie=yes;bad:cookie=yes').keys())
Back to Top