def requestReceived(self, command, path, version):
        from twisted.web.http import parse_qs
        if self.do_log:
            print '%f Request Received' % time.time()
        self.content.seek(0,0)
        self.args = {}
        self.stack = []

        self.method, self.uri = command, path
        self.clientproto = version
        x = self.uri.split(b'?', 1)

        if len(x) == 1:
            self.path = self.uri
        else:
            self.path, argstring = x
            self.args = parse_qs(argstring, 1)

        # cache the client and server information, we'll need this later to be
        # serialized and sent with the request so CGIs will work remotely
        self.client = self.channel.transport.getPeer()
        self.host = self.channel.transport.getHost()

        # Argument processing
        args = self.args
        ctype = self.requestHeaders.getRawHeaders(b'content-type')
        if ctype is not None:
            ctype = ctype[0]

        if self.method == b"POST" and ctype:
            mfd = b'multipart/form-data'
            key, pdict = _parseHeader(ctype)
            if key == b'application/x-www-form-urlencoded':
                args.update(parse_qs(self.content.read(), 1))
            elif key == mfd:
                try:
                    self.content.seek(0,0)
                    args.update(self.parse_multipart(self.content, pdict))
                    #args.update(cgi.parse_multipart(self.content, pdict))

                except KeyError as e:
                    if e.args[0] == b'content-disposition':
                        # Parse_multipart can't cope with missing
                        # content-dispostion headers in multipart/form-data
                        # parts, so we catch the exception and tell the client
                        # it was a bad request.
                        self.channel.transport.write(
                                b"HTTP/1.1 400 Bad Request\r\n\r\n")
                        self.channel.transport.loseConnection()
                        return
                    raise
            self.content.seek(0, 0)

        self.process()
Beispiel #2
0
def requestMock(path, method="GET", host="localhost", port=8080, isSecure=False,
                body=None, headers=None):
    if not headers:
        headers = {}

    if not body:
        body = ''

    request = server.Request(DummyChannel(), False)
    request.gotLength(len(body))
    request.content = StringIO()
    request.content.write(body)
    request.content.seek(0,0)
    request.requestHeaders = Headers(headers)
    request.setHost(host, port, isSecure)
    request.uri = path
    request.args = {}
    request.prepath = []
    request.postpath = path.split('/')
    request.method = method
    request.clientproto = 'HTTP/1.1'

    request.setResponseCode = Mock(wraps=request.setResponseCode)
    request.finish = Mock(wraps=request.finish)
    request.write = Mock(wraps=request.write)

    def registerProducer(producer, streaming):
        # This is a terrible terrible hack.
        producer.resumeProducing()
        producer.resumeProducing()

    request.registerProducer = registerProducer
    request.unregisterProducer = Mock()

    # copied from twisted.web.http.Request.requestReceived
    args = request.args
    ctype = request.requestHeaders.getRawHeaders(b'content-type')
    if ctype is not None:
        ctype = ctype[0]

    if request.method == b"POST" and ctype:
        mfd = b'multipart/form-data'
        key, pdict = _parseHeader(ctype)
        if key == b'application/x-www-form-urlencoded':
            args.update(parse_qs(request.content.read(), 1))
        elif key == mfd:
            try:
                args.update(cgi.parse_multipart(request.content, pdict))
            except KeyError as e:
                if e.args[0] == b'content-disposition':
                    # Parse_multipart can't cope with missing
                    # content-dispostion headers in multipart/form-data
                    # parts, so we catch the exception and tell the client
                    # it was a bad request.
                    request.channel.transport.write(
                            b"HTTP/1.1 400 Bad Request\r\n\r\n")
                    request.channel.transport.loseConnection()
                    return
                raise
        request.content.seek(0, 0)

    return request