Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
server.py480 linesDownload Raw Back to engineio
1import logging2import urllib3 4from . import base_server5from . import exceptions6from . import packet7from . import socket8 9default_logger = logging.getLogger('engineio.server')10 11 12class Server(base_server.BaseServer):13    """An Engine.IO server.14 15    This class implements a fully compliant Engine.IO web server with support16    for websocket and long-polling transports.17 18    :param async_mode: The asynchronous model to use. See the Deployment19                       section in the documentation for a description of the20                       available options. Valid async modes are "threading",21                       "eventlet", "gevent" and "gevent_uwsgi". If this22                       argument is not given, "eventlet" is tried first, then23                       "gevent_uwsgi", then "gevent", and finally "threading".24                       The first async mode that has all its dependencies25                       installed is the one that is chosen.26    :param ping_interval: The interval in seconds at which the server pings27                          the client. The default is 25 seconds. For advanced28                          control, a two element tuple can be given, where29                          the first number is the ping interval and the second30                          is a grace period added by the server.31    :param ping_timeout: The time in seconds that the client waits for the32                         server to respond before disconnecting. The default33                         is 20 seconds.34    :param max_http_buffer_size: The maximum size that is accepted for incoming35                                 messages.  The default is 1,000,000 bytes. In36                                 spite of its name, the value set in this37                                 argument is enforced for HTTP long-polling and38                                 WebSocket connections.39    :param allow_upgrades: Whether to allow transport upgrades or not. The40                           default is ``True``.41    :param http_compression: Whether to compress packages when using the42                             polling transport. The default is ``True``.43    :param compression_threshold: Only compress messages when their byte size44                                  is greater than this value. The default is45                                  1024 bytes.46    :param cookie: If set to a string, it is the name of the HTTP cookie the47                   server sends back tot he client containing the client48                   session id. If set to a dictionary, the ``'name'`` key49                   contains the cookie name and other keys define cookie50                   attributes, where the value of each attribute can be a51                   string, a callable with no arguments, or a boolean. If set52                   to ``None`` (the default), a cookie is not sent to the53                   client.54    :param cors_allowed_origins: Origin or list of origins that are allowed to55                                 connect to this server. Only the same origin56                                 is allowed by default. Set this argument to57                                 ``'*'`` to allow all origins, or to ``[]`` to58                                 disable CORS handling.59    :param cors_credentials: Whether credentials (cookies, authentication) are60                             allowed in requests to this server. The default61                             is ``True``.62    :param logger: To enable logging set to ``True`` or pass a logger object to63                   use. To disable logging set to ``False``. The default is64                   ``False``. Note that fatal errors are logged even when65                   ``logger`` is ``False``.66    :param json: An alternative json module to use for encoding and decoding67                 packets. Custom json modules must have ``dumps`` and ``loads``68                 functions that are compatible with the standard library69                 versions.70    :param async_handlers: If set to ``True``, run message event handlers in71                           non-blocking threads. To run handlers synchronously,72                           set to ``False``. The default is ``True``.73    :param monitor_clients: If set to ``True``, a background task will ensure74                            inactive clients are closed. Set to ``False`` to75                            disable the monitoring task (not recommended). The76                            default is ``True``.77    :param transports: The list of allowed transports. Valid transports78                       are ``'polling'`` and ``'websocket'``. Defaults to79                       ``['polling', 'websocket']``.80    :param kwargs: Reserved for future extensions, any additional parameters81                   given as keyword arguments will be silently ignored.82    """83    def send(self, sid, data):84        """Send a message to a client.85 86        :param sid: The session id of the recipient client.87        :param data: The data to send to the client. Data can be of type88                     ``str``, ``bytes``, ``list`` or ``dict``. If a ``list``89                     or ``dict``, the data will be serialized as JSON.90        """91        self.send_packet(sid, packet.Packet(packet.MESSAGE, data=data))92 93    def send_packet(self, sid, pkt):94        """Send a raw packet to a client.95 96        :param sid: The session id of the recipient client.97        :param pkt: The packet to send to the client.98        """99        try:100            socket = self._get_socket(sid)101        except KeyError:102            # the socket is not available103            self.logger.warning('Cannot send to sid %s', sid)104            return105        socket.send(pkt)106 107    def get_session(self, sid):108        """Return the user session for a client.109 110        :param sid: The session id of the client.111 112        The return value is a dictionary. Modifications made to this113        dictionary are not guaranteed to be preserved unless114        ``save_session()`` is called, or when the ``session`` context manager115        is used.116        """117        socket = self._get_socket(sid)118        return socket.session119 120    def save_session(self, sid, session):121        """Store the user session for a client.122 123        :param sid: The session id of the client.124        :param session: The session dictionary.125        """126        socket = self._get_socket(sid)127        socket.session = session128 129    def session(self, sid):130        """Return the user session for a client with context manager syntax.131 132        :param sid: The session id of the client.133 134        This is a context manager that returns the user session dictionary for135        the client. Any changes that are made to this dictionary inside the136        context manager block are saved back to the session. Example usage::137 138            @eio.on('connect')139            def on_connect(sid, environ):140                username = authenticate_user(environ)141                if not username:142                    return False143                with eio.session(sid) as session:144                    session['username'] = username145 146            @eio.on('message')147            def on_message(sid, msg):148                with eio.session(sid) as session:149                    print('received message from ', session['username'])150        """151        class _session_context_manager(object):152            def __init__(self, server, sid):153                self.server = server154                self.sid = sid155                self.session = None156 157            def __enter__(self):158                self.session = self.server.get_session(sid)159                return self.session160 161            def __exit__(self, *args):162                self.server.save_session(sid, self.session)163 164        return _session_context_manager(self, sid)165 166    def disconnect(self, sid=None):167        """Disconnect a client.168 169        :param sid: The session id of the client to close. If this parameter170                    is not given, then all clients are closed.171        """172        if sid is not None:173            try:174                socket = self._get_socket(sid)175            except KeyError:  # pragma: no cover176                # the socket was already closed or gone177                pass178            else:179                socket.close()180                if sid in self.sockets:  # pragma: no cover181                    del self.sockets[sid]182        else:183            for client in self.sockets.values():184                client.close()185            self.sockets = {}186 187    def handle_request(self, environ, start_response):188        """Handle an HTTP request from the client.189 190        This is the entry point of the Engine.IO application, using the same191        interface as a WSGI application. For the typical usage, this function192        is invoked by the :class:`Middleware` instance, but it can be invoked193        directly when the middleware is not used.194 195        :param environ: The WSGI environment.196        :param start_response: The WSGI ``start_response`` function.197 198        This function returns the HTTP response body to deliver to the client199        as a byte sequence.200        """201        if self.cors_allowed_origins != []:202            # Validate the origin header if present203            # This is important for WebSocket more than for HTTP, since204            # browsers only apply CORS controls to HTTP.205            origin = environ.get('HTTP_ORIGIN')206            if origin:207                allowed_origins = self._cors_allowed_origins(environ)208                if allowed_origins is not None and origin not in \209                        allowed_origins:210                    self._log_error_once(211                        origin + ' is not an accepted origin.', 'bad-origin')212                    r = self._bad_request('Not an accepted origin.')213                    start_response(r['status'], r['headers'])214                    return [r['response']]215 216        method = environ['REQUEST_METHOD']217        query = urllib.parse.parse_qs(environ.get('QUERY_STRING', ''))218        jsonp = False219        jsonp_index = None220 221        # make sure the client uses an allowed transport222        transport = query.get('transport', ['polling'])[0]223        if transport not in self.transports:224            self._log_error_once('Invalid transport', 'bad-transport')225            r = self._bad_request('Invalid transport')226            start_response(r['status'], r['headers'])227            return [r['response']]228 229        # make sure the client speaks a compatible Engine.IO version230        sid = query['sid'][0] if 'sid' in query else None231        if sid is None and query.get('EIO') != ['4']:232            self._log_error_once(233                'The client is using an unsupported version of the Socket.IO '234                'or Engine.IO protocols', 'bad-version')235            r = self._bad_request(236                'The client is using an unsupported version of the Socket.IO '237                'or Engine.IO protocols')238            start_response(r['status'], r['headers'])239            return [r['response']]240 241        if 'j' in query:242            jsonp = True243            try:244                jsonp_index = int(query['j'][0])245            except (ValueError, KeyError, IndexError):246                # Invalid JSONP index number247                pass248 249        if jsonp and jsonp_index is None:250            self._log_error_once('Invalid JSONP index number',251                                 'bad-jsonp-index')252            r = self._bad_request('Invalid JSONP index number')253        elif method == 'GET':254            if sid is None:255                # transport must be one of 'polling' or 'websocket'.256                # if 'websocket', the HTTP_UPGRADE header must match.257                upgrade_header = environ.get('HTTP_UPGRADE').lower() \258                    if 'HTTP_UPGRADE' in environ else None259                if transport == 'polling' \260                        or transport == upgrade_header == 'websocket':261                    r = self._handle_connect(environ, start_response,262                                             transport, jsonp_index)263                else:264                    self._log_error_once('Invalid websocket upgrade',265                                         'bad-upgrade')266                    r = self._bad_request('Invalid websocket upgrade')267            else:268                if sid not in self.sockets:269                    self._log_error_once('Invalid session ' + sid, 'bad-sid')270                    r = self._bad_request('Invalid session')271                else:272                    socket = self._get_socket(sid)273                    try:274                        packets = socket.handle_get_request(275                            environ, start_response)276                        if isinstance(packets, list):277                            r = self._ok(packets, jsonp_index=jsonp_index)278                        else:279                            r = packets280                    except exceptions.EngineIOError:281                        if sid in self.sockets:  # pragma: no cover282                            self.disconnect(sid)283                        r = self._bad_request()284                    if sid in self.sockets and self.sockets[sid].closed:285                        del self.sockets[sid]286        elif method == 'POST':287            if sid is None or sid not in self.sockets:288                self._log_error_once(289                    'Invalid session ' + (sid or 'None'), 'bad-sid')290                r = self._bad_request('Invalid session')291            else:292                socket = self._get_socket(sid)293                try:294                    socket.handle_post_request(environ)295                    r = self._ok(jsonp_index=jsonp_index)296                except exceptions.EngineIOError:297                    if sid in self.sockets:  # pragma: no cover298                        self.disconnect(sid)299                    r = self._bad_request()300                except:  # pragma: no cover301                    # for any other unexpected errors, we log the error302                    # and keep going303                    self.logger.exception('post request handler error')304                    r = self._ok(jsonp_index=jsonp_index)305        elif method == 'OPTIONS':306            r = self._ok()307        else:308            self.logger.warning('Method %s not supported', method)309            r = self._method_not_found()310 311        if not isinstance(r, dict):312            return r313        if self.http_compression and \314                len(r['response']) >= self.compression_threshold:315            encodings = [e.split(';')[0].strip() for e in316                         environ.get('HTTP_ACCEPT_ENCODING', '').split(',')]317            for encoding in encodings:318                if encoding in self.compression_methods:319                    r['response'] = \320                        getattr(self, '_' + encoding)(r['response'])321                    r['headers'] += [('Content-Encoding', encoding)]322                    break323        cors_headers = self._cors_headers(environ)324        start_response(r['status'], r['headers'] + cors_headers)325        return [r['response']]326 327    def shutdown(self):328        """Stop Socket.IO background tasks.329 330        This method stops background activity initiated by the Socket.IO331        server. It must be called before shutting down the web server.332        """333        self.logger.info('Socket.IO is shutting down')334        if self.service_task_event:  # pragma: no cover335            self.service_task_event.set()336            self.service_task_handle.join()337            self.service_task_handle = None338 339    def start_background_task(self, target, *args, **kwargs):340        """Start a background task using the appropriate async model.341 342        This is a utility function that applications can use to start a343        background task using the method that is compatible with the344        selected async mode.345 346        :param target: the target function to execute.347        :param args: arguments to pass to the function.348        :param kwargs: keyword arguments to pass to the function.349 350        This function returns an object that represents the background task,351        on which the ``join()`` methond can be invoked to wait for the task to352        complete.353        """354        th = self._async['thread'](target=target, args=args, kwargs=kwargs)355        th.start()356        return th  # pragma: no cover357 358    def sleep(self, seconds=0):359        """Sleep for the requested amount of time using the appropriate async360        model.361 362        This is a utility function that applications can use to put a task to363        sleep without having to worry about using the correct call for the364        selected async mode.365        """366        return self._async['sleep'](seconds)367 368    def _handle_connect(self, environ, start_response, transport,369                        jsonp_index=None):370        """Handle a client connection request."""371        if self.start_service_task:372            # start the service task to monitor connected clients373            self.start_service_task = False374            self.service_task_handle = self.start_background_task(375                self._service_task)376 377        sid = self.generate_id()378        s = socket.Socket(self, sid)379        self.sockets[sid] = s380 381        pkt = packet.Packet(packet.OPEN, {382            'sid': sid,383            'upgrades': self._upgrades(sid, transport),384            'pingTimeout': int(self.ping_timeout * 1000),385            'pingInterval': int(386                self.ping_interval + self.ping_interval_grace_period) * 1000})387        s.send(pkt)388        s.schedule_ping()389 390        # NOTE: some sections below are marked as "no cover" to workaround391        # what seems to be a bug in the coverage package. All the lines below392        # are covered by tests, but some are not reported as such for some393        # reason394        ret = self._trigger_event('connect', sid, environ, run_async=False)395        if ret is not None and ret is not True:  # pragma: no cover396            del self.sockets[sid]397            self.logger.warning('Application rejected connection')398            return self._unauthorized(ret or None)399 400        if transport == 'websocket':  # pragma: no cover401            ret = s.handle_get_request(environ, start_response)402            if s.closed and sid in self.sockets:403                # websocket connection ended, so we are done404                del self.sockets[sid]405            return ret406        else:  # pragma: no cover407            s.connected = True408            headers = None409            if self.cookie:410                if isinstance(self.cookie, dict):411                    headers = [(412                        'Set-Cookie',413                        self._generate_sid_cookie(sid, self.cookie)414                    )]415                else:416                    headers = [(417                        'Set-Cookie',418                        self._generate_sid_cookie(sid, {419                            'name': self.cookie, 'path': '/', 'SameSite': 'Lax'420                        })421                    )]422            try:423                return self._ok(s.poll(), headers=headers,424                                jsonp_index=jsonp_index)425            except exceptions.QueueEmpty:426                return self._bad_request()427 428    def _trigger_event(self, event, *args, **kwargs):429        """Invoke an event handler."""430        run_async = kwargs.pop('run_async', False)431        if event in self.handlers:432            def run_handler():433                try:434                    return self.handlers[event](*args)435                except:436                    self.logger.exception(event + ' handler error')437                    if event == 'connect':438                        # if connect handler raised error we reject the439                        # connection440                        return False441 442            if run_async:443                return self.start_background_task(run_handler)444            else:445                return run_handler()446 447    def _service_task(self):  # pragma: no cover448        """Monitor connected clients and clean up those that time out."""449        self.service_task_event = self.create_event()450        while not self.service_task_event.is_set():451            if len(self.sockets) == 0:452                # nothing to do453                if self.service_task_event.wait(timeout=self.ping_timeout):454                    break455                continue456 457            # go through the entire client list in a ping interval cycle458            sleep_interval = float(self.ping_timeout) / len(self.sockets)459 460            try:461                # iterate over the current clients462                for s in self.sockets.copy().values():463                    if s.closed:464                        try:465                            del self.sockets[s.sid]466                        except KeyError:467                            # the socket could have also been removed by468                            # the _get_socket() method from another thread469                            pass470                    elif not s.closing:471                        s.check_ping_timeout()472                    if self.service_task_event.wait(timeout=sleep_interval):473                        raise KeyboardInterrupt()474            except (SystemExit, KeyboardInterrupt):475                self.logger.info('service task canceled')476                break477            except:478                # an unexpected exception has occurred, log it and continue479                self.logger.exception('service task exception')480 
codekingpro/portable-devtools · Team Ai