Ticket #17277: django.unreadablepost.patch

File django.unreadablepost.patch, 2.6 KB (added by J. David Lowe, 13 years ago)
  • django/http/__init__.py

     
    33import datetime
    44import os
    55import re
     6import sys
    67import time
    78from pprint import pformat
    89from urllib import urlencode, quote
     
    175176                      unicode(cookies),
    176177                      unicode(meta)))
    177178
     179class UnreadablePostError(IOError):
     180    pass
    178181
    179182class HttpRequest(object):
    180183    """A basic HTTP request."""
     
    304307        if not hasattr(self, '_raw_post_data'):
    305308            if self._read_started:
    306309                raise Exception("You cannot access raw_post_data after reading from request's data stream")
    307             self._raw_post_data = self.read()
     310            try:
     311                self._raw_post_data = self.read()
     312            except IOError, e:
     313                raise UnreadablePostError, e, sys.exc_traceback
    308314            self._stream = StringIO(self._raw_post_data)
    309315        return self._raw_post_data
    310316    raw_post_data = property(_get_raw_post_data)
  • tests/regressiontests/requests/tests.py

     
    55from django.conf import settings
    66from django.core.handlers.modpython import ModPythonRequest
    77from django.core.handlers.wsgi import WSGIRequest, LimitedStream
    8 from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
     8from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError
    99from django.utils import unittest
    1010from django.utils.http import cookie_date
    1111
     
    410410        # Consume enough data to mess up the parsing:
    411411        self.assertEqual(request.read(13), u'--boundary\r\nC')
    412412        self.assertEqual(request.POST, {u'name': [u'value']})
     413
     414    def test_POST_connection_error(self):
     415        """
     416        If wsgi.input.read() raises an exception while trying to read() the
     417        POST, the exception should be identifiable (not a generic IOError).
     418        """
     419        class ExplodingStringIO(StringIO):
     420            def read(self, len=0):
     421                raise IOError("kaboom!")
     422
     423        payload = 'name=value'
     424        request = WSGIRequest({'REQUEST_METHOD': 'POST',
     425                               'CONTENT_LENGTH': len(payload),
     426                               'wsgi.input': ExplodingStringIO(payload)})
     427        self.assertRaises(UnreadablePostError, lambda: request.raw_post_data)
Back to Top