Ticket #16178: 16178.request_repr.diff

File 16178.request_repr.diff, 11.4 KB (added by Julien Phalip, 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):  
    4646        self._read_started = False
    4747
    4848    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)
    7350
    7451    def get_full_path(self):
    7552        # 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):  
    158158        self._read_started = False
    159159
    160160    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)
    184162
    185163    def get_full_path(self):
    186164        # 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):  
    135135
    136136RAISE_ERROR = object()
    137137
     138
     139
     140
     141def 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
    138177class HttpRequest(object):
    139178    """A basic HTTP request."""
    140179
    class HttpRequest(object):  
    147186        self.path = ''
    148187        self.path_info = ''
    149188        self.method = None
     189        self._post_parse_error = False
    150190
    151191    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)
    155193
    156194    def get_host(self):
    157195        """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  
    77
    88from django.conf import settings
    99from django.http import (HttpResponse, HttpResponseServerError,
    10     HttpResponseNotFound, HttpRequest)
     10    HttpResponseNotFound, HttpRequest, build_request_repr)
    1111from django.template import (Template, Context, TemplateDoesNotExist,
    1212    TemplateSyntaxError)
    1313from django.template.defaultfilters import force_escape, pprint
    class ExceptionReporterFilter(object):  
    9696        if request is None:
    9797            return repr(None)
    9898        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))
    127100
    128101    def get_post_parameters(self, request):
    129102        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  
    44
    55from django.core.handlers.modpython import ModPythonRequest
    66from django.core.handlers.wsgi import WSGIRequest, LimitedStream
    7 from django.http import HttpRequest, HttpResponse, parse_cookie
     7from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
    88from django.utils import unittest
    99from django.utils.http import cookie_date
    1010
    class RequestsTests(unittest.TestCase):  
    1616        self.assertEqual(request.COOKIES.keys(), [])
    1717        self.assertEqual(request.META.keys(), [])
    1818
     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
    1931    def test_wsgirequest(self):
    2032        request = WSGIRequest({'PATH_INFO': 'bogus', 'REQUEST_METHOD': 'bogus', 'wsgi.input': StringIO('')})
    2133        self.assertEqual(request.GET.keys(), [])
    class RequestsTests(unittest.TestCase):  
    2638        self.assertEqual(request.META['REQUEST_METHOD'], 'bogus')
    2739        self.assertEqual(request.META['SCRIPT_NAME'], '')
    2840
     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
    2952    def test_modpythonrequest(self):
    3053        class FakeModPythonRequest(ModPythonRequest):
    3154           def __init__(self, *args, **kwargs):
    class RequestsTests(unittest.TestCase):  
    4568        self.assertEqual(request.COOKIES.keys(), [])
    4669        self.assertEqual(request.META.keys(), [])
    4770
     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
    4887    def test_parse_cookie(self):
    4988        self.assertEqual(parse_cookie('invalid:key=true'), {})
    5089
Back to Top