Ticket #5611: 5611-5.diff

File 5611-5.diff, 7.5 KB (added by Claude Paroz, 12 years ago)

Patch including docs

  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index ecb3912..0b13857 100644
    a b class HttpRequest(object):  
    310310        self._post_parse_error = True
    311311
    312312    def _load_post_and_files(self):
    313         # Populates self._post and self._files
     313        """ Populates self._post and self._files if the content-type is a form type """
    314314        if self.method != 'POST':
    315315            self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()
    316316            return
    class HttpRequest(object):  
    318318            self._mark_post_parse_error()
    319319            return
    320320
    321         if self.META.get('CONTENT_TYPE', '').startswith('multipart'):
     321        if self.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'):
    322322            if hasattr(self, '_body'):
    323323                # Use already read data
    324324                data = BytesIO(self._body)
    class HttpRequest(object):  
    336336                # empty POST
    337337                self._mark_post_parse_error()
    338338                raise
    339         else:
     339        elif self.META.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
    340340            self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
     341        else:
     342            self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()
    341343
    342344    ## File-like and iterator interface.
    343345    ##
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index 90872a6..643dc1a 100644
    a b All attributes should be considered read-only, unless stated otherwise below.  
    9292
    9393.. attribute:: HttpRequest.POST
    9494
    95     A dictionary-like object containing all given HTTP POST parameters. See the
     95    A dictionary-like object containing all given HTTP POST parameters,
     96    providing that the request contains form data. See the
    9697    :class:`QueryDict` documentation below.
    9798
     99    .. versionchanged:: 1.5
     100        Before Django 1.5, HttpRequest.POST did also contain non-form data. Now if
     101        the ``Content-Type`` header is not ``multipart/form-data`` or
     102        ``application/x-www-form-urlencoded``, you should access the posted data
     103        through :attr:`HttpRequest.body` instead.
     104
    98105    It's possible that a request can come in via POST with an empty ``POST``
    99106    dictionary -- if, say, a form is requested via the POST HTTP method but
    100107    does not include form data. Therefore, you shouldn't use ``if request.POST``
  • docs/releases/1.5.txt

    diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt
    index d49bae8..b60761d 100644
    a b For consistency with the design of the other generic views,  
    245245dictionary into the context, instead passing the variables from the URLconf
    246246directly into the context.
    247247
     248Non-form data in HTTP requests
     249~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     250
     251:attr:`request.POST <django.http.HttpRequest.POST>` will no longer include data
     252posted via HTTP requests with non form-specific content-types in the header.
     253In prior versions, data posted with content-types other than
     254``multipart/form-data`` or ``application/x-www-form-urlencoded`` would still
     255end up represented in the :attr:`request.POST <django.http.HttpRequest.POST>`
     256attribute. Developers wishing to access the raw POST data for these cases,
     257should use the :attr:`request.body <django.http.HttpRequest.body>` attribute
     258instead.
     259
     260For requests without a ``Content-Type`` header, this conforms to the HTTP
     261specification (:rfc:`2616`) which states that: *If the media type remains
     262unknown, the recipient SHOULD treat it as type "application/octet-stream"*.
     263
    248264OPTIONS, PUT and DELETE requests in the test client
    249265~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    250266
  • tests/regressiontests/requests/tests.py

    diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
    index f9e1112..fc26844 100644
    a b class RequestsTests(unittest.TestCase):  
    265265    def test_stream(self):
    266266        payload = b'name=value'
    267267        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     268                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    268269                               'CONTENT_LENGTH': len(payload),
    269270                               'wsgi.input': BytesIO(payload)})
    270271        self.assertEqual(request.read(), b'name=value')
    class RequestsTests(unittest.TestCase):  
    276277        """
    277278        payload = b'name=value'
    278279        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     280                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    279281                               'CONTENT_LENGTH': len(payload),
    280282                               'wsgi.input': BytesIO(payload)})
    281283        self.assertEqual(request.POST, {'name': ['value']})
    class RequestsTests(unittest.TestCase):  
    289291        """
    290292        payload = b'name=value'
    291293        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     294                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    292295                               'CONTENT_LENGTH': len(payload),
    293296                               'wsgi.input': BytesIO(payload)})
    294297        self.assertEqual(request.read(2), b'na')
    class RequestsTests(unittest.TestCase):  
    337340                               'wsgi.input': BytesIO(payload)})
    338341        self.assertEqual(request.POST, {})
    339342
     343    def test_POST_binary_only(self):
     344        payload = b'\r\n\x01\x00\x00\x00ab\x00\x00\xcd\xcc,@'
     345        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     346                               'CONTENT_TYPE': 'application/octet-stream',
     347                               'CONTENT_LENGTH': len(payload),
     348                               'wsgi.input': BytesIO(payload)})
     349        self.assertEqual(request.POST, {})
     350        self.assertEqual(request.FILES, {})
     351        self.assertEqual(request.body, payload)
     352
    340353    def test_read_by_lines(self):
    341354        payload = b'name=value'
    342355        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     356                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    343357                               'CONTENT_LENGTH': len(payload),
    344358                               'wsgi.input': BytesIO(payload)})
    345359        self.assertEqual(list(request), [b'name=value'])
    class RequestsTests(unittest.TestCase):  
    350364        """
    351365        payload = b'name=value'
    352366        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     367                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    353368                               'CONTENT_LENGTH': len(payload),
    354369                               'wsgi.input': BytesIO(payload)})
    355370        raw_data = request.body
    class RequestsTests(unittest.TestCase):  
    362377        """
    363378        payload = b'name=value'
    364379        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     380                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    365381                               'CONTENT_LENGTH': len(payload),
    366382                               'wsgi.input': BytesIO(payload)})
    367383        raw_data = request.body
    class RequestsTests(unittest.TestCase):  
    414430
    415431        payload = b'name=value'
    416432        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     433                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
    417434                               'CONTENT_LENGTH': len(payload),
    418435                               'wsgi.input': ExplodingBytesIO(payload)})
    419436
Back to Top