Author: russellm
Date: 2011-06-10 01:39:38 -0700 (Fri, 10 Jun 2011)
New Revision: 16353

Modified:
   django/trunk/django/http/multipartparser.py
   django/trunk/tests/regressiontests/requests/tests.py
Log:
Fixed #16201 -- Ensure that requests with Content-Length=0 don't break the 
multipart parser. Thanks to albsen for the report and patch

Modified: django/trunk/django/http/multipartparser.py
===================================================================
--- django/trunk/django/http/multipartparser.py 2011-06-10 08:26:05 UTC (rev 
16352)
+++ django/trunk/django/http/multipartparser.py 2011-06-10 08:39:38 UTC (rev 
16353)
@@ -75,7 +75,7 @@
             # For now set it to 0; we'll try again later on down.
             content_length = 0
 
-        if content_length <= 0:
+        if content_length < 0:
             # This means we shouldn't continue...raise an error.
             raise MultiPartParserError("Invalid content length: %r" % 
content_length)
 
@@ -105,6 +105,11 @@
         encoding = self._encoding
         handlers = self._upload_handlers
 
+        # HTTP spec says that Content-Length >= 0 is valid
+        # handling content-length == 0 before continuing
+        if self._content_length == 0:
+            return QueryDict(MultiValueDict(), encoding=self._encoding), 
MultiValueDict()
+
         limited_input_data = LimitBytes(self._input_data, self._content_length)
 
         # See if the handler will want to take care of the parsing.

Modified: django/trunk/tests/regressiontests/requests/tests.py
===================================================================
--- django/trunk/tests/regressiontests/requests/tests.py        2011-06-10 
08:26:05 UTC (rev 16352)
+++ django/trunk/tests/regressiontests/requests/tests.py        2011-06-10 
08:39:38 UTC (rev 16353)
@@ -239,6 +239,27 @@
         self.assertEqual(request.POST, {u'name': [u'value']})
         self.assertRaises(Exception, lambda: request.raw_post_data)
 
+    def test_POST_multipart_with_content_length_zero(self):
+        """
+        Multipart POST requests with Content-Length >= 0 are valid and need to 
be handled.
+        """
+        # According to:
+        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
+        # Every request.POST with Content-Length >= 0 is a valid request,
+        # this test ensures that we handle Content-Length == 0.
+        payload = "\r\n".join([
+                '--boundary',
+                'Content-Disposition: form-data; name="name"',
+                '',
+                'value',
+                '--boundary--'
+                ''])
+        request = WSGIRequest({'REQUEST_METHOD': 'POST',
+                               'CONTENT_TYPE': 'multipart/form-data; 
boundary=boundary',
+                               'CONTENT_LENGTH': 0,
+                               'wsgi.input': StringIO(payload)})
+        self.assertEqual(request.POST, {})
+
     def test_read_by_lines(self):
         request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': 
StringIO('name=value')})
         self.assertEqual(list(request), ['name=value'])

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to