コード例 #1
0
ファイル: jsonrpc.py プロジェクト: Hazer/WebCore
    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_)
コード例 #2
0
ファイル: xml.py プロジェクト: Hazer/WebCore
    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,))
コード例 #3
0
ファイル: amf.py プロジェクト: Hazer/WebCore
    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()
コード例 #4
0
ファイル: xml.py プロジェクト: enfoundry/WebCore
 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__)
コード例 #5
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_context(self):
     route(Foo(), "deep2.foo", Foo)
コード例 #6
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_early_terminate(self):
     route(Foo, "normal.foo", Dialect)
コード例 #7
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_deep(self):
     route(Foo, "deep.normal", Dialect)
コード例 #8
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_deep_failure(self):
     route(Foo, "deep._private", Dialect)
コード例 #9
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_bad3(self):
     route(Foo, "string.foo", Dialect)
コード例 #10
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_bad2(self):
     route(Foo, "string", Dialect)
コード例 #11
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_bad(self):
     route(Foo, "bad", Dialect)
コード例 #12
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_private(self):
     route(Foo, "_private", Dialect)
コード例 #13
0
ファイル: test_rpc_routing.py プロジェクト: dsx/WebCore
 def test_basic(self):
     self.assertEqual(route(Foo, "normal", Dialect)[0], Foo.normal)