Ticket #16178: 16178.request_repr.diff
File 16178.request_repr.diff, 11.4 KB (added by , 13 years ago) |
---|
-
django/core/handlers/modpython.py
diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py index f0c7701..d793ed7 100644
a b class ModPythonRequest(http.HttpRequest): 46 46 self._read_started = False 47 47 48 48 def __repr__(self): 49 # Since this is called as part of error handling, we need to be very 50 # robust against potentially malformed input. 51 try: 52 get = pformat(self.GET) 53 except: 54 get = '<could not parse>' 55 if self._post_parse_error: 56 post = '<could not parse>' 57 else: 58 try: 59 post = pformat(self.POST) 60 except: 61 post = '<could not parse>' 62 try: 63 cookies = pformat(self.COOKIES) 64 except: 65 cookies = '<could not parse>' 66 try: 67 meta = pformat(self.META) 68 except: 69 meta = '<could not parse>' 70 return smart_str(u'<ModPythonRequest\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % 71 (self.path, unicode(get), unicode(post), 72 unicode(cookies), unicode(meta))) 49 return http.build_request_repr(self) 73 50 74 51 def get_full_path(self): 75 52 # RFC 3986 requires self._req.args to be in the ASCII range, but this -
django/core/handlers/wsgi.py
diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 434f91c..56f632b 100644
a b class WSGIRequest(http.HttpRequest): 158 158 self._read_started = False 159 159 160 160 def __repr__(self): 161 # Since this is called as part of error handling, we need to be very 162 # robust against potentially malformed input. 163 try: 164 get = pformat(self.GET) 165 except: 166 get = '<could not parse>' 167 if self._post_parse_error: 168 post = '<could not parse>' 169 else: 170 try: 171 post = pformat(self.POST) 172 except: 173 post = '<could not parse>' 174 try: 175 cookies = pformat(self.COOKIES) 176 except: 177 cookies = '<could not parse>' 178 try: 179 meta = pformat(self.META) 180 except: 181 meta = '<could not parse>' 182 return '<WSGIRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 183 (get, post, cookies, meta) 161 return http.build_request_repr(self) 184 162 185 163 def get_full_path(self): 186 164 # RFC 3986 requires query string arguments to be in the ASCII range. -
django/http/__init__.py
diff --git a/django/http/__init__.py b/django/http/__init__.py index ae507aa..706ef4b 100644
a b class Http404(Exception): 135 135 136 136 RAISE_ERROR = object() 137 137 138 139 140 141 def build_request_repr(request, path_override=None, GET_override=None, POST_override=None, COOKIES_override=None, META_override=None): 142 """ 143 Builds and returns the request's representation string. The request's 144 attributes may be overridden by pre-processed values. 145 """ 146 # Since this is called as part of error handling, we need to be very 147 # robust against potentially malformed input. 148 try: 149 get = pformat(GET_override) if GET_override is not None else pformat(request.GET) 150 except: 151 get = '<could not parse>' 152 if request._post_parse_error: 153 post = '<could not parse>' 154 else: 155 try: 156 post = pformat(POST_override) if POST_override is not None else pformat(request.POST) 157 except: 158 post = '<could not parse>' 159 try: 160 cookies = pformat(COOKIES_override) if COOKIES_override is not None else pformat(request.COOKIES) 161 except: 162 cookies = '<could not parse>' 163 try: 164 meta = pformat(META_override) if META_override is not None else pformat(request.META) 165 except: 166 meta = '<could not parse>' 167 path = path_override if path_override is not None else request.path 168 return smart_str(u'<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % 169 (request.__class__.__name__, 170 path, 171 unicode(get), 172 unicode(post), 173 unicode(cookies), 174 unicode(meta))) 175 176 138 177 class HttpRequest(object): 139 178 """A basic HTTP request.""" 140 179 … … class HttpRequest(object): 147 186 self.path = '' 148 187 self.path_info = '' 149 188 self.method = None 189 self._post_parse_error = False 150 190 151 191 def __repr__(self): 152 return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 153 (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES), 154 pformat(self.META)) 192 return build_request_repr(self) 155 193 156 194 def get_host(self): 157 195 """Returns the HTTP host using the environment or request headers.""" -
django/views/debug.py
diff --git a/django/views/debug.py b/django/views/debug.py index c607f43..d2e4c9c 100644
a b from pprint import pformat 7 7 8 8 from django.conf import settings 9 9 from django.http import (HttpResponse, HttpResponseServerError, 10 HttpResponseNotFound, HttpRequest )10 HttpResponseNotFound, HttpRequest, build_request_repr) 11 11 from django.template import (Template, Context, TemplateDoesNotExist, 12 12 TemplateSyntaxError) 13 13 from django.template.defaultfilters import force_escape, pprint … … class ExceptionReporterFilter(object): 96 96 if request is None: 97 97 return repr(None) 98 98 else: 99 # Since this is called as part of error handling, we need to be very 100 # robust against potentially malformed input. 101 try: 102 get = pformat(request.GET) 103 except: 104 get = '<could not parse>' 105 if request._post_parse_error: 106 post = '<could not parse>' 107 else: 108 try: 109 post = pformat(self.get_post_parameters(request)) 110 except: 111 post = '<could not parse>' 112 try: 113 cookies = pformat(request.COOKIES) 114 except: 115 cookies = '<could not parse>' 116 try: 117 meta = pformat(request.META) 118 except: 119 meta = '<could not parse>' 120 return smart_str(u'<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % 121 (request.__class__.__name__, 122 request.path, 123 unicode(get), 124 unicode(post), 125 unicode(cookies), 126 unicode(meta))) 99 return build_request_repr(request, POST_override=self.get_post_parameters(request)) 127 100 128 101 def get_post_parameters(self, request): 129 102 if request is None: -
tests/regressiontests/requests/tests.py
diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py index b682014..703e387 100644
a b from StringIO import StringIO 4 4 5 5 from django.core.handlers.modpython import ModPythonRequest 6 6 from django.core.handlers.wsgi import WSGIRequest, LimitedStream 7 from django.http import HttpRequest, HttpResponse, parse_cookie 7 from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr 8 8 from django.utils import unittest 9 9 from django.utils.http import cookie_date 10 10 … … class RequestsTests(unittest.TestCase): 16 16 self.assertEqual(request.COOKIES.keys(), []) 17 17 self.assertEqual(request.META.keys(), []) 18 18 19 def test_httprequest_repr(self): 20 request = HttpRequest() 21 request.path = u'/somepath/' 22 request.GET = {u'get-key': u'get-value'} 23 request.POST = {u'post-key': u'post-value'} 24 request.COOKIES = {u'post-key': u'post-value'} 25 request.META = {u'post-key': u'post-value'} 26 self.assertEqual(repr(request), u"<HttpRequest\npath:/somepath/,\nGET:{u'get-key': u'get-value'},\nPOST:{u'post-key': u'post-value'},\nCOOKIES:{u'post-key': u'post-value'},\nMETA:{u'post-key': u'post-value'}>") 27 self.assertEqual(build_request_repr(request), repr(request)) 28 self.assertEqual(build_request_repr(request, path_override='/otherpath/', GET_override={u'a': u'b'}, POST_override={u'c': u'd'}, COOKIES_override={u'e': u'f'}, META_override={u'g': u'h'}), 29 u"<HttpRequest\npath:/otherpath/,\nGET:{u'a': u'b'},\nPOST:{u'c': u'd'},\nCOOKIES:{u'e': u'f'},\nMETA:{u'g': u'h'}>") 30 19 31 def test_wsgirequest(self): 20 32 request = WSGIRequest({'PATH_INFO': 'bogus', 'REQUEST_METHOD': 'bogus', 'wsgi.input': StringIO('')}) 21 33 self.assertEqual(request.GET.keys(), []) … … class RequestsTests(unittest.TestCase): 26 38 self.assertEqual(request.META['REQUEST_METHOD'], 'bogus') 27 39 self.assertEqual(request.META['SCRIPT_NAME'], '') 28 40 41 def test_wsgirequest_repr(self): 42 request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': StringIO('')}) 43 request.GET = {u'get-key': u'get-value'} 44 request.POST = {u'post-key': u'post-value'} 45 request.COOKIES = {u'post-key': u'post-value'} 46 request.META = {u'post-key': u'post-value'} 47 self.assertEqual(repr(request), u"<WSGIRequest\npath:/somepath/,\nGET:{u'get-key': u'get-value'},\nPOST:{u'post-key': u'post-value'},\nCOOKIES:{u'post-key': u'post-value'},\nMETA:{u'post-key': u'post-value'}>") 48 self.assertEqual(build_request_repr(request), repr(request)) 49 self.assertEqual(build_request_repr(request, path_override='/otherpath/', GET_override={u'a': u'b'}, POST_override={u'c': u'd'}, COOKIES_override={u'e': u'f'}, META_override={u'g': u'h'}), 50 u"<WSGIRequest\npath:/otherpath/,\nGET:{u'a': u'b'},\nPOST:{u'c': u'd'},\nCOOKIES:{u'e': u'f'},\nMETA:{u'g': u'h'}>") 51 29 52 def test_modpythonrequest(self): 30 53 class FakeModPythonRequest(ModPythonRequest): 31 54 def __init__(self, *args, **kwargs): … … class RequestsTests(unittest.TestCase): 45 68 self.assertEqual(request.COOKIES.keys(), []) 46 69 self.assertEqual(request.META.keys(), []) 47 70 71 def test_modpythonrequest_repr(self): 72 class Dummy: 73 def get_options(self): 74 return {} 75 req = Dummy() 76 req.uri = '/somepath/' 77 request = ModPythonRequest(req) 78 request._get = {u'get-key': u'get-value'} 79 request._post = {u'post-key': u'post-value'} 80 request._cookies = {u'post-key': u'post-value'} 81 request._meta = {u'post-key': u'post-value'} 82 self.assertEqual(repr(request), u"<ModPythonRequest\npath:/somepath/,\nGET:{u'get-key': u'get-value'},\nPOST:{u'post-key': u'post-value'},\nCOOKIES:{u'post-key': u'post-value'},\nMETA:{u'post-key': u'post-value'}>") 83 self.assertEqual(build_request_repr(request), repr(request)) 84 self.assertEqual(build_request_repr(request, path_override='/otherpath/', GET_override={u'a': u'b'}, POST_override={u'c': u'd'}, COOKIES_override={u'e': u'f'}, META_override={u'g': u'h'}), 85 u"<ModPythonRequest\npath:/otherpath/,\nGET:{u'a': u'b'},\nPOST:{u'c': u'd'},\nCOOKIES:{u'e': u'f'},\nMETA:{u'g': u'h'}>") 86 48 87 def test_parse_cookie(self): 49 88 self.assertEqual(parse_cookie('invalid:key=true'), {}) 50 89