Exemple #1
0
    def __call__(self, request):
        """Parse the JSON body and dispatch."""

        if request.method != 'POST':
            raise HTTPMethodNotAllowed("POST required.")

        try:
            json = loads(request.body)
        except ValueError:
            raise HTTPBadRequest("Unable to parse JSON request.")

        for key in ('method', 'id'):
            if key not in json:
                raise HTTPBadRequest("Missing required JSON-RPC value: " + key)

        method = json['method']
        args = json.get('params', [])
        id_ = json['id']

        if not isinstance(args, list):
            raise HTTPBadRequest("Bad parameters, must be a list: {0!r}".format(args))

        log.debug("JSON-RPC Call: %s%r", method, args)

        func, parent = route(self, method, JSONRPCController)

        log.debug("Found method: %r", func)

        try:
            callback = getattr(parent, '__before__', None)
            if callback:
                args = callback(*args)

            result = func(*args)
            error = None

            callback = getattr(parent, '__after__', None)
            if callback:
                result = callback(result, *args)
        except:
            log.exception("Error calling RPC mechanism.")
            exc = exception()

            web.core.response.status_int = 500

            result = None
            error = dict(
                    name='JSONRPCError',
                    code=100,
                    message=str(exc.exception),
                    error="Not disclosed." if not web.core.config.get('debug', False) else exc.formatted
                )
        else:
            log.debug("Got result: %r", result)

        if id_ is not None:
            web.core.response.content_type = 'application/json'
            return 'json:', dict(result=result, error=error, id=id_)
Exemple #2
0
    def __call__(self, request):
        """Parse an XML-RPC body and dispatch."""

        length = int(request.headers.get('Content-Length', 0))

        if not length:
            log.debug("No content length specified, returning 411 HTTPLengthRequired error.")
            raise HTTPLengthRequired()

        if not length or length > self.__max_body_length__:
            log.debug("Content length larger than allowed maximum of %d bytes, "
                      "returning 413 HTTPRequestEntityTooLarge error.",
                      self.__max_body_length__)
            raise HTTPRequestEntityTooLarge("XML body too large.")

        try:
            args, method = xmlrpclib.loads(request.body, True)
        except:
            return self._fault(fault.parse.badly_formed)

        log.debug("XML-RPC Call: %s%r", method, args)

        try:
            func, parent = route(self, method, XMLRPCController)
        except RoutingError:
            return self._fault(fault.server.notfound)

        log.debug("Found method: %r", func)

        cargs, cdefaults, unlimited = getargspec(func)[:3]
        cargs = len(cargs)
        cdefaults = len(cdefaults)
        nargs = len(args)

        if nargs < cargs - cdefaults or (not unlimited and nargs > cargs):
            return self._fault(fault.server.params)

        try:
            callback = getattr(parent, '__before__', None)
            if callback:
                args = parent.__before__(*args)

            result = func(*args)

            callback = getattr(parent, '__after__', None)
            if callback:
                result = parent.__after__(result, *args)
        except HTTPException:
            raise
        except:
            log.error("Error calling RPC mechanism.", exc_info=True)
            return self._fault(fault.other.application)

        return self._response((result,))
Exemple #3
0
    def __call__(self, request):
        pyamf_request = pyamf.remoting.decode(request.body)
        pyamf_response = pyamf.remoting.Envelope(pyamf_request.amfVersion)

        for name, message in pyamf_request:
            # Dynamically build mapping.
            # This introduces a performance hit on the first request of each method.
            if message.target not in self._gateway.services:
                fn, parent = route(self, message.target, AMFController)
                self._gateway.addService(self._call(fn, parent), message.target)

            pyamf_response[name] = self._gateway.getProcessor(message)(message)

        web.core.response.headers['Content-Type'] = pyamf.remoting.CONTENT_TYPE

        return pyamf.remoting.encode(pyamf_response).getvalue()
Exemple #4
0
 def __call__(self, request):
     """Parse an XML-RPC body and dispatch."""
     
     length = int(request.headers.get('Content-Length', 0))
     
     if not length:
         log.debug("No content length specified, returning 411 HTTPLengthRequired error.")
         raise HTTPLengthRequired()
     
     if not length or length > self.__max_body_length__:
         log.debug("Content length larger than allowed maximum of %d bytes, "
                   "returning 413 HTTPRequestEntityTooLarge error.",
                   self.__max_body_length__)
         raise HTTPRequestEntityTooLarge("XML body too large.")
     
     args, method = xmlrpclib.loads(request.body, True)
     
     log.debug("XML-RPC Call: %s%r", method, args)
     
     func, parent = route(self, method, XMLRPCController)
     
     log.debug("Found method: %r", func)
     
     try:
         args = parent.__before__(*args)
     
     except AttributeError:
         pass
     
     result = func(*args)
     
     try:
         result = parent.__after__(result, *args)
     
     except AttributeError:
         pass
     
     log.debug("Got result: %r", result)
     
     response.content_type = 'text/xml'
     return xmlrpclib.dumps((result,), methodresponse=True,
                            allow_none=self.__allow_none__)
Exemple #5
0
 def test_context(self):
     route(Foo(), "deep2.foo", Foo)
Exemple #6
0
 def test_early_terminate(self):
     route(Foo, "normal.foo", Dialect)
Exemple #7
0
 def test_deep(self):
     route(Foo, "deep.normal", Dialect)
Exemple #8
0
 def test_deep_failure(self):
     route(Foo, "deep._private", Dialect)
Exemple #9
0
 def test_bad3(self):
     route(Foo, "string.foo", Dialect)
Exemple #10
0
 def test_bad2(self):
     route(Foo, "string", Dialect)
Exemple #11
0
 def test_bad(self):
     route(Foo, "bad", Dialect)
Exemple #12
0
 def test_private(self):
     route(Foo, "_private", Dialect)
Exemple #13
0
 def test_basic(self):
     self.assertEqual(route(Foo, "normal", Dialect)[0], Foo.normal)