Пример #1
0
    def get_environ(self):
        """
        Wrapper to handles client certificates and writes it to environ.
        """

        env = WebSocketHandler.get_environ(self)
        env['SSL'] = isinstance(self.socket, gevent.ssl.SSLSocket)
        env['SSL_CLIENT_AUTH_FORCE'] = (
            aj.config.data['ssl']['client_auth']['force']
            and aj.config.data['ssl']['client_auth']['enable'])
        env['SSL_CLIENT_VALID'] = False
        env['SSL_CLIENT_USER'] = None
        if env['SSL']:
            peer_cert = self.socket.getpeercert(True)
            if peer_cert:
                certificate = crypto.load_certificate(
                    crypto.FILETYPE_PEM,
                    gevent.ssl.DER_cert_to_PEM_cert(peer_cert))
                env['SSL_CLIENT_CERTIFICATE'] = certificate
                if certificate:
                    user = ClientCertificateVerificator.get(
                        aj.context).verify(certificate)
                    env['SSL_CLIENT_VALID'] = bool(user)
                    env['SSL_CLIENT_USER'] = user
                    env['SSL_CLIENT_DIGEST'] = certificate.digest('sha256')
        return env
Пример #2
0
    def _sendall(self, data):
        """
        Wrapper to ensure utf-8 compatibility.
        """

        if isinstance(data, str):
            data = data.encode('utf-8')
        return WebSocketHandler._sendall(self, data)
Пример #3
0
 def upgrade_websocket(self):
     return WebSocketHandler.upgrade_websocket(self)
Пример #4
0
 def upgrade_connection(self):
     return WebSocketHandler.upgrade_connection(self)
Пример #5
0
 def __init__(self, socket, address, server, rfile=None):
     WebSocketHandler.__init__(self,
                               socket,
                               address,
                               server,
                               rfile=rfile)
Пример #6
0
 def upgrade_websocket(self):
     return WebSocketHandler.upgrade_websocket(self)
Пример #7
0
 def upgrade_connection(self):
     return WebSocketHandler.upgrade_connection(self)
Пример #8
0
 def __init__(self, socket, address, server, rfile=None):
     WebSocketHandler.__init__(self, socket, address, server, rfile=rfile)
Пример #9
0
 def log_request(self):
     if isinstance(self.code, (int, long)) and 400 <= self.code <= 599:
         WebSocketHandler.log_request(self)
Пример #10
0
 def log_error(self, *args, **kwargs):
     with LoggedStderr(logger, logging.DEBUG, "WSGIServer stderr"):
         WebSocketHandler.log_error(self, *args, **kwargs)
Пример #11
0
    def handle_one_response(self):
        self.status = None
        self.headers_sent = False
        self.result = None
        self.response_length = 0
        self.response_use_chunked = False

        path = self.environ.get('PATH_INFO')

        logger.info("REQUEST: %s @ %s", self.environ["SERVER_PORT"], path)
        request_method = self.environ.get("REQUEST_METHOD")
        request_tokens = self.RE_REQUEST_URL.match(path)

        # Kick non-socket.io requests to our superclass
        if not path.lstrip('/').startswith(self.server.namespace):
            return WSGIHandler.handle_one_response(self)

        # Parse request URL and QUERY_STRING and do handshake
        if request_tokens:
            request_tokens = request_tokens.groupdict()
        else:
            handshake_tokens = self.RE_HANDSHAKE_URL.match(path)

            if handshake_tokens:
                return self._do_handshake(handshake_tokens.groupdict())
            else:
                # This is no socket.io request. Let the WSGI app handle it.
                return WSGIHandler.handle_one_response(self)

        # Setup the transport and session
        transport = self.handler_types.get(request_tokens["transport_id"])
        session_id = request_tokens["session_id"]
        session = self.server.get_session(session_id)
        if session is None:
            logger.warning("Connection from dead session: %s", session_id)
            headers = [("Content-Type", "text/plain")]
            if self.server.cors_domain:
                headers += [
                    ("Access-Control-Allow-Origin", self.server.cors_domain),
                    ("Access-Control-Allow-Credentials", "true"),
                ]
            self.start_response("404 Session not found", headers)
            self.result = [""]
            self.process_result()
            return
        logger.debug("Connection for session %r, transport %r", session.session_id, transport)

        # Make the session object available for WSGI apps
        self.environ['socketio'] = protocol.PySocketProtocol(session)

        if transport is transports.WebsocketTransport:
            # fake application
            try:
                _tmp, self.application = self.application, lambda *args: None
                logger.debug("Initializing websocket.")
                WebSocketHandler.handle_one_response(self)
            finally:
                self.application = _tmp

        # Create a transport and handle the request likewise
        logger.debug("Connecting transport: %r", transport)
        transport(self).connect(session, request_method)

        if session.wsgi_app_greenlet is not None:
            if not session.wsgi_app_greenlet:
                logger.debug("Joining finalized greenlet %r -> %r", self, session.wsgi_app_greenlet)
                session.wsgi_app_greenlet.join()
                session.wsgi_app_greenlet = None

        if not session.wsgi_app_greenlet:
            start_response = lambda status, headers, exc = None: None
            logger.debug("Spawning new greenlet for session: %r", session)
            session_greenlet = SessionGreenlet.spawn(self.application, self.environ, start_response)
            session_greenlet.join()
            logger.debug("Session greenlet joined: %r", session)
            del session
Пример #12
0
 def log_request(self):
     if isinstance(self.code, (int, long)) and 400 <= self.code <= 599:
         WebSocketHandler.log_request(self)
Пример #13
0
 def __init__(self, *args, **kwargs):
     WebSocketHandler.__init__(self, *args, **kwargs)