Ticket #288: http_request_args_property.diff

File http_request_args_property.diff, 2.6 KB (added by rikyu, 7 years ago)

a patch to http.Request that uses a property for args that parses the request body at first reference

  • twisted/web/http.py

     
    535535    sentLength = 0 # content-length of response, or total bytes sent via chunking
    536536    etag = None
    537537    lastModified = None
    538     args = None
     538    _args = None
    539539    path = None
    540540    content = None
    541541    _forceSSL = 0
     
    693693        @param version: The HTTP version of this request.
    694694        """
    695695        self.content.seek(0,0)
    696         self.args = {}
    697696        self.stack = []
    698697
    699698        self.method, self.uri = command, path
    700699        self.clientproto = version
    701700        x = self.uri.split('?', 1)
    702 
    703701        if len(x) == 1:
    704702            self.path = self.uri
    705703        else:
    706704            self.path, argstring = x
    707             self.args = parse_qs(argstring, 1)
    708705
    709706        # cache the client and server information, we'll need this later to be
    710707        # serialized and sent with the request so CGIs will work remotely
    711708        self.client = self.channel.transport.getPeer()
    712709        self.host = self.channel.transport.getHost()
     710       
     711        self.process()
    713712
     713    @property
     714    def args(self):
     715        if(self._args is not None):
     716            return self._args
     717       
     718        x = self.uri.split('?', 1)
     719        if len(x) != 1:
     720            self._args = parse_qs(x[1], 1)
     721       
    714722        # Argument processing
    715         args = self.args
     723        if(self._args is None):
     724            self._args = {}
     725        args = self._args
    716726        ctype = self.requestHeaders.getRawHeaders('content-type')
    717727        if ctype is not None:
    718728            ctype = ctype[0]
     
    737747                        return
    738748                    raise
    739749            self.content.seek(0, 0)
    740 
    741         self.process()
    742 
    743 
     750       
     751        return self._args
     752   
    744753    def __repr__(self):
    745754        return '<%s %s %s>'% (self.method, self.uri, self.clientproto)
    746755
  • twisted/web/test/test_web.py

     
    2020from twisted.python import log
    2121
    2222
    23 class DummyRequest:
     23class DummyRequest(http.Request):
    2424    """
    2525    Represents a dummy or fake request.
    2626
     
    6464        self.prepath = []
    6565        self.session = None
    6666        self.protoSession = session or server.Session(0, self)
    67         self.args = {}
     67        self._args = None
    6868        self.outgoingHeaders = {}
    6969        self.responseHeaders = http_headers.Headers()
    7070        self.responseCode = None