Ticket #17235: t17235.patch

File t17235.patch, 3.7 KB (added by Hiroki Kiyohara, 12 years ago)

Improved requests POST and FILES to be handled as immutable.

  • django/http/multipartparser.py

    diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
    index 070874f..fa11488 100644
    a b import cgi  
    1111
    1212from django.conf import settings
    1313from django.core.exceptions import SuspiciousOperation
    14 from django.utils.datastructures import MultiValueDict
     14from django.utils.datastructures import ImmutableMultiValueDict
    1515from django.utils.encoding import force_text
    1616from django.utils import six
    1717from django.utils.text import unescape_entities
    class MultiPartParser(object):  
    110110        # HTTP spec says that Content-Length >= 0 is valid
    111111        # handling content-length == 0 before continuing
    112112        if self._content_length == 0:
    113             return QueryDict('', encoding=self._encoding), MultiValueDict()
     113            return QueryDict('', mutable=True, encoding=self._encoding), ImmutableMultiValueDict()
    114114
    115115        # See if the handler will want to take care of the parsing.
    116116        # This allows overriding everything if somebody wants it.
    class MultiPartParser(object):  
    125125
    126126        # Create the data structures to be used later.
    127127        self._post = QueryDict('', mutable=True)
    128         self._files = MultiValueDict()
     128        self._files = ImmutableMultiValueDict(mutable=True)
    129129
    130130        # Instantiate the parser and stream:
    131131        stream = LazyStream(ChunkIter(self._input_data, self._chunk_size))
    class MultiPartParser(object):  
    242242            if retval:
    243243                break
    244244
     245        self._post._mutable = self._files._mutable = False
     246
    245247        return self._post, self._files
    246248
    247249    def handle_file_complete(self, old_field_name, counters):
  • django/utils/datastructures.py

    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index 64c218f..a8652cd 100644
    a b class MultiValueDict(dict):  
    443443        return dict((key, self[key]) for key in self)
    444444
    445445
     446class ImmutableMultiValueDict(MultiValueDict):
     447    _mutbale = False
     448
     449    def __init__(self, key_to_list_mapping=(), mutable=False):
     450        super(ImmutableMultiValueDict, self).__init__(key_to_list_mapping)
     451        self._mutable = mutable
     452
     453    def _assert_mutable(self):
     454        if not self._mutable:
     455            raise AttributeError("This ImmutableMultiValueDict instance is immutable")
     456
     457    def __setitem__(self, key, value):
     458        self._assert_mutable()
     459        super(ImmutableMultiValueDict, self).__setitem__(key, value)
     460
     461    def __delitem__(self, key):
     462        self._assert_mutable()
     463        super(ImmutableMultiValueDict, self).__delitem__(key)
     464   
     465    def setlist(self, key, list_):
     466        self._assert_mutable()
     467        super(ImmutableMultiValueDict, self).setlist(key, list_)
     468
     469    def setlistdefault(self, key, default_list=None):
     470        self._assert_mutable()
     471        return super(ImmutableMultiValueDict, self).setlistdefault(key, default_list)
     472
     473    def appendlist(self, key, value):
     474        self._assert_mutable()
     475        super(ImmutableMultiValueDict, self).appendlist(key, value)
     476
     477    def pop(self, key, *args):
     478        self._assert_mutable()
     479        return super(ImmutableMultiValueDict, self).pop(key, *args)
     480
     481    def popitem(self):
     482        self._assert_mutable()
     483        return super(ImmutableMultiValueDict, self).popitem()
     484
     485    def clear(self):
     486        self._assert_mutable()
     487        super(ImmutableMultiValueDict, self).clear()
     488
     489    def setdefault(self, key, default=None):
     490        self._assert_mutable()
     491        return super(ImmutableMultiValueDict, self).setdefault(key, default)
     492
     493
    446494class ImmutableList(tuple):
    447495    """
    448496    A tuple-like object that raises useful errors when it is asked to mutate.
Back to Top