Ticket #17277: django.unreadablepost.patch.2

File django.unreadablepost.patch.2, 2.5 KB (added by J. David Lowe, 13 years ago)
Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py (revision 17492)
4+++ django/http/__init__.py (working copy)
5@@ -3,6 +3,7 @@
6 import datetime
7 import os
8 import re
9+import sys
10 import time
11 import warnings
12
13@@ -177,6 +178,8 @@
14 unicode(cookies),
15 unicode(meta)))
16
17+class UnreadablePostError(IOError):
18+ pass
19
20 class HttpRequest(object):
21 """A basic HTTP request."""
22@@ -321,7 +324,10 @@
23 if not hasattr(self, '_body'):
24 if self._read_started:
25 raise Exception("You cannot access body after reading from request's data stream")
26- self._body = self.read()
27+ try:
28+ self._body = self.read()
29+ except IOError, e:
30+ raise UnreadablePostError, e, sys.exc_traceback
31 self._stream = StringIO(self._body)
32 return self._body
33
34Index: tests/regressiontests/requests/tests.py
35===================================================================
36--- tests/regressiontests/requests/tests.py (revision 17492)
37+++ tests/regressiontests/requests/tests.py (working copy)
38@@ -6,7 +6,7 @@
39 from django.conf import settings
40 from django.core.handlers.modpython import ModPythonRequest
41 from django.core.handlers.wsgi import WSGIRequest, LimitedStream
42-from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
43+from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError
44 from django.test.utils import get_warnings_state, restore_warnings_state
45 from django.utils import unittest
46 from django.utils.http import cookie_date
47@@ -430,3 +430,19 @@
48 self.assertEqual(request.body, request.raw_post_data)
49 finally:
50 restore_warnings_state(warnings_state)
51+
52+
53+ def test_POST_connection_error(self):
54+ """
55+ If wsgi.input.read() raises an exception while trying to read() the
56+ POST, the exception should be identifiable (not a generic IOError).
57+ """
58+ class ExplodingStringIO(StringIO):
59+ def read(self, len=0):
60+ raise IOError("kaboom!")
61+
62+ payload = 'name=value'
63+ request = WSGIRequest({'REQUEST_METHOD': 'POST',
64+ 'CONTENT_LENGTH': len(payload),
65+ 'wsgi.input': ExplodingStringIO(payload)})
66+ self.assertRaises(UnreadablePostError, lambda: request.raw_post_data)
Back to Top