Ticket #17277: django.unreadablepost.patch
File django.unreadablepost.patch, 2.6 KB (added by , 13 years ago) |
---|
-
django/http/__init__.py
3 3 import datetime 4 4 import os 5 5 import re 6 import sys 6 7 import time 7 8 from pprint import pformat 8 9 from urllib import urlencode, quote … … 175 176 unicode(cookies), 176 177 unicode(meta))) 177 178 179 class UnreadablePostError(IOError): 180 pass 178 181 179 182 class HttpRequest(object): 180 183 """A basic HTTP request.""" … … 304 307 if not hasattr(self, '_raw_post_data'): 305 308 if self._read_started: 306 309 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 308 314 self._stream = StringIO(self._raw_post_data) 309 315 return self._raw_post_data 310 316 raw_post_data = property(_get_raw_post_data) -
tests/regressiontests/requests/tests.py
5 5 from django.conf import settings 6 6 from django.core.handlers.modpython import ModPythonRequest 7 7 from django.core.handlers.wsgi import WSGIRequest, LimitedStream 8 from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr 8 from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError 9 9 from django.utils import unittest 10 10 from django.utils.http import cookie_date 11 11 … … 410 410 # Consume enough data to mess up the parsing: 411 411 self.assertEqual(request.read(13), u'--boundary\r\nC') 412 412 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)