Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17277 - Wrap IOErrors raised due to client disconnect in a spe…

…cific IOError subclass so they can be distinguished from more serious errors. Thanks David Lowe.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17493 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0ce6636102d9e1c8c158c0e3eadb5a5db6b06a71 1 parent 8be356e
Carl Meyer authored February 10, 2012
8  django/http/__init__.py
@@ -3,6 +3,7 @@
3 3
 import datetime
4 4
 import os
5 5
 import re
  6
+import sys
6 7
 import time
7 8
 import warnings
8 9
 
@@ -177,6 +178,8 @@ def build_request_repr(request, path_override=None, GET_override=None,
177 178
                       unicode(cookies),
178 179
                       unicode(meta)))
179 180
 
  181
+class UnreadablePostError(IOError):
  182
+    pass
180 183
 
181 184
 class HttpRequest(object):
182 185
     """A basic HTTP request."""
@@ -321,7 +324,10 @@ def body(self):
321 324
         if not hasattr(self, '_body'):
322 325
             if self._read_started:
323 326
                 raise Exception("You cannot access body after reading from request's data stream")
324  
-            self._body = self.read()
  327
+            try:
  328
+                self._body = self.read()
  329
+            except IOError, e:
  330
+                raise UnreadablePostError, e, sys.exc_traceback
325 331
             self._stream = StringIO(self._body)
326 332
         return self._body
327 333
 
22  tests/regressiontests/requests/tests.py
... ...
@@ -1,3 +1,5 @@
  1
+from __future__ import with_statement
  2
+
1 3
 import time
2 4
 import warnings
3 5
 from datetime import datetime, timedelta
@@ -6,7 +8,7 @@
6 8
 from django.conf import settings
7 9
 from django.core.handlers.modpython import ModPythonRequest
8 10
 from django.core.handlers.wsgi import WSGIRequest, LimitedStream
9  
-from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
  11
+from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError
10 12
 from django.test.utils import get_warnings_state, restore_warnings_state
11 13
 from django.utils import unittest
12 14
 from django.utils.http import cookie_date
@@ -430,3 +432,21 @@ def test_raw_post_data_returns_body(self):
430 432
             self.assertEqual(request.body, request.raw_post_data)
431 433
         finally:
432 434
             restore_warnings_state(warnings_state)
  435
+
  436
+
  437
+    def test_POST_connection_error(self):
  438
+        """
  439
+        If wsgi.input.read() raises an exception while trying to read() the
  440
+        POST, the exception should be identifiable (not a generic IOError).
  441
+        """
  442
+        class ExplodingStringIO(StringIO):
  443
+            def read(self, len=0):
  444
+                raise IOError("kaboom!")
  445
+
  446
+        payload = 'name=value'
  447
+        request = WSGIRequest({'REQUEST_METHOD': 'POST',
  448
+                               'CONTENT_LENGTH': len(payload),
  449
+                               'wsgi.input': ExplodingStringIO(payload)})
  450
+
  451
+        with self.assertRaises(UnreadablePostError):
  452
+            request.raw_post_data

0 notes on commit 0ce6636

Please sign in to comment.
Something went wrong with that request. Please try again.