HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberCP/lib/python3.10/site-packages/tornado/__pycache__/websocket.cpython-310.pyc
o

�h��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlZddlZddl
mZmZddlmZmZmZddlmZmZmZddlmZmZddlmZmZdd	lmZmZdd
l m!Z!ddlm"Z"ddl#m$Z$dd
l%m&Z&ddl'm(Z(ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4ddl5m6Z6e*r�ddl7m8Z8Gdd�de8�Z9Gdd�de8�Z:Gdd�de8�Z;dZ<Gdd�de=�Z>Gdd�de>�Z?Gdd�de=�Z@Gdd �d eA�ZBGd!d"�d"ejCjD�ZEd#e,d$e,d%dfd&d'�ZFGd(d)�d)ejG�ZHGd*d+�d+eA�ZIGd,d-�d-eA�ZJGd.d/�d/eH�ZKGd0d1�d1e"jL�ZMdddddde<ddf	d2e/eNejOfd3e-e2d4gdfd5e-ePd6e-e2e/deNeQfgdfd7e-e.eNe,fd8e-ePd9e-ePd:eRd;e-e0eNd<e-e!d%d=fd>d?�ZSdS)@a�Implementation of the WebSocket protocol.

`WebSockets <http://dev.w3.org/html5/websockets/>`_ allow for bidirectional
communication between the browser and server. WebSockets are supported in the
current versions of all major browsers.

This module implements the final version of the WebSocket protocol as
defined in `RFC 6455 <http://tools.ietf.org/html/rfc6455>`_.

.. versionchanged:: 4.0
   Removed support for the draft 76 protocol version.
�N)�urlparse)�Future�"future_set_result_unless_cancelled)�utf8�
native_str�
to_unicode)�gen�
httpclient�httputil)�IOLoop�PeriodicCallback)�StreamClosedError�IOStream)�gen_log�app_log)�Resolver)�simple_httpclient)�Queue)�	TCPClient)�_websocket_mask)�
TYPE_CHECKING�cast�Any�Optional�Dict�Union�List�	Awaitable�Callable�Tuple�Type)�
TracebackType)�Protocolc@s0eZdZdedefdd�Zdedefdd�ZdS)	�_Compressor�data�returncC�dS�N���selfr$r(r(�D/usr/local/CyberCP/lib/python3.10/site-packages/tornado/websocket.py�compress;�z_Compressor.compress�modecCr&r'r()r*r.r(r(r+�flush>r-z_Compressor.flushN)�__name__�
__module__�__qualname__�bytesr,�intr/r(r(r(r+r#:sr#c@s&eZdZdZdededefdd�ZdS)�
_Decompressor�r$�
max_lengthr%cCr&r'r()r*r$r7r(r(r+�
decompressDr-z_Decompressor.decompressN)r0r1r2�unconsumed_tailr3r4r8r(r(r(r+r5Asr5c@s�eZdZ	ddeedeeddfdd�Zdeeefdedfd	d
�Z	deddfdd
�Z
deddfdd�Zdeee
dee
deeddfdd�ZdS)�_WebSocketDelegateN�
close_code�close_reasonr%cCr&r'r(�r*r;r<r(r(r+�on_ws_connection_closeKsz)_WebSocketDelegate.on_ws_connection_close�messagezAwaitable[None]cCr&r'r(�r*r?r(r(r+�
on_messagePr-z_WebSocketDelegate.on_messager$cCr&r'r(r)r(r(r+�on_pingSr-z_WebSocketDelegate.on_pingcCr&r'r(r)r(r(r+�on_pongVr-z_WebSocketDelegate.on_pong�typ�value�tbcCr&r'r(�r*rDrErFr(r(r+�
log_exceptionYsz _WebSocketDelegate.log_exception�NN)r0r1r2rr4�strr>rr3rArBrCr �
BaseExceptionr!rHr(r(r(r+r:Gs*���
�
����r:i�c@�eZdZdS)�WebSocketErrorN�r0r1r2r(r(r(r+rMe�rMc@seZdZdZdS)�WebSocketClosedErrorzLRaised by operations on a closed connection.

    .. versionadded:: 3.2
    N)r0r1r2�__doc__r(r(r(r+rPisrPc@rL)�_DecompressTooLargeErrorNrNr(r(r(r+rRrrOrRc@sHeZdZddedfdeedeededeeee	fddf
dd�Z
dS)	�_WebSocketParamsN�
ping_interval�ping_timeout�max_message_size�compression_optionsr%cCs||_||_||_||_dSr'�rTrUrVrW)r*rTrUrVrWr(r(r+�__init__ws
z_WebSocketParams.__init__)r0r1r2�_default_max_message_sizer�floatr4rrJrrYr(r(r(r+rSvs ������rScs*eZdZdZdejjdejde	ddf�fdd�Z
d	e	de	ddfd
d�Zede
efdd
��Zede
efdd��Zedefdd��Z	dDdeeeeee	ffdeddfdd�Zdeede
efdd�Zede
efdd��Zde
eee	ffdd�Zd	edede
edfdd �Zdeeefde
edfd!d"�ZdEd$eeefddfd%d&�Z d$eddfd'd(�Z!d$eddfd)d*�Z"dFd+d,�Z#dGd-e
ed.e
eddfd/d0�Z$d1edefd2d3�Z%d4eddfd5d6�Z&dFd7d8�Z'	dGd9e
ed:e
eddfd;d<�Z(dF�fd=d>�Z)de
d?fd@dA�Z*de+fdBdC�Z,�Z-S)H�WebSocketHandlera�Subclass this class to create a basic WebSocket handler.

    Override `on_message` to handle incoming messages, and use
    `write_message` to send messages to the client. You can also
    override `open` and `on_close` to handle opened and closed
    connections.

    Custom upgrade response headers can be sent by overriding
    `~tornado.web.RequestHandler.set_default_headers` or
    `~tornado.web.RequestHandler.prepare`.

    See http://dev.w3.org/html5/websockets/ for details on the
    JavaScript interface.  The protocol is specified at
    http://tools.ietf.org/html/rfc6455.

    Here is an example WebSocket handler that echos back all received messages
    back to the client:

    .. testcode::

      class EchoWebSocket(tornado.websocket.WebSocketHandler):
          def open(self):
              print("WebSocket opened")

          def on_message(self, message):
              self.write_message(u"You said: " + message)

          def on_close(self):
              print("WebSocket closed")

    .. testoutput::
       :hide:

    WebSockets are not standard HTTP connections. The "handshake" is
    HTTP, but after the handshake, the protocol is
    message-based. Consequently, most of the Tornado HTTP facilities
    are not available in handlers of this type. The only communication
    methods available to you are `write_message()`, `ping()`, and
    `close()`. Likewise, your request handler class should implement
    `open()` method rather than ``get()`` or ``post()``.

    If you map the handler above to ``/websocket`` in your application, you can
    invoke it in JavaScript with::

      var ws = new WebSocket("ws://localhost:8888/websocket");
      ws.onopen = function() {
         ws.send("Hello, world");
      };
      ws.onmessage = function (evt) {
         alert(evt.data);
      };

    This script pops up an alert box that says "You said: Hello, world".

    Web browsers allow any site to open a websocket connection to any other,
    instead of using the same-origin policy that governs other network
    access from JavaScript.  This can be surprising and is a potential
    security hole, so since Tornado 4.0 `WebSocketHandler` requires
    applications that wish to receive cross-origin websockets to opt in
    by overriding the `~WebSocketHandler.check_origin` method (see that
    method's docs for details).  Failure to do so is the most likely
    cause of 403 errors when making a websocket connection.

    When using a secure websocket connection (``wss://``) with a self-signed
    certificate, the connection from a browser may fail because it wants
    to show the "accept this certificate" dialog but has nowhere to show it.
    You must first visit a regular HTML page using the same certificate
    to accept it before the websocket connection will succeed.

    If the application setting ``websocket_ping_interval`` has a non-zero
    value, a ping will be sent periodically, and the connection will be
    closed if a response is not received before the ``websocket_ping_timeout``.

    Messages larger than the ``websocket_max_message_size`` application setting
    (default 10MiB) will not be accepted.

    .. versionchanged:: 4.5
       Added ``websocket_ping_interval``, ``websocket_ping_timeout``, and
       ``websocket_max_message_size``.
    �application�request�kwargsr%Ncs2t�j||fi|��d|_d|_d|_d|_dS�NF)�superrY�
ws_connectionr;r<�_on_close_called)r*r]r^r_��	__class__r(r+rY�s

zWebSocketHandler.__init__�argsc�sD�||_||_|jj�dd���dkr&|�d�d}|�|�t�	|�dS|jj}t
dd�|�dd��d	��}d
|vrO|�d�d}|�|�t�	|�dSd|jjvr]|jj�d�}n|jj�d
d�}|dur�|�|�s�|�d�d}|�|�t�	|�dS|�
�|_|jr�|j�|�IdHdS|�dd�|�dd�dS)N�Upgrade��	websocket�z"Can "Upgrade" only to "WebSocket".cSs|����Sr')�strip�lower)�sr(r(r+�<lambda>�sz&WebSocketHandler.get.<locals>.<lambda>�
Connection�,�upgradez"Connection" must be "Upgrade".�OriginzSec-Websocket-Origini�z#Cross origin websockets not allowedi�zUpgrade Required�Sec-WebSocket-Versionz7, 8, 13)�	open_args�open_kwargsr^�headers�getrl�
set_status�finishr�debug�map�split�check_origin�get_websocket_protocolrb�accept_connection�
set_header)r*rfr_�log_msgrv�
connection�originr(r(r+rw�sB�


�






zWebSocketHandler.getcC�|j�dd�S)zpThe interval for websocket keep-alive pings.

        Set websocket_ping_interval = 0 to disable pings.
        �websocket_ping_intervalN��settingsrw�r*r(r(r+rT�zWebSocketHandler.ping_intervalcCr�)z�If no ping is received in this many seconds,
        close the websocket connection (VPNs, etc. can fail to cleanly close ws connections).
        Default is max of 3 pings or 30 seconds.
        �websocket_ping_timeoutNr�r�r(r(r+rUr�zWebSocketHandler.ping_timeoutcCs|j�dt�S)z�Maximum allowed message size.

        If the remote peer sends a message larger than this, the connection
        will be closed.

        Default is 10MiB.
        �websocket_max_message_size)r�rwrZr�r(r(r+rV&s	�z!WebSocketHandler.max_message_sizeFr?�binary�Future[None]cCs@|jdus
|j��r
t��t|t�rtj�|�}|jj||d�S)aHSends the given message to the client of this Web Socket.

        The message may be either a string or a dict (which will be
        encoded as json).  If the ``binary`` argument is false, the
        message will be sent as utf8; in binary mode any byte string
        is allowed.

        If the connection is already closed, raises `WebSocketClosedError`.
        Returns a `.Future` which can be used for flow control.

        .. versionchanged:: 3.2
           `WebSocketClosedError` was added (previously a closed connection
           would raise an `AttributeError`)

        .. versionchanged:: 4.3
           Returns a `.Future` which can be used for flow control.

        .. versionchanged:: 5.0
           Consistently raises `WebSocketClosedError`. Previously could
           sometimes raise `.StreamClosedError`.
        N�r�)	rb�
is_closingrP�
isinstance�dict�tornado�escape�json_encode�
write_message�r*r?r�r(r(r+r�3s

zWebSocketHandler.write_message�subprotocolscC�dS)a�Override to implement subprotocol negotiation.

        ``subprotocols`` is a list of strings identifying the
        subprotocols proposed by the client.  This method may be
        overridden to return one of those strings to select it, or
        ``None`` to not select a subprotocol.

        Failure to select a subprotocol does not automatically abort
        the connection, although clients may close the connection if
        none of their proposed subprotocols was selected.

        The list may be empty, in which case this method must return
        None. This method is always called exactly once even if no
        subprotocols were proposed so that the handler can be advised
        of this fact.

        .. versionchanged:: 5.1

           Previously, this method was called with a list containing
           an empty string instead of an empty list if no subprotocols
           were proposed by the client.
        Nr()r*r�r(r(r+�select_subprotocolQsz#WebSocketHandler.select_subprotocolcCs|jdusJ�|jjS)zYThe subprotocol returned by `select_subprotocol`.

        .. versionadded:: 5.1
        N)rb�selected_subprotocolr�r(r(r+r�jsz%WebSocketHandler.selected_subprotocolcCr�)a�Override to return compression options for the connection.

        If this method returns None (the default), compression will
        be disabled.  If it returns a dict (even an empty one), it
        will be enabled.  The contents of the dict may be used to
        control the following compression options:

        ``compression_level`` specifies the compression level.

        ``mem_level`` specifies the amount of memory used for the internal compression state.

         These parameters are documented in details here:
         https://docs.python.org/3.6/library/zlib.html#zlib.compressobj

        .. versionadded:: 4.1

        .. versionchanged:: 4.5

           Added ``compression_level`` and ``mem_level``.
        Nr(r�r(r(r+�get_compression_optionsssz(WebSocketHandler.get_compression_optionscOr�)a�Invoked when a new WebSocket is opened.

        The arguments to `open` are extracted from the `tornado.web.URLSpec`
        regular expression, just like the arguments to
        `tornado.web.RequestHandler.get`.

        `open` may be a coroutine. `on_message` will not be called until
        `open` has returned.

        .. versionchanged:: 5.1

           ``open`` may be a coroutine.
        Nr()r*rfr_r(r(r+�open�szWebSocketHandler.opencCst�)z�Handle incoming messages on the WebSocket

        This method must be overridden.

        .. versionchanged:: 4.5

           ``on_message`` can be a coroutine.
        ��NotImplementedErrorr@r(r(r+rA�s	zWebSocketHandler.on_messager6r$cCs2t|�}|jdus|j��rt��|j�|�dS)a�Send ping frame to the remote end.

        The data argument allows a small amount of data (up to 125
        bytes) to be sent as a part of the ping message. Note that not
        all websocket implementations expose this data to
        applications.

        Consider using the ``websocket_ping_interval`` application
        setting instead of sending pings manually.

        .. versionchanged:: 5.1

           The data argument is now optional.

        N)rrbr�rP�
write_pingr)r(r(r+�ping�szWebSocketHandler.pingcCr�)z6Invoked when the response to a ping frame is received.Nr(r)r(r(r+rC��zWebSocketHandler.on_pongcCr�)z*Invoked when the a ping frame is received.Nr(r)r(r(r+rB�r�zWebSocketHandler.on_pingcCr�)a`Invoked when the WebSocket is closed.

        If the connection was closed cleanly and a status code or reason
        phrase was supplied, these values will be available as the attributes
        ``self.close_code`` and ``self.close_reason``.

        .. versionchanged:: 4.0

           Added ``close_code`` and ``close_reason`` attributes.
        Nr(r�r(r(r+�on_close�szWebSocketHandler.on_close�code�reasoncCs"|jr|j�||�d|_dSdS)aCCloses this Web Socket.

        Once the close handshake is successful the socket will be closed.

        ``code`` may be a numeric status code, taken from the values
        defined in `RFC 6455 section 7.4.1
        <https://tools.ietf.org/html/rfc6455#section-7.4.1>`_.
        ``reason`` may be a textual message about why the connection is
        closing.  These values are made available to the client, but are
        not otherwise interpreted by the websocket protocol.

        .. versionchanged:: 4.0

           Added the ``code`` and ``reason`` arguments.
        N)rb�close�r*r�r�r(r(r+r��s
�zWebSocketHandler.closer�cCs,t|�}|j}|��}|jj�d�}||kS)a�Override to enable support for allowing alternate origins.

        The ``origin`` argument is the value of the ``Origin`` HTTP
        header, the url responsible for initiating this request.  This
        method is not called for clients that do not send this header;
        such requests are always allowed (because all browsers that
        implement WebSockets support this header, and non-browser
        clients do not have the same cross-site security concerns).

        Should return ``True`` to accept the request or ``False`` to
        reject it. By default, rejects all requests with an origin on
        a host other than this one.

        This is a security protection against cross site scripting attacks on
        browsers, since WebSockets are allowed to bypass the usual same-origin
        policies and don't use CORS headers.

        .. warning::

           This is an important security measure; don't disable it
           without understanding the security implications. In
           particular, if your authentication is cookie-based, you
           must either restrict the origins allowed by
           ``check_origin()`` or implement your own XSRF-like
           protection for websocket connections. See `these
           <https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html>`_
           `articles
           <https://devcenter.heroku.com/articles/websocket-security>`_
           for more.

        To accept all cross-origin traffic (which was the default prior to
        Tornado 4.0), simply override this method to always return ``True``::

            def check_origin(self, origin):
                return True

        To allow connections from any subdomain of your site, you might
        do something like::

            def check_origin(self, origin):
                parsed_origin = urllib.parse.urlparse(origin)
                return parsed_origin.netloc.endswith(".mydomain.com")

        .. versionadded:: 4.0

        �Host)r�netlocrlr^rvrw)r*r��
parsed_origin�hostr(r(r+r}�s
/zWebSocketHandler.check_originrEcCs|jdusJ�|j�|�dS)a1Set the no-delay flag for this stream.

        By default, small messages may be delayed and/or combined to minimize
        the number of packets sent.  This can sometimes cause 200-500ms delays
        due to the interaction between Nagle's algorithm and TCP delayed
        ACKs.  To reduce this delay (at the expense of possibly increasing
        bandwidth usage), call ``self.set_nodelay(True)`` once the websocket
        connection is established.

        See `.BaseIOStream.set_nodelay` for additional details.

        .. versionadded:: 3.1
        N)rb�set_nodelay�r*rEr(r(r+r�szWebSocketHandler.set_nodelaycCs:|jr|j��d|_|jsd|_|��|��dSdS)NT)rb�on_connection_closercr��
_break_cyclesr�r(r(r+r�-s
�z$WebSocketHandler.on_connection_closer;r<cC�||_||_|��dSr'�r;r<r�r=r(r(r+r>6�z'WebSocketHandler.on_ws_connection_closecs$|��dks	|jrt���dSdS�N�e)�
get_statusrcrar�r�rdr(r+r�=s�zWebSocketHandler._break_cycles�WebSocketProtocolcCs@|jj�d�}|dvrt|j|j|j|��d�}t|d|�SdS)Nrs)�7�8�13rXF)	r^rvrwrSrTrUrVr��WebSocketProtocol13)r*�websocket_version�paramsr(r(r+r~Fs�z'WebSocketHandler.get_websocket_protocolcCsdD]}t||t�q|��S)N)�write�redirectr��
set_cookierxr/ry)�setattr�#_raise_not_supported_for_websockets�detach)r*�methodr(r(r+�_detach_streamRs	zWebSocketHandler._detach_stream�F�r6�r%NrI).r0r1r2rQr��web�Applicationr
�HTTPServerRequestrrYrw�propertyrr[rTrUr4rVrr3rJr�boolr�rr�r�r�rr�rAr�rCrBr�r�r}r�r�r>r�r~rr��
__classcell__r(r(rdr+r\�sfQ����4
���
�"
 
8

���
�	r\rfr_r%cOstd��)Nz$Method not supported for Web Sockets)�RuntimeError)rfr_r(r(r+r�asr�c	@s^eZdZdZd0dd�Zded	ed
eddfdd
�Zd1dd�Zd1dd�Z	e
jd2dee
deeddfdd��Ze
jdefdd��Ze
jdeddfdd��Ze
j	d3deeeeeeffdeddfdd��Zee
jdeefd d!���Ze
jd"eddfd#d$��Ze
jd%eeefd&ejddfd'd(��Ze
jd1d)d*��Ze
jd1d+d,��Ze
jd-eddfd.d/��Z dS)4r�z+Base class for WebSocket protocol versions.�handlerr:r%NcCs||_d|_d|_d|_dSr`)r��stream�client_terminated�server_terminated�r*r�r(r(r+rYhs
zWebSocketProtocol.__init__�callbackrfr_zOptional[Future[Any]]cOsxz	||i|��}Wnty|jjt���|��YdSw|dur:t�|�}|jdus0J�|jj	�
|dd��|S)z�Runs the given callback with exception handling.

        If the callback is a coroutine, returns its Future. On error, aborts the
        websocket connection and returns None.
        NcSs|��Sr')�result��fr(r(r+rn�sz1WebSocketProtocol._run_callback.<locals>.<lambda>)�	Exceptionr�rH�sys�exc_info�_abortr�convert_yieldedr��io_loop�
add_future)r*r�rfr_r�r(r(r+�
_run_callbackns�
zWebSocketProtocol._run_callbackcCs|��dSr')r�r�r(r(r+r��sz%WebSocketProtocol.on_connection_closecCs,d|_d|_|jdur|j��|��dS)z?Instantly aborts the WebSocket connection by closing the socketTN)r�r�r�r�r�r(r(r+r��s


zWebSocketProtocol._abortr�r�cC�t��r'r�r�r(r(r+r���zWebSocketProtocol.closecCr�r'r�r�r(r(r+r��r�zWebSocketProtocol.is_closingc���t��r'r�r�r(r(r+r���z#WebSocketProtocol.accept_connectionFr?r�r�cCr�r'r�r�r(r(r+r���zWebSocketProtocol.write_messagecCr�r'r�r�r(r(r+r��sz&WebSocketProtocol.selected_subprotocolr$cCr�r'r�r)r(r(r+r��r�zWebSocketProtocol.write_ping�keyrvcCr�r'r�)r*r�rvr(r(r+�_process_server_headers�r�z)WebSocketProtocol._process_server_headerscCr�r'r�r�r(r(r+�
start_pinging�r�zWebSocketProtocol.start_pingingc�r�r'r�r�r(r(r+�_receive_frame_loop�r�z%WebSocketProtocol._receive_frame_loop�xcCr�r'r��r*r�r(r(r+r��r�zWebSocketProtocol.set_nodelay)r�r:r%Nr�rIr�)!r0r1r2rQrYrrr�r�r��abc�abstractmethodrr4rJr�r�r�r\rrr3rr�r�r�r�r
�HTTPHeadersr�r�r�r�r(r(r(r+r�es`
���
�

"����
���r�c
@sVeZdZ	ddedeedeeeefddfdd�Z	dd	d
�Z
dedefdd
�ZdS)�_PerMessageDeflateCompressorN�
persistent�	max_wbitsrWr%cCs�|durtj}d|krtjksntd|tj��||_|dus%d|vr,tjjj|_n|d|_|dus9d|vr=d|_	n|d|_	|rK|�
�|_dSd|_dS)N��.Invalid max_wbits value %r; allowed range 8-%d�compression_level�	mem_level)�zlib�	MAX_WBITS�
ValueError�
_max_wbitsr�r��GZipContentEncoding�
GZIP_LEVEL�_compression_level�
_mem_level�_create_compressor�_compressor)r*r�r�rWr(r(r+rY�s&�


z%_PerMessageDeflateCompressor.__init__r#cCst�|jtj|j|j�Sr')r��compressobjr��DEFLATEDr�r�r�r(r(r+r����z/_PerMessageDeflateCompressor._create_compressorr$cCs>|jp|��}|�|�|�tj�}|�d�sJ�|dd�S)N������)r�r�r,r/r��Z_SYNC_FLUSH�endswith)r*r$�
compressorr(r(r+r,�sz%_PerMessageDeflateCompressor.compressr')r%r#)
r0r1r2r�rr4rrJrrYr�r3r,r(r(r(r+r��s����
�
#r�c@sZeZdZ	ddedeededeeeefddf
dd�Z	dd
d�Z
dedefd
d�ZdS)�_PerMessageDeflateDecompressorNr�r�rVrWr%cCs\||_|dur
tj}d|krtjksntd|tj��||_|r)|��|_dSd|_dS)Nr�r�)�_max_message_sizer�r�r�r��_create_decompressor�
_decompressor)r*r�r�rVrWr(r(r+rY�s��
z'_PerMessageDeflateDecompressor.__init__r5cCst�|j�Sr')r��
decompressobjr�r�r(r(r+r	sz3_PerMessageDeflateDecompressor._create_decompressorr$cCs0|jp|��}|�|d|j�}|jrt��|S)Nr)r	rr8rr9rR)r*r$�decompressorr�r(r(r+r8s
�z)_PerMessageDeflateDecompressor.decompressr')r%r5)
r0r1r2r�rr4rrJrrYrr3r8r(r(r(r+r�s�����
�
rc@s�eZdZdZdZdZdZdZeeBeBZdZ	dZ
dd	d
ededdfd
d�Z
edeefdd��Zejdeeddfdd��Zdeddfdd�Zdeddfdd�Zedeeefdefdd��Zdedefdd�Zdeddfdd�Zdejdeeee eefffdd �Z!deeefdejddfd!d"�Z"	dTd#ed$e ee#fd%ee ee#fde ee#ffd&d'�Z$	dTd#ed$e ee#fd%ee ee#fddfd(d)�Z%	*dUd+ed,e&d-ed.e&dd/f
d0d1�Z'	2dVd3eeee ee#ffd4edd/fd5d6�Z(d-eddfd7d8�Z)dWd9d:�Z*d;e&defd<d=�Z+dWd>d?�Z,d,e&d-edd@fdAdB�Z-dXdCee&dDeeddfdEdF�Z.defdGdH�Z/edee0fdIdJ��Z1edee0fdKdL��Z2dWdMdN�Z3dWdOdP�Z4dQeddfdRdS�Z5dS)Yr�z�Implementation of the WebSocket protocol from RFC 6455.

    This class supports versions 7 and 8 of the protocol in addition to the
    final version 13.
    ��@� ��Nr�r:�
mask_outgoingr�r%cCs�t�||�||_||_d|_d|_d|_d|_d|_d|_	d|_
d|_|j|_
d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_dS)NFrg)r�rYrr��_final_frame�
_frame_opcode�
_masked_frame�_frame_mask�
_frame_length�_fragmented_message_buffer�_fragmented_message_opcode�_waitingrW�_compression_optionsr	r��_frame_compressed�_message_bytes_in�_message_bytes_out�_wire_bytes_in�_wire_bytes_out�
ping_callback�	last_ping�	last_pongr;r<)r*r�rr�r(r(r+rY's0
zWebSocketProtocol13.__init__cCs|jSr'��_selected_subprotocolr�r(r(r+r�Lr�z(WebSocketProtocol13.selected_subprotocolrEcCs
||_dSr'r#r�r(r(r+r�Ps
c�s��z|�|�Wnty#|�d�d}|�|�t�|�YdSwz|�|�IdHWdStjy>|�	�YdStyRtjddd�|�	�YdSw)Nrj�!Missing/Invalid WebSocket headersz$Malformed WebSocket request receivedT�r�)
�_handle_websocket_headersr�rxryrrz�_accept_connection�asyncio�CancelledErrorr�)r*r�r�r(r(r+rTs(�


��z%WebSocketProtocol13.accept_connectioncs&d}tt�fdd�|��std��dS)z�Verifies all invariant- and required headers

        If a header is missing or have an incorrect value ValueError will be
        raised
        )r��Sec-Websocket-KeyzSec-Websocket-Versioncs�jj�|�Sr')r^rvrwr��r�r(r+rnosz?WebSocketProtocol13._handle_websocket_headers.<locals>.<lambda>r%N)�allr{r�)r*r��fieldsr(r,r+r'hs�z-WebSocketProtocol13._handle_websocket_headersr�cCs2t��}|�t|��|�d�tt�|����S)zoComputes the value for the Sec-WebSocket-Accept header,
        given the value for Sec-WebSocket-Key.
        s$258EAFA5-E914-47DA-95CA-C5AB0DC85B11)�hashlib�sha1�updaterr�base64�	b64encode�digest)r�r0r(r(r+�compute_accept_valuers
z(WebSocketProtocol13.compute_accept_valuecCst�tt|jj�d���S)Nr+)r�r5rrJr^rvrwr�r(r(r+�_challenge_response|rz'WebSocketProtocol13._challenge_responsec	�s��|jj�d�}|rdd�|�d�D�}ng}|�|�|_|jr.|j|vs'J�|�d|j�|�|jj�}|D]8}|ddkro|jduro|�	d|d|j�d	|dvra|dd	dura|dd	=|�d
t
�d|d��nq7|�d�|�
d�|�d
d�|�dd
�|�d|�|��|��|��|_|��z|j|ji|j��}|dur�|IdHWnty�|jt���|��YdSw|��IdHdS)N�Sec-WebSocket-ProtocolcSsg|]}|���qSr()rk)�.0rmr(r(r+�
<listcomp>�sz:WebSocketProtocol13._accept_connection.<locals>.<listcomp>rpr�permessage-deflate�server��client_max_window_bits�Sec-WebSocket-ExtensionszContent-Typer�rgrirozSec-WebSocket-Accept)r^rvrwr|r�r�r��_parse_extensions_headerr�_create_compressorsr
�_encode_header�clear_headerrxr6ryr�r�r�r�rtrur�rHr�r�r�r�)r*r��subprotocol_headerr��
extensions�ext�open_resultr(r(r+r(�sR�
��



��z&WebSocketProtocol13._accept_connectionrvcCs(|�dd�}|rdd�|�d�D�SgS)Nr>rhcSsg|]	}t�|����qSr()r
�
_parse_headerrk)r8�er(r(r+r9�sz@WebSocketProtocol13._parse_extensions_header.<locals>.<listcomp>rp)rwr|)r*rvrDr(r(r+r?�sz,WebSocketProtocol13._parse_extensions_headercCs�|d��dks
J�|d��dksJ�|�|�}|d|ks!J�|�|�}|D]}|ddkr>|jdur>|�d	|d
�q(td|��|�dd�|_dS)
z�Process the headers sent by the server to this client connection.

        'key' is the websocket handshake challenge/response key.
        rgrirorqzSec-Websocket-Acceptrr:N�clientr<zunsupported extension %rr7)rlr5r?rr@r�rwr�)r*r�rv�acceptrDrEr(r(r+r��s


z+WebSocketProtocol13._process_server_headers�side�agreed_parametersrWcCsNt|d|vd�}|�|dd�}|durtj|d<nt|�|d<||d<|S)zlConverts a websocket agreed_parameters set to keyword arguments
        for our compressor objects.
        �_no_context_takeover)r��_max_window_bitsNr�rW)r�rwr�r�r4)r*rKrLrW�options�wbits_headerr(r(r+�_get_compressor_options�s	
�z+WebSocketProtocol13._get_compressor_optionscCsztgd��}|D]}||vrtd|��q|dkrdnd}tdi|�|||���|_tdd|jji|�|||���|_dS)N)�server_no_context_takeover�client_no_context_takeover�server_max_window_bitsr=z$unsupported compression parameter %rr;rIrVr()	�setr�r�rQr�rr�rVr	)r*rKrLrW�allowed_keysr��
other_sider(r(r+r@�s$������z'WebSocketProtocol13._create_compressorsr�fin�opcoder$�flagsr�c
Cs�t|�}|d@r|std��|dkrtd��|r|j}nd}t�d||B|B�}|jr.d}nd}|dkr?|t�d||B�7}n|d	krO|t�d
d|B|�7}n|t�dd|B|�7}|jrit�d
�}	|	t|	|�}||7}|j	t|�7_	|j
�|�S)Nr�z$control frames may not be fragmented�}z/control frame payloads may not exceed 125 bytesr�Br�~i��z!BHz!BQ��)�lenr��FIN�struct�packr�os�urandomrrr�r�)
r*rXrYr$rZ�data_len�finbit�frame�mask_bit�maskr(r(r+�_write_frames0
z WebSocketProtocol13._write_frameFr?r�cs�|rd}nd}t|t�rtj�|�}tj�|�}t|t�sJ�|jt|�7_d}|j	r8|j	�
|�}||jO}z|jd|||d��Wn
t
yMt��wd
�fdd	�}t�|��S)z9Sends the given message to the client of this Web Socket.�r<rT)rZr%Nc�s(�z�IdHWdStyt��wr')r
rPr(��futr(r+�wrapper>s��z2WebSocketProtocol13.write_message.<locals>.wrapperr�)r�r�r�r�r�rr3rr`r�r,�RSV1rkr
rPr)�
ensure_future)r*r?r�rYrZror(rmr+r�#s&

�z!WebSocketProtocol13.write_messagecCs t|t�sJ�|�dd|�dS)zSend ping frame.T�	N)r�r3rkr)r(r(r+r�FszWebSocketProtocol13.write_pingc�sR�z|js|��IdH|jrWn
ty|��Ynw|j�|j|j�dSr')r��_receive_framer
r�r�r>r;r<r�r(r(r+r�Ks����z'WebSocketProtocol13._receive_frame_loop�nc�s&�|j�|�IdH}|j|7_|Sr')r��
read_bytesr)r*rtr$r(r(r+�_read_bytesSs�zWebSocketProtocol13._read_bytesc�sT�|�d�IdH}t�d|�\}}||j@}||j@}||j@}|d@}|jdur;|dkr;t||j@�|_	||jM}|rC|�
�dSt|d@�}|d@}	|rY|	dkrY|�
�dS|	dkra|	|_n)|	dkrv|�d�IdH}t�d|�d}	n|	dkr�|�d�IdH}t�d	|�d}	|	}
|jdur�|
t
|j�7}
|
|jjkr�|�d
d�|�
�dS|r�|�d�IdH|_|�|	�IdH}|r�|jdus�J�t|j|�}|r�|s�|�
�dSn:|dkr�|jdur�|�
�dS|j�|�|r�|j}t|j�}d|_n|jdu�r|�
�dS|�s||_t|�|_|�r&|�||�}|du�r(|IdHdSdSdS)
Nrl�BBr�rrr^r]z!Hz!Q��zmessage too bigr_)rvrb�unpackra�RSV_MASK�OPCODE_MASKr	r�rprr�rrr`r�rVr�rr�extendrr3�	bytearray�_handle_message)r*r$�header�mask_payloadlen�is_final_frame�
reserved_bitsrY�opcode_is_control�	is_masked�
payloadlen�new_len�handled_futurer(r(r+rsXs��



�

�

�z"WebSocketProtocol13._receive_framezOptional[Future[None]]cCs�|jrdS|jr,|jdusJ�z|j�|�}Wnty+|�dd�|��YdSw|dkrW|jt|�7_z|�	d�}Wnt
yN|��YdSw|�|jj
|�S|dkrl|jt|�7_|�|jj
|�S|dkr�d|_t|�dkr�t�d	|dd��d
|_t|�dkr�t|dd��|_|�|j�dS|dkr�z	|�dd|�Wn
ty�|��Ynw|�|jj|�dS|dkr�t����|_|�|jj|�S|��dS)
z>Execute on_message, returning its Future if it is a coroutine.Nrxz#message too big after decompressionr<zutf-8rlr�T�>Hrrr�
)r�rr	r8rRr�r�rr`�decode�UnicodeDecodeErrorr�r�rArbryr;rr<rkr
rBr�current�timer"rC)r*rYr$�decodedr(r(r+r~�sX�����z#WebSocketProtocol13._handle_messager�r�cCs|jsC|j��s@|dur|durd}|durd}nt�d|�}|dur)|t|�7}z	|�dd|�Wn
ty?|��Ynwd|_|j	r\|j
durV|jj�|j
�d|_
|j�
�n|j
durq|jj�|jj��d|j�|_
|jr~|j��d|_dSdS)z Closes the WebSocket connection.N��r6r�Tr��)r�r��closedrbrcrrkr
r�r�rr��remove_timeoutr��add_timeoutr�r �stop)r*r�r��
close_datar(r(r+r��s8
�

�

�zWebSocketProtocol13.closecCs|j��p
|jp
|jS)z�Return ``True`` if this connection is closing.

        The connection is considered closing if either side has
        initiated its closing handshake or if the stream has been
        shut down uncleanly.
        )r�r�r�r�r�r(r(r+r��szWebSocketProtocol13.is_closingcCs|jj}|dur
|SdS)Nr)r�rT)r*�intervalr(r(r+rTsz!WebSocketProtocol13.ping_intervalcCs2|jj}|dur
|S|jdusJ�td|jd�S)N��)r�rUrT�max)r*�timeoutr(r(r+rUs
z WebSocketProtocol13.ping_timeoutcCsR|jdusJ�|jdkr't����|_|_t|j|jd�|_|j�	�dSdS)z9Start sending periodic pings to keep the connection aliveNrr�)
rTrr�r�r!r"r�
periodic_pingr �startr�r(r(r+r�s
��z!WebSocketProtocol13.start_pingingcCs�|��r|jdur|j��dSt����}||j}||j}|jdus'J�|j	dus.J�|d|jkr@||j	kr@|�
�dS|�d�||_dS)z�Send a ping to keep the websocket alive

        Called periodically if the websocket_ping_interval is set and non-zero.
        Nrlr6)r�r r�rr�r�r"r!rTrUr�r�)r*�now�since_last_pong�since_last_pingr(r(r+r� s





z!WebSocketProtocol13.periodic_pingr�cCs|j�|�dSr')r�r�r�r(r(r+r�;�zWebSocketProtocol13.set_nodelayr')rr�r�rI)6r0r1r2rQrarp�RSV2�RSV3rzr{r�r�rSrYr�rrJr��setterr\rr'�staticmethodrr3r5r6r(r
r�rrrr?r�rrQr@r4rkr�r�r�rvrsr~r�r�r[rTrUr�r�r�r(r(r(r+r�s����
�%
	3�
�
��
���
��

���
��
������
�"���
�#

S 3 	


r�csLeZdZdZdZddddeddfdejdee	e
deefgdfdee
eefdeedeeded	eeed
eeddf�fdd
�
ZdAdd�ZdBdeedeeddfdd�ZdA�fdd�Z	dBdeedeeddfdd�Zdejddfdd�Zde
ejejfdejddf�fdd �Z	!dCd"e
eee
eeffd#edd$fd%d&�Z 	dDd'ee	d(gdfde!e
deeffd)d*�Z"d"e
eefdee!dfd+d,�Z#d"e
deefdee!dfd-d.�Z$dEd0eddfd1d2�Z%d0eddfd3d4�Z&d0eddfd5d6�Z'de(fd7d8�Z)e*deefd9d:��Z+d;d<d=ee,d>ee-ddfd?d@�Z.�Z/S)F�WebSocketClientConnectionz�WebSocket client connection.

    This class should not be instantiated directly; use the
    `websocket_connect` function instead.
    Nr^�on_message_callbackrWrTrUrVr��resolverr%c	
s�t�|_td�|_t�t�d��|_||_	d|_
d|_t||||d�|_
|j�d�\}	}
}ddd�|	}	|	|
||_|j�dd	|jd
d��|durSd�|�|jd
<|dur\d|jd<d|_t|d�|_t��d|dd�|jd|jdd�dS)Nr<rrX�:�http�https)�ws�wssrirgr�)rgrozSec-WebSocket-Keyrsrpr7z*permessage-deflate; client_max_window_bitsr>F)r�cSr&r'r(r(r(r(r+rn~sz4WebSocketClientConnection.__init__.<locals>.<lambda>i@i)r�connect_futurer�
read_queuer2r3rdrer��_on_message_callbackr;r<rSr��url�	partitionrvr1�join�follow_redirectsr�
tcp_clientrarY�_on_http_response)r*r^r�rWrTrUrVr�r��scheme�sep�restrdr(r+rYHsN
�����z"WebSocketClientConnection.__init__cCs|jdur
t�dt�dSdS)Nz"Unclosed WebSocketClientConnection)�protocol�warnings�warn�ResourceWarningr�r(r(r+�__del__�s
�z!WebSocketClientConnection.__del__r�r�cCs&|jdur|j�||�d|_dSdS)z�Closes the websocket connection.

        ``code`` and ``reason`` are documented under
        `WebSocketHandler.close`.

        .. versionadded:: 3.2

        .. versionchanged:: 4.0

           Added the ``code`` and ``reason`` arguments.
        N)r�r�r�r(r(r+r��s

�zWebSocketClientConnection.closecs:|j��s|j�t��|�d�|j��t���dSr')	r��done�
set_exceptionr
�_on_messager�r�rar�r�rdr(r+r��s



z-WebSocketClientConnection.on_connection_closer;r<cCr�r'r�r=r(r(r+r>�r�z0WebSocketClientConnection.on_ws_connection_close�responsecCs:|j��s|jr|j�|j�dS|j�td��dSdS)NzNon-websocket response)r�r��errorr�rM)r*r�r(r(r+r��s
��z+WebSocketClientConnection._on_http_response�
start_linervc�s��t|tj�s	J�|jdkrt��||�IdHdS|jdur)|j�|j�d|_||_	|�
�|_|j�|j
|j	�|j��|j_t���|jj�|j��d|_t|j|�dSr�)r�r
�ResponseStartLiner�ra�headers_received�_timeoutr�r�rvr~r�r�r�r�r�r�rr��add_callbackr�r��final_callbackrr�)r*r�rvrdr(r+r��s �



z*WebSocketClientConnection.headers_receivedFr?r�r�cCs"|jdur	td��|jj||d�S)aJSends a message to the WebSocket server.

        If the stream is closed, raises `WebSocketClosedError`.
        Returns a `.Future` which can be used for flow control.

        .. versionchanged:: 5.0
           Exception raised on a closed stream changed from `.StreamClosedError`
           to `WebSocketClosedError`.
        Nz!Client connection has been closedr�)r�rPr�r�r(r(r+r��s
z'WebSocketClientConnection.write_messager�zFuture[Union[None, str, bytes]]cCs*|j��}|dur|j�t�|�|�|S)axReads a message from the WebSocket server.

        If on_message_callback was specified at WebSocket
        initialization, this function will never return messages

        Returns a future whose result is the message, or None
        if the connection is closed.  If a callback argument
        is given it will be called with the future when it is
        ready.
        N)r�rwr�r�r)rq)r*r��	awaitabler(r(r+�read_message�s
z&WebSocketClientConnection.read_messagecCs
|�|�Sr')r�r@r(r(r+rA�s
z$WebSocketClientConnection.on_messagecCs |jr
|�|�dS|j�|�Sr')r�r��putr@r(r(r+r��s
z%WebSocketClientConnection._on_messager6r$cCs(t|�}|jdurt��|j�|�dS)a�Send ping frame to the remote end.

        The data argument allows a small amount of data (up to 125
        bytes) to be sent as a part of the ping message. Note that not
        all websocket implementations expose this data to
        applications.

        Consider using the ``ping_interval`` argument to
        `websocket_connect` instead of sending pings manually.

        .. versionadded:: 5.1

        N)rr�rPr�r)r(r(r+r�s
zWebSocketClientConnection.pingcCr&r'r(r)r(r(r+rCr-z!WebSocketClientConnection.on_pongcCr&r'r(r)r(r(r+rBr-z!WebSocketClientConnection.on_pingcCst|d|jd�S)NT)rr�)r�r�r�r(r(r+r~r�z0WebSocketClientConnection.get_websocket_protocolcCs|jjS)zOThe subprotocol selected by the server.

        .. versionadded:: 5.1
        )r�r�r�r(r(r+r� sz.WebSocketClientConnection.selected_subprotocolrDzOptional[Type[BaseException]]rErFcCs2|dusJ�|dusJ�tjd||||fd�dS)NzUncaught exception %sr&)rr�rGr(r(r+rH(sz'WebSocketClientConnection.log_exceptionr�rIr�r'r�)0r0r1r2rQr�rZr	�HTTPRequestrrrrJr3rrr[r4rrrYr�r�r�r>�HTTPResponser�r
�RequestStartLiner�r�r�r�r�rr�rAr�r�rCrBr�r~r�r�rKr!rHr�r(r(rdr+r�?s��������
�	�
�
> 	���
�	������
���
�"�

�	����r�r�r�z!Future[WebSocketClientConnection]�connect_timeoutr�rWrTrUrVr�r�z$Awaitable[WebSocketClientConnection]c

Cs�t|tj�r|dusJ�|}
t�|
j�|
_ntj||d�}
ttjt�|
tjj��}
t	|
|||||||	d�}|durBt
���|j
|�|j
S)aaClient-side websocket support.

    Takes a url and returns a Future whose result is a
    `WebSocketClientConnection`.

    ``compression_options`` is interpreted in the same way as the
    return value of `.WebSocketHandler.get_compression_options`.

    The connection supports two styles of operation. In the coroutine
    style, the application typically calls
    `~.WebSocketClientConnection.read_message` in a loop::

        conn = yield websocket_connect(url)
        while True:
            msg = yield conn.read_message()
            if msg is None: break
            # Do something with msg

    In the callback style, pass an ``on_message_callback`` to
    ``websocket_connect``. In both styles, a message of ``None``
    indicates that the connection has been closed.

    ``subprotocols`` may be a list of strings specifying proposed
    subprotocols. The selected protocol may be found on the
    ``selected_subprotocol`` attribute of the connection object
    when the connection is complete.

    .. versionchanged:: 3.2
       Also accepts ``HTTPRequest`` objects in place of urls.

    .. versionchanged:: 4.1
       Added ``compression_options`` and ``on_message_callback``.

    .. versionchanged:: 4.5
       Added the ``ping_interval``, ``ping_timeout``, and ``max_message_size``
       arguments, which have the same meaning as in `WebSocketHandler`.

    .. versionchanged:: 5.0
       The ``io_loop`` argument (deprecated since version 4.1) has been removed.

    .. versionchanged:: 5.1
       Added the ``subprotocols`` argument.

    .. versionchanged:: 6.3
       Added the ``resolver`` argument.
    N)r�)r�rWrTrUrVr�r�)r�r	r�r
r�rvr�
_RequestProxy�	_DEFAULTSr�rr�r�r�)r�r�r�r�rWrTrUrVr�r�r^�connr(r(r+�websocket_connect3s,:��
r�)TrQr�r)r2r/rdr�rbr��urllib.parserr�r��tornado.concurrentrr�tornado.escaperrrrr	r
�tornado.iolooprr�tornado.iostreamr
r�tornado.logrr�tornado.netutilrr�tornado.queuesr�tornado.tcpclientr�tornado.utilr�typingrrrrrrrrrrr �typesr!�typing_extensionsr"r#r5r:rZr�rMrPrR�objectrSr��RequestHandlerr\r��ABCr�r�rr��_HTTPConnectionr�rJr�r[r3r4r�r(r(r(r+�<module>s�
4
	`[0&-w���������
	�
��