Ticket #16201: multipartparser_content_length_0.diff

File multipartparser_content_length_0.diff, 2.5 KB (added by asd, 13 years ago)

patch for multipartparser bug

  • django/http/multipartparser.py

     
    7575            # For now set it to 0; we'll try again later on down.
    7676            content_length = 0
    7777
    78         if content_length <= 0:
     78        if content_length < 0:
    7979            # This means we shouldn't continue...raise an error.
    8080            raise MultiPartParserError("Invalid content length: %r" % content_length)
    8181
     
    105105        encoding = self._encoding
    106106        handlers = self._upload_handlers
    107107
     108        # HTTP spec says that Content-Length >= 0 is valid
     109        # handling content-length == 0 before continuing
     110        if self._content_length == 0:
     111            return QueryDict(MultiValueDict(), encoding=self._encoding), MultiValueDict()
     112
    108113        limited_input_data = LimitBytes(self._input_data, self._content_length)
    109114
    110115        # See if the handler will want to take care of the parsing.
  • tests/regressiontests/requests/tests.py

     
    200200        self.assertEqual(request.POST, {u'name': [u'value']})
    201201        self.assertRaises(Exception, lambda: request.raw_post_data)
    202202
     203    def test_POST_multipart_with_content_length_zero(self):
     204        """
     205        Multipart POST requests with Content-Length >= 0 are valid and need to be handled.
     206        """
     207        # According to:
     208        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
     209        # Every request.POST with Content-Length >= 0 is a valid request,
     210        # this test ensures that we handle Content-Length == 0.
     211        payload = "\r\n".join([
     212                '--boundary',
     213                'Content-Disposition: form-data; name="name"',
     214                '',
     215                'value',
     216                '--boundary--'
     217                ''])
     218        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     219                               'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
     220                               'CONTENT_LENGTH': 0,
     221                               'wsgi.input': StringIO(payload)})
     222        self.assertEqual(request.POST, {})
     223
    203224    def test_read_by_lines(self):
    204225        request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
    205226        self.assertEqual(list(request), ['name=value'])
Back to Top