
    Mh                     J   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddlZddlZddlmZmZ ddlmZmZmZ ddl
mZmZmZ ddlmZ ddlmZmZ dd	lmZm Z  dd
l!m"Z" ddl
m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 e+r0ddl8m9Z9  G d de9          Z: G d de9          Z; G d de9          Z<dZ= ej>        ej?                  Z@ G d deA          ZB G d deB          ZC G d deA          ZD G d d           ZE G d! d"e
jF        jG                  ZHd#e-d$e-d%dfd&ZI G d' d(ejJ                  ZK G d) d*          ZL G d+ d,          ZM G d- d.eK          ZN G d/ d0e#jO                  ZPdddddde=ddf	d1e0eQejR        f         d2e.e3d3gdf                  d4e.eS         d5e.e3e0deQeTf         gdf                  d6e.e/eQe-f                  d7e.eS         d8e.eS         d9eUd:e.e1eQ                  d;e.e"         d%d<fd=ZVdS )>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)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                   .    e Zd ZdedefdZdedefdZdS )_Compressordatareturnc                     d S N selfr%   s     Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/tornado/websocket.pycompressz_Compressor.compress=       D    modec                     d S r(   r)   )r+   r0   s     r,   flushz_Compressor.flush@   r.   r/   N)__name__
__module____qualname__bytesr-   intr2   r)   r/   r,   r$   r$   <   sX        	 	5 	 	 	 		c 	e 	 	 	 	 	 	r/   r$   c                   &    e Zd ZdZdededefdZdS )_Decompressorr/   r%   
max_lengthr&   c                     d S r(   r)   )r+   r%   r:   s      r,   
decompressz_Decompressor.decompressF   r.   r/   N)r3   r4   r5   unconsumed_tailr6   r7   r<   r)   r/   r,   r9   r9   C   sB        	5 	c 	e 	 	 	 	 	 	r/   r9   c                       e Zd Z	 ddee         dee         ddfdZdeeef         ded         fdZ	d	eddfd
Z
d	eddfdZdeee                  dee         dee         ddfdZdS )_WebSocketDelegateN
close_codeclose_reasonr&   c                     d S r(   r)   r+   r@   rA   s      r,   on_ws_connection_closez)_WebSocketDelegate.on_ws_connection_closeM   s	     Dr/   messagezAwaitable[None]c                     d S r(   r)   r+   rE   s     r,   
on_messagez_WebSocketDelegate.on_messageR   r.   r/   r%   c                     d S r(   r)   r*   s     r,   on_pingz_WebSocketDelegate.on_pingU   r.   r/   c                     d S r(   r)   r*   s     r,   on_pongz_WebSocketDelegate.on_pongX   r.   r/   typvaluetbc                     d S r(   r)   r+   rM   rN   rO   s       r,   log_exceptionz _WebSocketDelegate.log_exception[   s	     Dr/   NN)r3   r4   r5   r   r7   strrD   r   r6   rH   rJ   rL   r    BaseExceptionr!   rR   r)   r/   r,   r?   r?   I   s       
 SW	 	&sm	BJ3-		 	 	 	
	eCJ&7 	HEV<W 	 	 	 		 	$ 	 	 	 		 	$ 	 	 	 		$}-.	 M*	 '		
 	 	 	 	 	 	r/   r?   i   c                       e Zd ZdS )WebSocketErrorNr3   r4   r5   r)   r/   r,   rW   rW   j           Dr/   rW   c                       e Zd ZdZdS )WebSocketClosedErrorzLRaised by operations on a closed connection.

    .. versionadded:: 3.2
    N)r3   r4   r5   __doc__r)   r/   r,   r[   r[   n   s         
 	Dr/   r[   c                       e Zd ZdS )_DecompressTooLargeErrorNrX   r)   r/   r,   r^   r^   w   rY   r/   r^   c                   h    e Zd Zddedfdee         dee         dedeeee	f                  ddf
dZ
dS )_WebSocketParamsNping_intervalping_timeoutmax_message_sizecompression_optionsr&   c                 >    || _         || _        || _        || _        d S r(   ra   rb   rc   rd   )r+   ra   rb   rc   rd   s        r,   __init__z_WebSocketParams.__init__|   s*     +( 0#6   r/   )r3   r4   r5   _default_max_message_sizer   floatr7   r   rT   r   rg   r)   r/   r,   r`   r`   {   s         *.(, 98<
7 
7
7 uo
7 	
7
 &d38n5
7 

7 
7 
7 
7 
7 
7r/   r`   c                       e Zd ZdZdej        j        dej        de	ddf fdZ
de	de	ddfd	Zedee         fd
            Zedee         fd            Zedefd            Z	 d-deeeeee	f         f         deddfdZdee         dee         fdZedee         fd            Zdeeee	f                  fdZdededeed                  fdZdeeef         deed                  fdZd.deeef         ddfdZ deddfdZ!deddfdZ"d/dZ#d0dee         dee         ddfd Z$d!edefd"Z%d#eddfd$Z&d/d%Z'	 d0d&ee         d'ee         ddfd(Z(d/ fd)Z)ded*         fd+Z*de+fd,Z, xZ-S )1WebSocketHandleraN  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")

    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``.
    Both settings are in seconds; floating point values are allowed.
    The default timeout is equal to the interval.

    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&   Nc                 v     t                      j        ||fi | d | _        d | _        d | _        d| _        d S NF)superrg   ws_connectionr@   rA   _on_close_called)r+   rl   rm   rn   	__class__s       r,   rg   zWebSocketHandler.__init__   sK     	g88888!  %r/   argsc                 T  K   || _         || _        | j        j                            dd                                          dk    rB|                     d           d}|                     |           t          j	        |           d S | j        j        }t          d |                    dd                              d                    }d	|vrB|                     d           d
}|                     |           t          j	        |           d S d| j        j        v r | j        j                            d          }n | j        j                            dd           }|W|                     |          sB|                     d           d}|                     |           t          j	        |           d S |                                 | _        | j        r"| j                            |            d {V  d S |                     dd           |                     dd           d S )NUpgrade 	websocket  z"Can "Upgrade" only to "WebSocket".c                 N    |                                                                  S r(   )striplower)ss    r,   <lambda>z&WebSocketHandler.get.<locals>.<lambda>   s    aggiioo'' r/   
Connection,upgradez"Connection" must be "Upgrade".OriginzSec-Websocket-Origini  z#Cross origin websockets not allowedi  zUpgrade RequiredSec-WebSocket-Versionz7, 8, 13)	open_argsopen_kwargsrm   headersgetr}   
set_statusfinishr   debugmapsplitcheck_originget_websocket_protocolrr   accept_connection
set_header)r+   ru   rn   log_msgr   
connectionorigins          r,   r   zWebSocketHandler.get   s*     ! <##Ir2288::kIIOOC   :GKK   M'"""F
 ,&''\2)F)F)L)LS)Q)Q
 

 J&&OOC   7GKK   M'"""F t|+++\)--h77FF\)--.DdKKF
 d&7&7&?&?OOC   ;GKK   M'"""F!88:: 	A$66t<<<<<<<<<<<OOC!3444OO3Z@@@@@r/   c                 8    | j                             dd          S )a  The interval for sending websocket pings.

        If this is non-zero, the websocket will send a ping every
        ping_interval seconds.
        The client will respond with a "pong". The connection can be configured
        to timeout on late pong delivery using ``websocket_ping_timeout``.

        Set ``websocket_ping_interval = 0`` to disable pings.

        Default: ``0``
        websocket_ping_intervalNsettingsr   r+   s    r,   ra   zWebSocketHandler.ping_interval  s     }  !:DAAAr/   c                 8    | j                             dd          S )a-  Timeout if no pong is received in this many seconds.

        To be used in combination with ``websocket_ping_interval > 0``.
        If a ping response (a "pong") is not received within
        ``websocket_ping_timeout`` seconds, then the websocket connection
        will be closed.

        This can help to clean up clients which have disconnected without
        cleanly closing the websocket connection.

        Note, the ping timeout cannot be longer than the ping interval.

        Set ``websocket_ping_timeout = 0`` to disable the ping timeout.

        Default: equal to the ``ping_interval``.

        .. versionchanged:: 6.5.0
           Default changed from the max of 3 pings or 30 seconds.
           The ping timeout can no longer be configured longer than the
           ping interval.
        websocket_ping_timeoutNr   r   s    r,   rb   zWebSocketHandler.ping_timeout)  s    . }  !94@@@r/   c                 B    | j                             dt                    S )zMaximum 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   r   rh   r   s    r,   rc   z!WebSocketHandler.max_message_sizeB  s$     }  (*C
 
 	
r/   FrE   binaryFuture[None]c                     | j         | j                                         rt                      t          |t                    rt
          j                            |          }| j                             ||          S )aH  Sends 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   )	rr   
is_closingr[   
isinstancedicttornadoescapejson_encodewrite_messager+   rE   r   s      r,   r   zWebSocketHandler.write_messageO  sp    0 %);)F)F)H)H%&(((gt$$ 	:n0099G!///GGGr/   subprotocolsc                     dS )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   s     r,   select_subprotocolz#WebSocketHandler.select_subprotocolm  s	    . tr/   c                 ,    | j         J | j         j        S )zYThe subprotocol returned by `select_subprotocol`.

        .. versionadded:: 5.1
        )rr   selected_subprotocolr   s    r,   r   z%WebSocketHandler.selected_subprotocol  s     !---!66r/   c                     dS )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 detail here:
         https://docs.python.org/3.13/library/zlib.html#zlib.compressobj

        .. versionadded:: 4.1

        .. versionchanged:: 4.5

           Added ``compression_level`` and ``mem_level``.
        Nr)   r   s    r,   get_compression_optionsz(WebSocketHandler.get_compression_options  s	    , tr/   c                     dS )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+   ru   rn   s      r,   openzWebSocketHandler.open  s	     	r/   c                     t           )zHandle incoming messages on the WebSocket

        This method must be overridden.

        .. versionchanged:: 4.5

           ``on_message`` can be a coroutine.
        NotImplementedErrorrG   s     r,   rH   zWebSocketHandler.on_message  s
     "!r/   r/   r%   c                     t          |          }| j        | j                                        rt                      | j                            |           dS )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)r   rr   r   r[   
write_pingr*   s     r,   pingzWebSocketHandler.ping  sU      Dzz%);)F)F)H)H%&(((%%d+++++r/   c                     dS )z6Invoked when the response to a ping frame is received.Nr)   r*   s     r,   rL   zWebSocketHandler.on_pong      r/   c                     dS )z*Invoked when the a ping frame is received.Nr)   r*   s     r,   rJ   zWebSocketHandler.on_ping  r   r/   c                     dS )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   s    r,   on_closezWebSocketHandler.on_close  s	     	r/   codereasonc                 \    | j         r$| j                             ||           d| _         dS dS )aC  Closes 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)rr   closer+   r   r   s      r,   r   zWebSocketHandler.close  s@       	&$$T6222!%D	& 	&r/   r   c                     t          |          }|j        }|                                }| j        j                            d          }||k    S )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   netlocr}   rm   r   r   )r+   r   parsed_originhosts       r,   r   zWebSocketHandler.check_origin   sK    ^ !((%|#''// ~r/   rN   c                 L    | j         J | j                             |           dS )a1  Set 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)rr   set_nodelayr+   rN   s     r,   r   zWebSocketHandler.set_nodelay8  s0     !---&&u-----r/   c                     | j         r | j                                          d | _         | j        s1d| _        |                                  |                                  d S d S )NT)rr   on_connection_closers   r   _break_cyclesr   s    r,   r   z$WebSocketHandler.on_connection_closeI  sl     	&22444!%D$ 	!$(D!MMOOO     	! 	!r/   r@   rA   c                 J    || _         || _        |                                  d S r(   r@   rA   r   rC   s      r,   rD   z'WebSocketHandler.on_ws_connection_closeR  +     %(  """""r/   c                     |                                  dk    s| j        r"t                                                       d S d S Ne   )
get_statusrs   rq   r   r+   rt   s    r,   r   zWebSocketHandler._break_cyclesY  sF     ??##t'<#GG!!##### $#r/   WebSocketProtocolc                     | j         j                            d          }|dv rEt          | j        | j        | j        |                                           }t          | d|          S d S )Nr   )7813rf   F)	rm   r   r   r`   ra   rb   rc   r   WebSocketProtocol13)r+   websocket_versionparamss      r,   r   z'WebSocketHandler.get_websocket_protocolb  sv     L0445LMM 000%"0!.!%!6$($@$@$B$B	  F 'tUF;;;tr/   c                 `    dD ]}t          | |t                     |                                 S )N)writeredirectr   
set_cookier   r2   r   )setattr#_raise_not_supported_for_websocketsdetach)r+   methods     r,   _detach_streamzWebSocketHandler._detach_streamn  s;    
 		G 		GF D&"EFFFF{{}}r/   Fr/   r&   NrS   ).r3   r4   r5   r\   r   webApplicationr   HTTPServerRequestr   rg   r   propertyr   ri   ra   rb   r7   rc   r   r6   rT   r   boolr   r   r   r   r   r   r   rH   r   rL   rJ   r   r   r   r   r   rD   r   r   r   r   __classcell__rt   s   @r,   rk   rk      s       N N`
&[,
& +
& 	
&
 

& 
& 
& 
& 
& 
&2As 2Ac 2Ad 2A 2A 2A 2Ah Bx B B B XB Ahuo A A A XA0 

# 

 

 

 X

 JOH HUCc3h78HBFH	H H H H<tCy Xc]    2 7hsm 7 7 7 X7$sCx.)A    0#  )D/1J     	"%U
"3 	"48Q 	" 	" 	" 	", ,sEz* ,T , , , ,*E d    E d       & &(3- & &QU & & & &(63 64 6 6 6 6p. .$ . . . ."! ! ! ! OS# #"3-#>Fsm#	# # # #$ $ $ $ $ $
1D(E 
 
 
 
        r/   rk   ru   rn   r&   c                       t          d          )Nz$Method not supported for Web Sockets)RuntimeError)ru   rn   s     r,   r   r   }  s    
=
>
>>r/   c            	       P   e Zd ZdZd"dZdeded	edd
fdZd#dZd#dZ	e
j        d$dee         dee         ddfd            Ze
j        defd            Ze
j        deddfd            Ze
j        	 d%deeeeeef         f         deddfd            Zee
j        dee         fd                        Ze
j        deddfd            Ze
j        deeef         dej        ddfd            Ze
j        d#d            Ze
j        d#d            Ze
j        d eddfd!            Z dS )&r   z+Base class for WebSocket protocol versions.handlerr?   r&   Nc                 >    || _         d | _        d| _        d| _        d S rp   )r   streamclient_terminatedserver_terminatedr+   r   s     r,   rg   zWebSocketProtocol.__init__  s&    !&!&r/   callbackru   rn   zOptional[Future[Any]]c                 "   	  ||i |}|>t          j        |          }| j        J | j        j                            |d            |S # t
          $ r8  | j        j        t          j	                      | 
                                 Y dS w xY w)zRuns the given callback with exception handling.

        If the callback is a coroutine, returns its Future. On error, aborts the
        websocket connection and returns None.
        Nc                 *    |                                  S r(   )result)fs    r,   r   z1WebSocketProtocol._run_callback.<locals>.<lambda>  s     r/   )r	   convert_yieldedr   io_loop
add_future	Exceptionr   rR   sysexc_info_abort)r+   r   ru   rn   r   s        r,   _run_callbackzWebSocketProtocol._run_callback  s    	Xt.v..F !,V44{...#..v7K7KLLLM  	 	 	&DL&77KKMMM44	s   A >BBc                 .    |                                   d S r(   )r  r   s    r,   r   z%WebSocketProtocol.on_connection_close  s    r/   c                     d| _         d| _        | j        | j                                         |                                  dS )z?Instantly aborts the WebSocket connection by closing the socketTN)r   r   r   r   r   s    r,   r  zWebSocketProtocol._abort  s?    !%!%;"K

r/   r   r   c                     t                      r(   r   r   s      r,   r   zWebSocketProtocol.close      !###r/   c                     t                      r(   r   r   s    r,   r   zWebSocketProtocol.is_closing  r
  r/   c                 "   K   t                      r(   r   r   s     r,   r   z#WebSocketProtocol.accept_connection        !###r/   FrE   r   r   c                     t                      r(   r   r   s      r,   r   zWebSocketProtocol.write_message       "###r/   c                     t                      r(   r   r   s    r,   r   z&WebSocketProtocol.selected_subprotocol  s     "###r/   r%   c                     t                      r(   r   r*   s     r,   r   zWebSocketProtocol.write_ping  r
  r/   keyr   c                     t                      r(   r   )r+   r  r   s      r,   _process_server_headersz)WebSocketProtocol._process_server_headers  r  r/   c                     t                      r(   r   r   s    r,   start_pingingzWebSocketProtocol.start_pinging  r
  r/   c                 "   K   t                      r(   r   r   s    r,   _receive_frame_loopz%WebSocketProtocol._receive_frame_loop  r  r/   xc                     t                      r(   r   r+   r  s     r,   r   zWebSocketProtocol.set_nodelay  r
  r/   )r   r?   r&   Nr   rS   r   )!r3   r4   r5   r\   rg   r   r   r  r   r  abcabstractmethodr   r7   rT   r   r   r   rk   r   r   r6   r   r   r   r   r   r   HTTPHeadersr  r  r  r   r)   r/   r,   r   r     s       55' ' ' ' ),8;	    *       	$ $(3- $ $QU $ $ $ $ 	$D $ $ $ $ 	$/? $D $ $ $ $ 	IN$ $S%c3h78$BF$	$ $ $ $
 $hsm $ $ $  X$ 	$u $ $ $ $ $ 	$e$$/7/C$	$ $ $ $
 	$ $ $ $ 	$ $ $ $ 	$T $d $ $ $ $ $ $r/   r   c            
       j    e Zd Z	 ddedee         deeeef                  ddfdZ	ddZ
d	edefd
ZdS )_PerMessageDeflateCompressorN
persistent	max_wbitsrd   r&   c                 t   |t           j        }d|cxk    rt           j        k    sn t          d|t           j                  || _        |d|vrt          j        j        j        | _        n|d         | _        |d|vrd| _	        n|d         | _	        |r| 
                                | _        d S d | _        d S )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"  rd   s       r,   rg   z%_PerMessageDeflateCompressor.__init__  s     IY0000$.0000@  
 $  '"*===&-k&E&PD##&9:M&ND#&+=P*P*PDOO1+>DO 	$#6688D#Dr/   r$   c                 d    t          j        | j        t           j        | j         | j                  S r(   )r(  compressobjr.  DEFLATEDr+  r/  r   s    r,   r0  z/_PerMessageDeflateCompressor._create_compressor   s,    #T]T_4Ddo
 
 	
r/   r%   c                     | j         p|                                 }|                    |          |                    t          j                  z   }|                    d          sJ |d d         S )N     )r1  r0  r-   r2   r(  Z_SYNC_FLUSHendswith)r+   r%   
compressors      r,   r-   z%_PerMessageDeflateCompressor.compress  sk    %B)@)@)B)B
""4((:+;+;D<M+N+NN}}011111CRCyr/   r(   )r&   r$   )r3   r4   r5   r   r   r7   r   rT   r   rg   r0  r6   r-   r)   r/   r,   r   r     s        
 9=	!$ !$!$ C=!$ &d38n5	!$
 
!$ !$ !$ !$F
 
 
 

U u      r/   r   c                   n    e Zd Z	 ddedee         dedeeeef                  ddf
dZ	dd	Z
d
edefdZdS )_PerMessageDeflateDecompressorNr!  r"  rc   rd   r&   c                     || _         |t          j        }d|cxk    rt          j        k    sn t          d|t          j                  || _        |r|                                 | _        d S d | _        d S )Nr$  r%  )_max_message_sizer(  r)  r*  r+  _create_decompressor_decompressor)r+   r!  r"  rc   rd   s        r,   rg   z'_PerMessageDeflateDecompressor.__init__  s     "2IY0000$.0000@  
 $ 	&))++  "&Dr/   r9   c                 6    t          j        | j                   S r(   )r(  decompressobjr+  r   s    r,   r?  z3_PerMessageDeflateDecompressor._create_decompressor%  s    !4?"2333r/   r%   c                     | j         p|                                 }|                    |dz   | j                  }|j        rt                      |S )Nr6  )r@  r?  r<   r>  r=   r^   )r+   r%   decompressorr   s       r,   r<   z)_PerMessageDeflateDecompressor.decompress(  s\    )HT-F-F-H-H((&&(>
 
 ' 	-*,,,r/   r(   )r&   r9   )r3   r4   r5   r   r   r7   r   rT   r   rg   r?  r6   r<   r)   r/   r,   r<  r<    s         9=& && C=& 	&
 &d38n5& 
& & & &04 4 4 4u       r/   r<  c                   d   e Zd ZdZdZdZdZdZeez  ez  ZdZ	dZ
dd	d
ededdfdZedee         fd            Zej        dee         ddfd            ZdeddfdZdeddfdZedeeef         defd            ZdedefdZdeddfdZdej        deeee eef         f                  fdZ!deeef         dej        ddfdZ"	 d;dede ee#f         dee ee#f                  de ee#f         fdZ$	 d;dede ee#f         dee ee#f                  ddfdZ%	 d<d ed!e&d"ed#e&dd$f
d%Z'	 d=d'eeee ee#f         f         d(edd$fd)Z(d"eddfd*Z)d>d+Z*d,e&defd-Z+d>d.Z,d!e&d"edd/fd0Z-d?d1ee&         d2ee         ddfd3Z.defd4Z/d5eddfd6Z0ede1fd7            Z2ede1fd8            Z3d>d9Z4d>d:Z5dS )@r   zImplementation 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&   c                 z   t                               | |           || _        || _        d| _        d | _        d | _        d | _        d | _        d | _	        d | _
        d | _        |j        | _        d | _        d | _        d | _        d| _        d| _        d| _        d| _        d| _        d | _        d | _        d | _        d S )NFr   )r   rg   rK  r   _final_frame_frame_opcode_masked_frame_frame_mask_frame_length_fragmented_message_buffer_fragmented_message_opcode_waitingrd   _compression_optionsr@  r1  _frame_compressed_message_bytes_in_message_bytes_out_wire_bytes_in_wire_bytes_out_received_pongr@   rA   _ping_coroutine)r+   r   rK  r   s       r,   rg   zWebSocketProtocol13.__init__C  s     	""4111*!!!!*.'*.'$*$>!!!% "#"#   # #r/   c                     | j         S r(   _selected_subprotocolr   s    r,   r   z(WebSocketProtocol13.selected_subprotocolg  s    ))r/   rN   c                     || _         d S r(   r^  r   s     r,   r   z(WebSocketProtocol13.selected_subprotocolk  s    %*"""r/   c                   K   	 |                      |           nQ# t          $ rD |                    d           d}|                    |           t	          j        |           Y d S w xY w	 |                     |           d {V  d S # t          j        $ r | 	                                 Y d S t          $ r. t	          j        dd           | 	                                 Y d S w xY w)Nrz   !Missing/Invalid WebSocket headersz$Malformed WebSocket request receivedTr  )
_handle_websocket_headersr*  r   r   r   r   _accept_connectionasyncioCancelledErrorr  )r+   r   r   s      r,   r   z%WebSocketProtocol13.accept_connectiono  s     	**73333 	 	 	s###9GNN7###M'"""FF		))'22222222222% 	 	 	KKMMMFF 	 	 	M@4PPPPKKMMMFF	s(    A
A('A(,B	 	#C&/3C&%C&c                 j    d}t          t          fd|                    st          d          dS )zVerifies all invariant- and required headers

        If a header is missing or have an incorrect value ValueError will be
        raised
        )r   Sec-Websocket-KeyzSec-Websocket-Versionc                 B    j         j                            |           S r(   )rm   r   r   )r   r   s    r,   r   z?WebSocketProtocol13._handle_websocket_headers.<locals>.<lambda>  s    !8!<!<Q!?!? r/   rb  N)allr   r*  )r+   r   fieldss    ` r,   rd  z-WebSocketProtocol13._handle_websocket_headers  sK     H3????HHII 	B@AAA	B 	Br/   r  c                     t          j                    }|                    t          |                      |                    d           t	          t          j        |                                                    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updater   r   base64	b64encodedigest)r  ro  s     r,   compute_accept_valuez(WebSocketProtocol13.compute_accept_value  s[    
 |~~DII;<<<&*4;;==99:::r/   c                     t                               t          t          |j        j                            d                              S )Nri  )r   rt  r   rT   rm   r   r   r   s     r,   _challenge_responsez'WebSocketProtocol13._challenge_response  s;    "77go-112EFFGG
 
 	
r/   c           	        K   |j         j                            d          }|r d |                    d          D             }ng }|                    |          | _        | j        r&| j        |v sJ |                    d| j                   |                     |j         j                  }|D ]}|d         dk    r{| j        t| 	                    d|d         | j                   d|d         v r|d         d         	|d         d= |                    d	t          j        d|d                               n|                    d
           |                    d           |                    dd           |                    dd           |                    d|                     |                     |                                 |                                | _        |                                  	  |j        |j        i |j        }|| d {V  n@# t,          $ r3  |j        t1          j                      |                                  Y d S w xY w|                                  d {V  d S )NSec-WebSocket-Protocolc                 6    g | ]}|                                 S r)   )r|   ).0r~   s     r,   
<listcomp>z:WebSocketProtocol13._accept_connection.<locals>.<listcomp>  s     MMM!AGGIIMMMr/   r   r   permessage-deflateserver   client_max_window_bitsSec-WebSocket-ExtensionszContent-Typer   rw   ry   r   zSec-WebSocket-Accept)rm   r   r   r   r   r   r   _parse_extensions_headerrU  _create_compressorsr   _encode_headerclear_headerr   rv  r   r   r   r  r   r   r   r  rR   r  r  r  r  )r+   r   subprotocol_headerr   
extensionsextopen_results          r,   re  z&WebSocketProtocol13._accept_connection  s     $_4889QRR 	MM/A/G/G/L/LMMMLLL$+$>$>|$L$L!$ 	T,<<<<79RSSS227?3JKK
 	 	C1v---$2K2W ((3q64;TUUU,A66A78@ A78"".+,@#a&II   ^,,,39k222<333143K3KG3T3TUUU,,..	&',(9QW=PQQK&!!!!!!!! 	 	 	!G!3<>>22KKMMMFF	
 &&(((((((((((s   9!H 9IIr   c                 t    |                     dd          }|rd |                    d          D             S g S )Nr  rx   c                 Z    g | ](}t          j        |                                          )S r)   )r   _parse_headerr|   )rz  es     r,   r{  z@WebSocketProtocol13._parse_extensions_header.<locals>.<listcomp>  s+    UUU!H*1779955UUUr/   r   )r   r   )r+   r   r  s      r,   r  z,WebSocketProtocol13._parse_extensions_header  sI     [[!;R@@
 	VUUz?O?OPS?T?TUUUU	r/   c                    |d                                          dk    sJ |d                                          dk    sJ |                     |          }|d         |k    sJ |                     |          }|D ]A}|d         dk    r$| j        |                     d	|d
                    2t          d|          |                    dd          | _        dS )zProcess the headers sent by the server to this client connection.

        'key' is the websocket handshake challenge/response key.
        rw   ry   r   r   zSec-Websocket-Acceptr   r|  Nclientr~  zunsupported extension %rrx  )r}   rt  r  rU  r  r*  r   r   )r+   r  r   acceptr  r  s         r,   r  z+WebSocketProtocol13._process_server_headers  s     y!''))[8888|$**,,	9999**3//-.&8888227;;
 	B 	BC1v---$2K2W((3q6:::: !;SAAA$+KK0H$$O$O!!!r/   sideagreed_parametersrd   c                     t          |dz   |v          }|                    |dz   d          }|t          j        |d<   nt	          |          |d<   ||d<   |S )zlConverts a websocket agreed_parameters set to keyword arguments
        for our compressor objects.
        _no_context_takeover)r!  _max_window_bitsNr"  rd   )r   r   r(  r)  r7   )r+   r  r  rd   optionswbits_headers         r,   _get_compressor_optionsz+WebSocketProtocol13._get_compressor_options  s{     55>OO
 
 
 ),,T4F-FMM#'>GK  #&|#4#4GK )<%&r/   c           	         h d}|D ]}||vrt          d|z            |dk    rdnd}t          di |                     |||          | _        t	          dd| j        j        i|                     |||          | _        d S )N>   r  server_max_window_bitsclient_no_context_takeoverserver_no_context_takeoverz$unsupported compression parameter %rr}  r  rc   r)   )r*  r   r  r1  r<  r   rc   r@  )r+   r  r  rd   allowed_keysr  
other_sides          r,   r  z'WebSocketProtocol13._create_compressors   s    
 
 
 % 	O 	OC,&& !G#!MNNN '"&("2"2XX
7 
 
**41BDWXX
 
 < 
 
![9
**-/B 
 
r/   r   finopcoder%   flagsr   c                 r   t          |          }|dz  r&|st          d          |dk    rt          d          |r| j        }nd}t          j        d||z  |z            }| j        rd}nd}|dk     r|t          j        d||z            z  }n?|d	k    r|t          j        d
d|z  |          z  }n|t          j        dd|z  |          z  }| j        r't          j        d          }	|	t          |	|          z   }||z  }| xj	        t          |          z  c_	        | j
                            |          S )Nr$  z$control frames may not be fragmented}   z/control frame payloads may not exceed 125 bytesr   BrF  ~   i  z!BHz!BQ      )lenr*  FINstructpackrK  osurandomr   rZ  r   r   )
r+   r  r  r%   r  data_lenfinbitframemask_bitmasks
             r,   _write_framez WebSocketProtocol13._write_frame  se    t99C< 	T  I !GHHH#~~ !RSSS 	XFFFC&5!899 	HHHc>>V[h&9:::EEV[hAAAEEV[hAAAE 	6:a==D/$555DE

*{  '''r/   FrE   r   c                 ,   |rd}nd}t          |t                    rt          j                            |          }t          j                            |          }t          |t                    sJ | xj        t          |          z  c_        d}| j	        r$| j	        
                    |          }|| j        z  }	 |                     d|||          n# t          $ r t                      w xY wd	fd}t          j         |                      S )
z9Sends the given message to the client of this Web Socket.   r~  r   T)r  r&   Nc                  V   K   	   d {V  d S # t           $ r t                      w xY wr(   )r   r[   )futs   r,   wrapperz2WebSocketProtocol13.write_message.<locals>.wrapperW  sH      -									$ - - -*,,,-s    (r   )r   r   r   r   r   r   r6   rX  r  r1  r-   RSV1r  r   r[   rf  ensure_future)r+   rE   r   r  r  r  r  s         @r,   r   z!WebSocketProtocol13.write_message<  s8     	FFFgt$$ 	:n0099G.%%g..'5)))))3w<</ 	&//88GTYE	)##D&'#GGCC  	) 	) 	)&(((	)	- 	- 	- 	- 	- 	- $WWYY///s   >C C1c                 b    t          |t                    sJ |                     dd|           dS )zSend ping frame.T	   N)r   r6   r  r*   s     r,   r   zWebSocketProtocol13.write_ping_  s6    $&&&&&$T*****r/   c                    K   	 | j         s!|                                  d {V  | j         !n$# t          $ r |                                  Y nw xY w| j                            | j        | j                   d S r(   )r   _receive_framer   r  r   rD   r@   rA   r   s    r,   r  z'WebSocketProtocol13._receive_frame_loopd  s      	, ,))+++++++++ , ,  	 	 	KKMMMMM	++DOT=NOOOOOs   (- AAnc                 j   K   | j                             |           d {V }| xj        |z  c_        |S r(   )r   
read_bytesrY  )r+   r  r%   s      r,   _read_byteszWebSocketProtocol13._read_bytesl  sF      [++A........q r/   c                   K   |                      d           d {V }t          j        d|          \  }}|| j        z  }|| j        z  }|| j        z  }|dz  }| j        -|dk    r't          || j        z            | _	        || j         z  }|r| 
                                 d S t          |dz            }|dz  }	|r|	dk    r| 
                                 d S |	dk     r|	| _        ny|	dk    r7|                      d           d {V }t          j        d|          d         }	n<|	dk    r6|                      d           d {V }t          j        d	|          d         }	|	}
| j        |
t          | j                  z  }
|
| j        j        k    r,|                     d
d           | 
                                 d S |r |                      d           d {V | _        |                      |	           d {V }|r| j        J t%          | j        |          }|r|s| 
                                 d S n|dk    r\| j        | 
                                 d S | j                            |           |r"| j        }t+          | j                  }d | _        n:| j        | 
                                 d S |s|| _        t-          |          | _        |r"|                     ||          }|| d {V  d S d S d S )Nr  BBr$  r   rF  r  r  z!Hz!Q  zmessage too bigr  )r  r  unpackr  RSV_MASKOPCODE_MASKr@  r   r  rV  r  rQ  rR  r  r   rc   r   rP  r   extendrS  r6   	bytearray_handle_message)r+   r%   headermask_payloadlenis_final_framereserved_bitsr  opcode_is_control	is_masked
payloadlennew_lenhandled_futures               r,   r  z"WebSocketProtocol13._receive_frameq  s     %%a(((((((("(-d";";$(*.$**"SL)fkk &*-$)*C%D%DD"diZ'M 	KKMMMF4/00	$t+
  	s!2!2KKMMMF!+D3))!,,,,,,,,DtT2215JJ3))!,,,,,,,,DtT2215J*6s4:;;;GT[111JJt.///KKMMMF  	9%)%5%5a%8%8888888D%%j11111111 	;#///"4#3T::D  	B "  q[[.6+224888 78T<==26/.:! B28/2;D/// 	%!11&$??N)$$$$$$$$$$	% 	%))r/   zOptional[Future[None]]c                    | j         rdS | j        r`| j        J 	 | j                            |          }n;# t          $ r. |                     dd           |                                  Y dS w xY w|dk    ry| xj        t          |          z  c_        	 |	                    d          }n%# t          $ r |                                  Y dS w xY w|                     | j        j        |          S |dk    r=| xj        t          |          z  c_        |                     | j        j        |          S |dk    rd| _         t          |          dk    r(t          j        d	|dd                   d
         | _        t          |          dk    rt#          |dd                   | _        |                     | j                   n|dk    r^	 |                     dd|           n$# t(          $ r |                                  Y nw xY w|                     | j        j        |           nA|dk    r'd| _        |                     | j        j        |          S |                                  dS )z>Execute on_message, returning its Future if it is a coroutine.Nr  z#message too big after decompressionr~  zutf-8r  r$  T>Hr   r  
   )r   rV  r@  r<   r^   r   r  rW  r  decodeUnicodeDecodeErrorr  r   rH   r  r  r@   r   rA   r  r   rJ   r[  rL   )r+   r  r%   decodeds       r,   r  z#WebSocketProtocol13._handle_message  s   ! 	4! 	%111)44T::+   

4!FGGGtt
 S==""c$ii/""++g..%   tt %%dl&=wGGGs]]""c$ii/""%%dl&=tDDDs]]%)D"4yyA~~"(-d2A2h"?"?"B4yy1}}$.tABBx$8$8!JJt''''s]]!!$T2222$   t|3T::::s]]"&D%%dl&:DAAAKKMMMts3   6 4A.-A.B+ +CCG$ $HHr   r   c                    | j         s| j                                        sq||d}|d}nt          j        d|          }||t          |          z  }	 |                     dd|           n$# t          $ r |                                  Y nw xY wd| _         | j	        rL| j
        +| j        j                            | j
                   d| _
        | j                                         nP| j
        I| j        j                            | j        j                                        dz   | j                  | _
        | j        r"| j                                         d| _        dS dS )z Closes the WebSocket connection.Ni  r/   r  Tr$     )r   r   closedr  r  r   r  r   r  r   rT  r   remove_timeoutr   add_timeouttimer\  cancel)r+   r   r   
close_datas       r,   r   zWebSocketProtocol13.close  s   % 	*;%%'' "<F$6D<!$JJ!'T4!8!8J%$v,,.J"%%dC<<<<( " " "KKMMMMM"%)D"! 
	}(#224=AAA $K]" !K/;;#((**Q. DM  	( '')))#'D   	( 	(s   A. .BBc                 P    | j                                         p| j        p| j        S )zReturn ``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   s    r,   r   zWebSocketProtocol13.is_closing  s(     {!!##Wt'=WAWWr/   r  c                 :    | j                             |           d S r(   )r   r   r  s     r,   r   zWebSocketProtocol13.set_nodelay   s    """""r/   c                 &    | j         j        }||S dS )Nr   )r   ra   )r+   intervals     r,   ra   z!WebSocketProtocol13.ping_interval#  s    ;,Oqr/   c           	          | j         j        }|H| j        r?|| j        k    r4t          t          j        d| d| j         d| j                    | j        S |S | j        S )NzThe websocket_ping_timeout (z5) cannot be longer than the websocket_ping_interval (z").
Setting websocket_ping_timeout=)r   rb   ra   de_dupe_gen_logloggingWARNING)r+   timeouts     r,   rb   z WebSocketProtocol13.ping_timeout*  s    +*! 	*g0B&B&B OM7 M M:>:LM M8<8JM M	   ))N!!r/   c                     | j         s8| j        dk    r/t          j        |                                           | _         dS dS dS )z9Start sending periodic pings to keep the connection aliver   N)r\  ra   rf  create_taskperiodic_pingr   s    r,   r  z!WebSocketProtocol13.start_pinging;  sY     $	M "Q&&#*#6t7I7I7K7K#L#LD   	M 	M '&r/   c                   K   | j         }| j        }t          j        |           d{V  	 d| _        t          j                                                    }|                     d           t          j        |           d{V  |dk    r| j        s| 	                    d           dS t          j        t          j                                                    |z
  |z              d{V  )zSend a ping and wait for a pong if ping_timeout is configured.

        Called periodically if the websocket_ping_interval is set and non-zero.
        NTFr/   r   zping timed out)r   )
ra   rb   rf  sleepr[  r   currentr  r   r   )r+   r  r  	ping_times       r,   r  z!WebSocketProtocol13.periodic_pingE  s     
 %#mH%%%%%%%%%	P"'D((--//IOOC    -((((((((( {{4#6{

"2
333 - 0 0 5 5 7 7) Ch NOOOOOOOOO	Pr/   r(   )r   r   r   rS   )6r3   r4   r5   r\   r  r  RSV2RSV3r  r  r   r   r`   rg   r   r   rT   r   setterrk   r   rd  staticmethodr   r6   rt  rv  re  r   r  r   r   r   r  r  r   r  r  r7   r  r   r   r  r  r  r  r   r   r   ri   ra   rb   r  r  r)   r/   r,   r   r   2  s         CDDDd{T!HKF!$%!$ !$ !	!$
 
!$ !$ !$ !$H *hsm * * * X*  +(3- +D + + + ! +/? D    (B1A Bd B B B B ;%U
"3 ; ; ; ; \;
+; 
 
 
 
 

1)0@ 1)T 1) 1) 1) 1)f+	eCc3h'(	)   Pe$P/7/CP	P P P P2 9=	   S> &d38n5	
 
c3h   0 9=	
 

  S>
 &d38n5	

 

 
 
 
8 AB( ((!$(,1(:=(	( ( ( (D JO!0 !0S%c3h78!0BF!0	!0 !0 !0 !0F+u + + + + +
P P P P3 5    
Q% Q% Q% Q%f1c 1 1;S 1 1 1 1f( ((3- ( (QU ( ( ( (@XD X X X X#T #d # # # # u    X "e " " " X" M M M MP P P P P Pr/   r   c                   R    e Zd ZdZdZddddeddfdej        dee	e
deef         gdf                  deeeef                  dee         dee         ded	eee                  d
ee         ddf fdZd0dZd1dee         dee         ddfdZd0 fdZ	 d1dee         dee         ddfdZdej        ddfdZde
ej        ej        f         dej        ddf fdZ	 d2de
eeeeef         f         deddfdZ 	 d3dee	d gdf                  de!e
deef                  fd!Z"de
eef         dee!d                  fd"Z#de
deef         dee!d                  fd#Z$d4d%eddfd&Z%d%eddfd'Z&d%eddfd(Z'de(fd)Z)e*dee         fd*            Z+d+d,d-ee,         d.ee-         ddfd/Z. xZ/S )5WebSocketClientConnectionzWebSocket client connection.

    This class should not be instantiated directly; use the
    `websocket_connect` function instead.
    Nrm   on_message_callbackrd   ra   rb   rc   r   resolverr&   c	           
         t                      | _        t          d          | _        t	          j        t          j        d                    | _        || _	        d | _
        d | _        t          ||||          | _        |j                            d          \  }	}
}ddd|	         }	|	|
z   |z   |_        |j                            dd	t%          | j                  d
d           |d                    |          |j        d<   |
d|j        d<   d|_        t+          |          | _        t/                                          d |d | j        d| j        dd           d S )Nr~  rI  rf   :httphttps)wswssry   rw   r   )rw   r   zSec-WebSocket-Keyr   r   rx  z*permessage-deflate; client_max_window_bitsr  F)r  c                      d S r(   r)   r)   r/   r,   r   z4WebSocketClientConnection.__init__.<locals>.<lambda>  s    D r/   i  @i   )r   connect_futurer   
read_queuerq  rr  r  r  r  _on_message_callbackr@   rA   r`   r   url	partitionr   rp  r   joinfollow_redirectsr   
tcp_clientrq   rg   _on_http_response)r+   rm   r  rd   ra   rb   rc   r   r  schemeseprestrt   s               r,   rg   z"WebSocketClientConnection.__init__j  s    %hh((#BJrNN33$7! &'%- 3	
 
 
 $K11#66Tw//7slT)&'%/%9%9)-	 	
 	
 	
 #8;8N8NGO45* = O67
 $) #X666L"O		
 		
 		
 		
 		
r/   c                 L    | j         t          j        dt                     d S d S )Nz"Unclosed WebSocketClientConnection)protocolwarningswarnResourceWarningr   s    r,   __del__z!WebSocketClientConnection.__del__  s-    =$ M>PPPPP %$r/   r   r   c                 \    | j         $| j                             ||           d| _         dS dS )zCloses 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   s      r,   r   zWebSocketClientConnection.close  s7     =$Mf--- DMMM %$r/   c                 "   | j                                         s&| j                             t                                 |                     d            | j                                         t                                                       d S r(   )	r  doneset_exceptionr   _on_messager  r   rq   r   r   s    r,   r   z-WebSocketClientConnection.on_connection_close  s}    "'')) 	C--.?.A.ABBB##%%%%%r/   r@   rA   c                 J    || _         || _        |                                  d S r(   r   rC   s      r,   rD   z0WebSocketClientConnection.on_ws_connection_close  r   r/   responsec                     | j                                         sQ|j        r!| j                             |j                   d S | j                             t	          d                     d S d S )NzNon-websocket response)r  r  errorr  rW   )r+   r  s     r,   r	  z+WebSocketClientConnection._on_http_response  s}    "'')) 	~ #11(.AAAAA#11"#;<<    		 	r/   
start_liner   c                   K   t          |t          j                  sJ |j        dk    r*t	                                          ||           d {V  d S | j        &| j                            | j                   d | _        || _	        | 
                                | _        | j                            | j        | j	                   | j                                        | j        _        t#          j                                        | j        j                   | j                                         d | _        t/          | j        |            d S r   )r   r   ResponseStartLiner   rq   headers_received_timeoutr   r  r   r   r  r  r  r   r   r   r   r  add_callbackr  r  final_callbackr   r  )r+   r  r   rt   s      r,   r  z*WebSocketClientConnection.headers_received  s-     
 *h&@AAAAA?c!!''**:w?????????F=$L''666 DM3355--dhEEE#5577%%dm&GHHH##%%% #*4+>EEEEEr/   FrE   r   r   c                 f    | j         t          d          | j                             ||          S )aJ  Sends 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  r[   r   r   s      r,   r   z'WebSocketClientConnection.write_message  s6     = &'JKKK}**76*BBBr/   r   zFuture[Union[None, str, bytes]]c                     | j                                         }|-| j                            t	          j        |          |           |S )ax  Reads 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.
        )r  r   r   r  rf  r  )r+   r   	awaitables      r,   read_messagez&WebSocketClientConnection.read_message  sF     O''))	L##G$9)$D$DhOOOr/   c                 ,    |                      |          S r(   )r  rG   s     r,   rH   z$WebSocketClientConnection.on_message  s    (((r/   c                 r    | j         r|                      |           d S | j                            |          S r(   )r  r  putrG   s     r,   r  z%WebSocketClientConnection._on_message  s>     $ 	0%%g...4?&&w///r/   r/   r%   c                     t          |          }| j        t                      | j                            |           dS )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)r   r  r[   r   r*   s     r,   r   zWebSocketClientConnection.ping&  s?     Dzz= &(((  &&&&&r/   c                     d S r(   r)   r*   s     r,   rL   z!WebSocketClientConnection.on_pong9      r/   c                     d S r(   r)   r*   s     r,   rJ   z!WebSocketClientConnection.on_ping<  r,  r/   c                 0    t          | d| j                  S )NT)rK  r   )r   r   r   s    r,   r   z0WebSocketClientConnection.get_websocket_protocol?  s    "4tDKPPPPr/   c                     | j         j        S )zOThe subprotocol selected by the server.

        .. versionadded:: 5.1
        )r  r   r   s    r,   r   z.WebSocketClientConnection.selected_subprotocolB  s     }11r/   rM   zOptional[Type[BaseException]]rN   rO   c                 J    |J |J t          j        d||||f           d S )NzUncaught exception %src  )r   r  rQ   s       r,   rR   z'WebSocketClientConnection.log_exceptionJ  s>        -uUB?OPPPPPPr/   r   rS   r   r(   r   )0r3   r4   r5   r\   r  rh   r
   HTTPRequestr   r   r   rT   r6   r   r   ri   r7   r   r   rg   r  r   r   rD   HTTPResponser	  r   RequestStartLiner  r  r  r   r   r   r&  rH   r  r   rL   rJ   r   r   r   r   rU   r!   rR   r   r   s   @r,   r  r  a  sO         H
 TX8<)-(, 9,0'+<
 <
'<
 &hdC6F0G/H$/N&OP<
 &d38n5	<

  <
 uo<
 <
 tCy)<
 8$<
 
<
 <
 <
 <
 <
 <
|Q Q Q Q! !(3- ! !QU ! ! ! ! & & & & & & OS# #"3-#>Fsm#	# # # #**A d    F(3X5OOPF %F 
	F F F F F F> JOC CS%c3h78CBFC	C C C C$ SW 8%F$G$MNO 
5sE)*	+   ()%U
"3 )48Q ) ) ) )0T3-.0	)D/	"0 0 0 0' ' ' ' ' ' '&E d    E d    Q(9 Q Q Q Q 2hsm 2 2 2 X2Q,Q &Q ]#	Q
 
Q Q Q Q Q Q Q Qr/   r  r  r   z!Future[WebSocketClientConnection]connect_timeoutr  rd   ra   rb   rc   r   r  z$Awaitable[WebSocketClientConnection]c
           
         t          | t          j                  r%|J | }
t          j        |
j                  |
_        nt          j        | |          }
t          t          j        t          j        |
t          j        j                            }
t          |
|||||||	          }|Ht          j        dt          d           t          j                                        |j        |           |j        S )aG  Client-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.

    .. deprecated:: 6.5
       The ``callback`` argument is deprecated and will be removed in Tornado 7.0.
       Use the returned Future instead. Note that ``on_message_callback`` is not
       deprecated and may still be used.
    N)r4  )r  rd   ra   rb   rc   r   r  zZThe callback argument to websocket_connect is deprecated. Use the returned Future instead.r  )
stacklevel)r   r
   r1  r   r  r   r   _RequestProxy	_DEFAULTSr  r  r  DeprecationWarningr   r  r  r  )r  r   r4  r  rd   ra   rb   rc   r   r  rm   conns               r,   websocket_connectr;  U  s
   ~ #z-.. O&&& #.w??(oNNN **@*JKK G %//#!)!	 	 	D /		
 	
 	
 	
 	##D$7BBBr/   )Wr\   r  rf  rq  	functoolsrn  r  r  r  r  r   urllib.parser   r  r(  tornado.concurrentr   r   tornado.escaper   r   r   r	   r
   r   tornado.ioloopr   tornado.iostreamr   r   tornado.logr   r   tornado.netutilr   r   tornado.queuesr   tornado.tcpclientr   tornado.utilr   typingr   r   r   r   r   r   r   r   r   r   r    typesr!   typing_extensionsr"   r$   r9   r?   rh   	lru_cachelogr  r  rW   r[   r^   r`   r   RequestHandlerrk   r   ABCr   r   r<  r   _HTTPConnectionr  rT   r1  ri   r6   r7   r;  r)   r/   r,   <module>rO     s    


         				 



   ! ! ! ! ! !   I I I I I I I I 7 7 7 7 7 7 7 7 7 7 - - - - - - - - - - ! ! ! ! ! ! 8 8 8 8 8 8 8 8 ( ( ( ( ( ( ( ( $ $ $ $ $ $ % % % % % %             ' ' ' ' ' ' ( ( ( ( ( (                                 *******    h              X   6 -  &)%gk22	 	 	 	 	Y 	 	 		 	 	 	 	> 	 	 		 	 	 	 	y 	 	 	7 7 7 7 7 7 7 7q q q q qw{1 q q qh?s ?c ?d ? ? ? ?X$ X$ X$ X$ X$ X$ X$ X$v- - - - - - - -`# # # # # # # #LlP lP lP lP lP+ lP lP lP^qQ qQ qQ qQ qQ 1 A qQ qQ qQl QU'+OS48%)$(5(,#'] ]	sJ**	+]x!D Et KLM] e_] "(E$U2B,C+Dd+J"KL	]
 "$sCx.1] E?] 5/] ] 49%] x ] ,] ] ] ] ] ]r/   