How to use the crossbar.webservice.base.RouterWebService function in crossbar

To help you get started, we’ve selected a few crossbar examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crossbario / crossbar / crossbar / webservice / base.py View on Github external
# now issue the forwarded request to the HTTP server that is being reverse-proxied
        clientFactory = self.proxyClientFactoryClass(
            request.method, rest, request.clientproto,
            request.getAllHeaders(), request.content.read(), request)
        self.reactor.connectTCP(self.host, self.port, clientFactory)

        # the proxy client request created ^ is taking care of actually finishing the request ..
        return NOT_DONE_YET

    def getChild(self, path, request):
        return ExtReverseProxyResource(
            self.host, self.port, self.path + b'/' + urlquote(path, safe=b"").encode('utf-8'),
            forwarded_port=self._forwarded_port, forwarded_proto=self._forwarded_proto)


class RouterWebServiceReverseWeb(RouterWebService):
    """
    Reverse Web proxy service.
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['reverseproxy'](personality, config)

        # target HTTP server to forward incoming HTTP requests to
        host = config['host']
        port = int(config.get('port', 80))
        base_path = config.get('path', '').encode('utf-8')

        # public listening port and protocol (http vs https) the crossbar
        # web transport is listening on. this might be used by the HTTP server
github crossbario / crossbar / crossbar / webservice / misc.py View on Github external
# set response headers for cross-origin requests
        #
        if self._allow_cross_origin:
            set_cross_origin_headers(request)

        # set response headers to disallow caching
        #
        if self._discourage_caching:
            request.setHeader(b'cache-control', b'no-store, no-cache, must-revalidate, max-age=0')

        self._requests_served += 1

        return self._data


class RouterWebServiceJson(RouterWebService):
    """
    JSON static value Web service.
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['json'](personality, config)

        value = config['value']

        resource = JsonResource(value)

        return RouterWebServiceJson(transport, path, config, resource)
github crossbario / crossbar / crossbar / webservice / static.py View on Github external
# need to manually set this - above explicitly enumerates constructor args
        similar_file._cache_timeout = self._cache_timeout

        return similar_file


class StaticResourceNoListing(StaticResource):
    """
    A file hierarchy resource with directory listing disabled.
    """

    def directoryListing(self):
        return self.childNotFound


class RouterWebServiceStatic(RouterWebService):
    """
    Static file serving Web service.
    """

    @staticmethod
    def create(transport, path, config):

        # get source for file serving (either a directory, or a Python package)
        #
        static_options = config.get('options', {})

        if 'directory' in config:

            static_dir = os.path.abspath(os.path.join(transport.cbdir, config['directory']))

        elif 'package' in config:
github crossbario / crossbar / crossbar / webservice / rest.py View on Github external
# add the publisher session to the router
        #
        router = transport._worker._router_session_factory._routerFactory._routers[config['realm']]
        transport._worker._router_session_factory.add(publisher_session,
                                                      router,
                                                      authrole=config.get('role', 'anonymous'))

        # now create the publisher Twisted Web resource
        #
        resource = PublisherResource(config.get('options', {}), publisher_session)

        return RouterWebServiceRestPublisher(transport, path, config, resource)


class RouterWebServiceRestCaller(RouterWebService):
    """
    HTTP/REST-to-WAMP Caller Web service (part of REST-bridge).
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['caller'](personality, config)

        # create a vanilla session: the caller will use this to inject calls
        #
        caller_session_config = ComponentConfig(realm=config['realm'], extra=None)
        caller_session = ApplicationSession(caller_session_config)

        # add the calling session to the router
        #
github crossbario / crossbar / crossbar / webservice / wsgi.py View on Github external
#
#####################################################################################

import sys
import importlib

from twisted.python.threadpool import ThreadPool
from twisted.web.wsgi import WSGIResource

from autobahn.wamp import ApplicationError
from autobahn.twisted.resource import WSGIRootResource

from crossbar.webservice.base import RouterWebService


class RouterWebServiceWsgi(RouterWebService):
    """
    WSGI application Web service.
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['wsgi'](personality, config)
        reactor = transport.worker.components_shared['reactor']

        if 'module' not in config:
            raise ApplicationError('crossbar.error.invalid_configuration', 'missing WSGI app module')

        if 'object' not in config:
            raise ApplicationError('crossbar.error.invalid_configuration', 'missing WSGI app object')
github crossbario / crossbar / crossbar / webservice / rest.py View on Github external
router = transport._worker._router_session_factory._routerFactory._routers[config['realm']]
        transport._worker._router_session_factory.add(caller_session,
                                                      router,
                                                      authrole=config.get('role', 'anonymous'))

        # now create the caller Twisted Web resource
        #
        resource = CallerResource(
            config.get('options', {}),
            caller_session
        )

        return RouterWebServiceRestCaller(transport, path, config, resource)


class RouterWebServiceWebhook(RouterWebService):
    """
    HTTP/POST Webhook service (part of REST-bridge).
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['webhook'](personality, config)

        # create a vanilla session: the webhook will use this to inject events
        #
        webhook_session_config = ComponentConfig(realm=config['realm'], extra=None)
        webhook_session = ApplicationSession(webhook_session_config)

        # add the webhook session to the router
        #
github crossbario / crossbar / crossbar / webservice / misc.py View on Github external
s = self._page.render(cbVersion=crossbar.__version__,
                              workerPid=self._pid,
                              peer=peer,
                              **node_info)

        request.write(s.encode('utf8'))
        request.finish()

    def render_GET(self, request):
        # http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html
        d = self._controller_session.call('crossbar.get_status')
        d.addCallback(self._delayedRender, request)
        return server.NOT_DONE_YET


class RouterWebServiceNodeInfo(RouterWebService):
    """
    Node information page service.
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['nodeinfo'](personality, config)

        resource = NodeInfoResource(transport.templates, transport.worker)

        return RouterWebServiceNodeInfo(transport, path, config, resource)


class JsonResource(Resource):
    """
github crossbario / crossbar / crossbar / webservice / archive.py View on Github external
# guess MIME type from file extension
            _, ext = os.path.splitext(search_path)
            content_type = self.contentTypes.get(ext, None)

            # create and return resource that returns the file contents
            res = ZipFileResource(fd, file_size, content_type)
            return res

        else:
            if self._default_file and retry:
                return self.getChild(self._default_file, request, False)
            else:
                return resource.NoResource()


class RouterWebServiceArchive(RouterWebService):
    """
    Static Web from ZIP archive service.
    """

    @staticmethod
    def check(personality, config):
        """
        Checks the configuration item. When errors are found, an
        InvalidConfigException exception is raised.

        :param personality: The node personality class.
        :param config: The Web service configuration item.

        :raises: crossbar.common.checkconfig.InvalidConfigException
        """
        if 'type' not in config:
github crossbario / crossbar / crossbar / webservice / base.py View on Github external
Redirecting Twisted Web resource.
    """

    isLeaf = True

    def __init__(self, redirect_url):
        Resource.__init__(self)
        self._redirect_url = redirect_url

    def render_GET(self, request):
        request.redirect(self._redirect_url)
        request.finish()
        return server.NOT_DONE_YET


class RouterWebServiceRedirect(RouterWebService):
    """
    Redirecting Web service.
    """

    @staticmethod
    def create(transport, path, config):
        personality = transport.worker.personality
        personality.WEB_SERVICE_CHECKERS['redirect'](personality, config)

        redirect_url = config['url'].encode('ascii', 'ignore')
        resource = RedirectResource(redirect_url)

        return RouterWebServiceRedirect(transport, path, config, resource)


class RouterWebServiceTwistedWeb(RouterWebService):
github crossbario / crossbar / crossbar / webservice / wap.py View on Github external
except NotFound:
            request.setResponseCode(404)
            return self._render_error('Path "{full_path}" not found [werkzeug.routing.MapAdapter.match]'.format(full_path=full_path), request)

        except MethodNotAllowed:
            request.setResponseCode(511)
            return self._render_error('Method not allowed on path "{full_path}" [werkzeug.routing.MapAdapter.match]'.format(full_path=full_path), request)

        except Exception:
            request.setResponseCode(500)
            request.write(self._render_error('Unknown error with path "{full_path}" [werkzeug.routing.MapAdapter.match]'.format(full_path=full_path), request))
            raise


class RouterWebServiceWap(RouterWebService):
    """
    WAMP Application Page service.
    """

    @staticmethod
    def check(personality, config):
        """
        Checks the configuration item. When errors are found, an
        InvalidConfigException exception is raised.

        :param personality: The node personality class.
        :param config: The Web service configuration item.
        :raises: crossbar.common.checkconfig.InvalidConfigException
        """
        if 'type' not in config:
            raise InvalidConfigException("missing mandatory attribute 'type' in Web service configuration")