
    h?                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKJr  SSKrSSKrSSKJrJr  SSKJrJrJr  SSK
JrJrJr  SSKJr  SSKJrJr  SS	KJrJ r   SS
K!J"r"  SSK
J#r#  SSK$J%r%  SSK&J'r'  SSK(J)r)  SSK*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5  SSK6J7r7  \+(       a'  SSK8J9r9   " S S\95      r: " S S\95      r; " S S\95      r<Sr=\R|                  " \R~                  5      r@ " S S\A5      rB " S S\B5      rC " S S\A5      rD " S S 5      rE " S! S"\
R                  R                  5      rHS#\-S$\-S%S4S& jrI " S' S(\R                  5      rK " S) S*5      rL " S+ S,5      rM " S- S.\K5      rN " S/ S0\#R                  5      rPSSSSSS\=SS4	S1\0\Q\R                  4   S2\.\3S3/S4      S4\.\S   S5\.\3\0S\Q\T4   /S4      S6\.\/\Q\-4      S7\.\S   S8\.\S   S9\US:\.\1\Q      S;\.\"   S%S<4S= jjrVg)>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                   8    \ rS rSrS\S\4S jrS\S\4S jrSrg)	_Compressor<   datareturnc                     g N selfr&   s     dC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\tornado/websocket.pycompress_Compressor.compress=           modec                     g r)   r*   )r,   r2   s     r-   flush_Compressor.flush@   r0   r1   r*   N)	__name__
__module____qualname____firstlineno__bytesr.   intr4   __static_attributes__r*   r1   r-   r$   r$   <   s(    	 	5 		c 	e 	r1   r$   c                   .    \ rS rSrSrS\S\S\4S jrSrg)	_DecompressorC   r1   r&   
max_lengthr'   c                     g r)   r*   )r,   r&   r@   s      r-   
decompress_Decompressor.decompressF   r0   r1   r*   N)	r6   r7   r8   r9   unconsumed_tailr:   r;   rB   r<   r*   r1   r-   r>   r>   C   s!    	5 	c 	e 	r1   r>   c                       \ rS rSr SS\\   S\\   SS4S jjrS\\\	4   S\S   4S	 jr
S
\	SS4S jrS
\	SS4S jrS\\\      S\\   S\\   SS4S jrSrg)_WebSocketDelegateI   N
close_codeclose_reasonr'   c                     g r)   r*   r,   rH   rI   s      r-   on_ws_connection_close)_WebSocketDelegate.on_ws_connection_closeM   s     r1   messagezAwaitable[None]c                     g r)   r*   r,   rN   s     r-   
on_message_WebSocketDelegate.on_messageR   r0   r1   r&   c                     g r)   r*   r+   s     r-   on_ping_WebSocketDelegate.on_pingU   r0   r1   c                     g r)   r*   r+   s     r-   on_pong_WebSocketDelegate.on_pongX   r0   r1   typvaluetbc                     g r)   r*   r,   rY   rZ   r[   s       r-   log_exception _WebSocketDelegate.log_exception[   s     r1   r*   NN)r6   r7   r8   r9   r   r;   strrL   r   r:   rQ   rT   rW   r    BaseExceptionr!   r^   r<   r*   r1   r-   rF   rF   I   s    
 SW	&sm	BJ3-		
	eCJ&7 	HEV<W 		 	$ 		 	$ 		$}-.	 M*	 '		
 	r1   rF   i   c                       \ rS rSrSrg)WebSocketErrorj   r*   Nr6   r7   r8   r9   r<   r*   r1   r-   rd   rd   j       r1   rd   c                       \ rS rSrSrSrg)WebSocketClosedErrorn   zDRaised by operations on a closed connection.

.. versionadded:: 3.2
r*   N)r6   r7   r8   r9   __doc__r<   r*   r1   r-   ri   ri   n   s    
 	r1   ri   c                       \ rS rSrSrg)_DecompressTooLargeErrorw   r*   Nrf   r*   r1   r-   rm   rm   w   rg   r1   rm   c                   Z    \ rS rSrSS\S4S\\   S\\   S\S\\\	\
4      SS4
S jjrS	rg)
_WebSocketParams{   Nping_intervalping_timeoutmax_message_sizecompression_optionsr'   c                 4    Xl         X l        X0l        X@l        g r)   rr   rs   rt   ru   )r,   rr   rs   rt   ru   s        r-   __init___WebSocketParams.__init__|   s     +( 0#6 r1   )ru   rt   rr   rs   )r6   r7   r8   r9   _default_max_message_sizer   floatr;   r   ra   r   rx   r<   r*   r1   r-   rp   rp   {   s_     *.(, 98<
7
7 uo
7 	
7
 &d38n5
7 

7 
7r1   rp   c                     ^  \ rS rSrSrS\R                  R                  S\R                  S\
SS4U 4S jjrS	\
S\
SS4S
 jr\S\\   4S j5       r\S\\   4S j5       r\S\4S j5       r S-S\\\\\\
4   4   S\SS4S jjrS\\   S\\   4S jr\S\\   4S j5       rS\\\\
4      4S jrS	\S\S\\S      4S jrS\\\4   S\\S      4S jr S.S\\\4   SS4S jjr!S\SS4S jr"S\SS4S jr#S/S jr$S0S\\   S\\   SS4S jjr%S \S\4S! jr&S"\SS4S# jr'S/S$ jr( S0S%\\   S&\\   SS4S' jjr)S/U 4S( jjr*S\S)   4S* jr+S\,4S+ jr-S,r.U =r/$ )1WebSocketHandler   aZ  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                 `   > [         TU ]  " X40 UD6  S U l        S U l        S U l        SU l        g NF)superrx   ws_connectionrH   rI   _on_close_called)r,   r   r   r   	__class__s       r-   rx   WebSocketHandler.__init__   s6     	88!  %r1   argsc                 d  #    Xl         X l        U R                  R                  R	                  SS5      R                  5       S:w  a;  U R                  S5        SnU R                  U5        [        R                  " U5        g U R                  R                  n[        S UR	                  SS5      R                  S5      5      nS	U;  a;  U R                  S5        S
nU R                  U5        [        R                  " U5        g SU R                  R                  ;   a&  U R                  R                  R	                  S5      nO&U R                  R                  R	                  SS 5      nUbQ  U R                  U5      (       d;  U R                  S5        SnU R                  U5        [        R                  " U5        g U R                  5       U l        U R                  (       a$  U R                  R                  U 5      I S h  vN   g U R                  SS5        U R!                  SS5        g  N*7f)NUpgrade 	websocket  z"Can "Upgrade" only to "WebSocket".c                 >    U R                  5       R                  5       $ r)   )striplower)ss    r-   <lambda>&WebSocketHandler.get.<locals>.<lambda>   s    aggioo'r1   
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_kwargsr   headersgetr   
set_statusfinishr   debugmapsplitcheck_originget_websocket_protocolr   accept_connection
set_header)r,   r   r   log_msgr   
connectionorigins          r-   r   WebSocketHandler.get   s    ! <<##Ir288:kIOOC :GKK MM'"
 ,,&&'\2)F)L)LS)Q

 J&OOC 7GKK MM'" t||+++\\))--h7F\\))--.DdKF
 d&7&7&?&?OOC ;GKK MM'"!88:$$66t<<<OOC!34OO3Z@ =s   HH0H.+H0c                 :    U R                   R                  SS5      $ )aN  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-   rr   WebSocketHandler.ping_interval  s     }}  !:DAAr1   c                 :    U R                   R                  SS5      $ )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-   rs   WebSocketHandler.ping_timeout)  s    . }}  !94@@r1   c                 B    U R                   R                  S[        5      $ )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   rz   r   s    r-   rt   !WebSocketHandler.max_message_sizeB  s!     }}  (*C
 	
r1   rN   binaryFuture[None]c                    U R                   b  U R                   R                  5       (       a
  [        5       e[        U[        5      (       a  [
        R                  R                  U5      nU R                   R                  XS9$ )a  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`.
r   )	r   
is_closingri   
isinstancedicttornadoescapejson_encodewrite_messager,   rN   r   s      r-   r   WebSocketHandler.write_messageO  sh    0 %););)F)F)H)H&((gt$$nn009G!!///GGr1   subprotocolsc                     g)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_subprotocol#WebSocketHandler.select_subprotocolm  s    . r1   c                 L    U R                   c   eU R                   R                  $ )zIThe subprotocol returned by `select_subprotocol`.

.. versionadded:: 5.1
)r   selected_subprotocolr   s    r-   r   %WebSocketHandler.selected_subprotocol  s(     !!---!!666r1   c                     g)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_options(WebSocketHandler.get_compression_options  s    , r1   c                     g)aP  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,   r   r   s      r-   openWebSocketHandler.open  s     	r1   c                     [         e)zHandle incoming messages on the WebSocket

This method must be overridden.

.. versionchanged:: 4.5

   ``on_message`` can be a coroutine.
NotImplementedErrorrP   s     r-   rQ   WebSocketHandler.on_message  s
     "!r1   r&   c                     [        U5      nU R                  b  U R                  R                  5       (       a
  [        5       eU R                  R	                  U5        g)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   r   r   ri   
write_pingr+   s     r-   pingWebSocketHandler.ping  sJ      Dz%););)F)F)H)H&((%%d+r1   c                     g)z6Invoked when the response to a ping frame is received.Nr*   r+   s     r-   rW   WebSocketHandler.on_pong      r1   c                     g)z*Invoked when the a ping frame is received.Nr*   r+   s     r-   rT   WebSocketHandler.on_ping  r   r1   c                     g)a0  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_closeWebSocketHandler.on_close  s     	r1   codereasonc                 l    U R                   (       a#  U R                   R                  X5        SU l         gg)a  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)r   closer,   r   r   s      r-   r   WebSocketHandler.close  s.      $$T2!%D r1   r   c                     [        U5      nUR                  nUR                  5       nU R                  R                  R                  S5      nX:H  $ )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   r   r   r   )r,   r   parsed_originhosts       r-   r   WebSocketHandler.check_origin   sI    ^ !(%%||##''/ ~r1   rZ   c                 X    U R                   c   eU R                   R                  U5        g)a  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)r   set_nodelayr,   rZ   s     r-   r   WebSocketHandler.set_nodelay8  s*     !!---&&u-r1   c                     U R                   (       a!  U R                   R                  5         S U l         U R                  (       d(  SU l        U R                  5         U R	                  5         g g )NT)r   on_connection_closer   r   _break_cyclesr   s    r-   r   $WebSocketHandler.on_connection_closeI  sP    224!%D$$$(D!MMO  %r1   rH   rI   c                 <    Xl         X l        U R                  5         g r)   rH   rI   r   rK   s      r-   rL   'WebSocketHandler.on_ws_connection_closeR       %(  "r1   c                 n   > U R                  5       S:w  d  U R                  (       a  [        TU ]  5         g g Ne   )
get_statusr   r   r   r,   r   s    r-   r   WebSocketHandler._break_cyclesY  s,     ??#t'<'<G!# (=r1   WebSocketProtocolc                     U R                   R                  R                  S5      nUS;   aE  [        U R                  U R
                  U R                  U R                  5       S9n[        U SU5      $ g )Nr   )7813rw   F)	r   r   r   rp   rr   rs   rt   r   WebSocketProtocol13)r,   websocket_versionparamss      r-   r   'WebSocketHandler.get_websocket_protocolb  sl     LL00445LM 00%"00!..!%!6!6$($@$@$B	F 'tUF;;r1   c                 T    S H  n[        X[        5        M     U R                  5       $ )N)writeredirectr   
set_cookier   r4   r   )setattr#_raise_not_supported_for_websocketsdetach)r,   methods     r-   _detach_streamWebSocketHandler._detach_streamn  s)    
F D"EF
 {{}r1   )r   rH   rI   r   r   r   Fr1   r'   Nr`   )0r6   r7   r8   r9   rk   r   webApplicationr   HTTPServerRequestr   rx   r   propertyr   r{   rr   rs   r;   rt   r   r:   ra   r   boolr   r   r   r   r   r   r   rQ   r   rW   rT   r   r   r   r   r   rL   r   r   r   r  r<   __classcell__r   s   @r-   r}   r}      s   N`
&[[,,
& ++
& 	
&
 

&2As 2Ac 2Ad 2Ah Bx B B Ahuo A A0 

# 

 

 JOHUCc3h78HBFH	H<tCy Xc] 2 7hsm 7 7$sCx.)A 0#  )D/1J  	"%U
"3 	"48Q 	",sEz* ,T ,*E d E d &(3- & &QU &(63 64 6p. .$ ."! OS#"3-#>Fsm#	#$
1D(E 
  r1   r}   r   r   r'   c                      [        S5      e)Nz$Method not supported for Web Sockets)RuntimeError)r   r   s     r-   r  r  }  s    
=
>>r1   c            	       ~   \ rS rSrSrS"S jrS\S\S	\SS
4S jrS#S jr	S#S jr
\R                  S$S\\   S\\   SS4S jj5       r\R                  S\4S j5       r\R                  S\SS4S j5       r\R                   S%S\\\\\\4   4   S\SS4S jj5       r\\R                  S\\   4S j5       5       r\R                  S\SS4S j5       r\R                  S\\\4   S\R:                  SS4S j5       r\R                  S#S j5       r\R                  S#S j5       r \R                  S\SS4S  j5       r!S!r"g)&r   i  z+Base class for WebSocket protocol versions.handlerr'   Nc                 :    Xl         S U l        SU l        SU l        g r   )r  streamclient_terminatedserver_terminatedr,   r  s     r-   rx   WebSocketProtocol.__init__  s    !&!&r1   callbackr   r   zOptional[Future[Any]]c                 N    U" U0 UD6nUbL  [         R                  " U5      nU R                  c   eU R                  R                  R	                  US 5        U$ ! [
         a?    U R                  R                  " [        R                  " 5       6   U R                  5          gf = f)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                 "    U R                  5       $ r)   )result)fs    r-   r   1WebSocketProtocol._run_callback.<locals>.<lambda>  s
    r1   )r	   convert_yieldedr!  io_loop
add_future	Exceptionr  r^   sysexc_info_abort)r,   r&  r   r   r)  s        r-   _run_callbackWebSocketProtocol._run_callback  s    	t.v.F !,,V4{{...##..v7KLM  	LL&&7KKM	s   A AB$#B$c                 $    U R                  5         g r)   )r2  r   s    r-   r   %WebSocketProtocol.on_connection_close  s    r1   c                     SU l         SU l        U R                  b  U R                  R                  5         U R                  5         g)z?Instantly aborts the WebSocket connection by closing the socketTN)r"  r#  r!  r   r   s    r-   r2  WebSocketProtocol._abort  s5    !%!%;;"KK

r1   r   r   c                     [        5       er)   r   r   s      r-   r   WebSocketProtocol.close      !##r1   c                     [        5       er)   r   r   s    r-   r   WebSocketProtocol.is_closing  r;  r1   c                    #    [        5       e7fr)   r   r$  s     r-   r   #WebSocketProtocol.accept_connection       !##   rN   r   r   c                     [        5       er)   r   r   s      r-   r   WebSocketProtocol.write_message       "##r1   c                     [        5       er)   r   r   s    r-   r   &WebSocketProtocol.selected_subprotocol  s     "##r1   r&   c                     [        5       er)   r   r+   s     r-   r   WebSocketProtocol.write_ping  r;  r1   keyr   c                     [        5       er)   r   )r,   rI  r   s      r-   _process_server_headers)WebSocketProtocol._process_server_headers  rD  r1   c                     [        5       er)   r   r   s    r-   start_pingingWebSocketProtocol.start_pinging  r;  r1   c                    #    [        5       e7fr)   r   r   s    r-   _receive_frame_loop%WebSocketProtocol._receive_frame_loop  r@  rA  xc                     [        5       er)   r   r,   rS  s     r-   r   WebSocketProtocol.set_nodelay  r;  r1   )r"  r  r#  r!  )r  rF   r'   Nr  r`   r  )#r6   r7   r8   r9   rk   rx   r   r   r3  r   r2  abcabstractmethodr   r;   ra   r   r  r   r}   r   r   r:   r   r   r  r   r   r   HTTPHeadersrK  rN  rQ  r   r<   r*   r1   r-   r   r     s   5' ),8;	 * 	$(3- $ $QU $ $ 	$D $ $ 	$/? $D $ $ 	IN$S%c3h78$BF$	$ $
 $hsm $  $ 	$u $ $ $ 	$e$$/7/C/C$	$ $
 	$ $ 	$ $ 	$T $d $ $r1   r   c            
       f    \ rS rSr SS\S\\   S\\\\	4      SS4S jjr
SS jrS	\S\4S
 jrSrg)_PerMessageDeflateCompressori  N
persistent	max_wbitsru   r'   c                    Uc  [         R                  nSUs=::  a  [         R                  ::  d  O  [        SU[         R                  5      eX l        Ub  SU;  a*  [        R
                  R                  R                  U l        O
US   U l        Ub  SU;  a  SU l	        O
US   U l	        U(       a  U R                  5       U l        g S U l        g )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]  ru   s       r-   rx   %_PerMessageDeflateCompressor.__init__  s     IY0$..0@ 
 $  '"*==&-kk&E&E&P&PD#&9:M&ND#&+=P*PDO1+>DO#668D#Dr1   c                     [         R                  " U R                  [         R                  U R                  * U R
                  5      $ r)   )rc  compressobjri  DEFLATEDrf  rj  r   s    r-   rk  /_PerMessageDeflateCompressor._create_compressor   s3    ##T]]T__4Ddoo
 	
r1   r&   c                     U R                   =(       d    U R                  5       nUR                  U5      UR                  [        R
                  5      -   nUR                  S5      (       d   eUS S $ )N     )rl  rk  r.   r4   rc  Z_SYNC_FLUSHendswith)r,   r&   
compressors      r-   r.   %_PerMessageDeflateCompressor.compress  sc    %%B)@)@)B
""4(:+;+;D<M<M+NN}}01111CRyr1   )ri  rl  rf  rj  r)   )r'   r$   )r6   r7   r8   r9   r  r   r;   r   ra   r   rx   rk  r:   r.   r<   r*   r1   r-   r[  r[    s\    
 9=	!$!$ C=!$ &d38n5	!$
 
!$F

U u r1   r[  c                   j    \ rS rSr SS\S\\   S\S\\\\	4      SS4
S jjr
SS	 jrS
\S\4S jrSrg)_PerMessageDeflateDecompressori  Nr\  r]  rt   ru   r'   c                     X0l         Uc  [        R                  nSUs=::  a  [        R                  ::  d  O  [        SU[        R                  5      eX l        U(       a  U R                  5       U l        g S U l        g )Nr_  r`  )_max_message_sizerc  rd  re  rf  _create_decompressor_decompressor)r,   r\  r]  rt   ru   s        r-   rx   '_PerMessageDeflateDecompressor.__init__  sl     "2IY0$..0@ 
 $))+  "&Dr1   c                 D    [         R                  " U R                  * 5      $ r)   )rc  decompressobjrf  r   s    r-   r}  3_PerMessageDeflateDecompressor._create_decompressor%  s    !!4??"233r1   r&   c                     U R                   =(       d    U R                  5       nUR                  US-   U R                  5      nUR                  (       a
  [        5       eU$ )Nrs  )r~  r}  rB   r|  rD   rm   )r,   r&   decompressorr)  s       r-   rB   )_PerMessageDeflateDecompressor.decompress(  sU    ))HT-F-F-H((&&(>(>
 ''*,,r1   )r~  r|  rf  r)   )r'   r>   )r6   r7   r8   r9   r  r   r;   r   ra   r   rx   r}  r:   rB   r<   r*   r1   r-   rz  rz    se     9=&& C=& 	&
 &d38n5& 
&04u  r1   rz  c                   <   \ rS rSrSrSrSrSrSr\\-  \-  r	Sr
SrS	S
S\S\SS4S jr\S\\   4S j5       r\R&                  S\\   SS4S j5       rS	\SS4S jrS	\SS4S jr\S\\\4   S\4S j5       rS	\S\4S jrS	\SS4S jrS\R<                  S\\ \\!\\4   4      4S jr"S\\\4   S\R<                  SS4S jr# S;S\S\!\\$4   S\\!\\$4      S\!\\$4   4S jjr% S;S\S\!\\$4   S\\!\\$4      SS4S jjr& S<S \S!\'S"\S#\'SS$4
S% jjr( S=S&\\\\!\\$4   4   S'\SS$4S( jjr)S"\SS4S) jr*S>S* jr+S+\'S\4S, jr,S>S- jr-S!\'S"\SS.4S/ jr.S?S0\\'   S1\\   SS4S2 jjr/S\4S3 jr0S4\SS4S5 jr1\S\24S6 j5       r3\S\24S7 j5       r4S>S8 jr5S>S9 jr6S:r7g)@r  i2  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  rF   mask_outgoingr  r'   c                 r   [         R                  X5        X l        X0l        SU l        S U l        S U l        S U l        S U l        S U l	        S U l
        S U l        UR                  U l        S U l        S U l        S U l        SU l        SU l        SU l        SU l        SU l        S U l        S U l        S U l        g )NFr   )r   rx   r  r  _final_frame_frame_opcode_masked_frame_frame_mask_frame_length_fragmented_message_buffer_fragmented_message_opcode_waitingru   _compression_optionsr~  rl  _frame_compressed_message_bytes_in_message_bytes_out_wire_bytes_in_wire_bytes_out_received_pongrH   rI   _ping_coroutine)r,   r  r  r  s       r-   rx   WebSocketProtocol13.__init__C  s     	""41*!!!!*.'*.'$*$>$>!!!% "#"#   # #r1   c                     U R                   $ r)   _selected_subprotocolr   s    r-   r   (WebSocketProtocol13.selected_subprotocolg  s    )))r1   rZ   c                     Xl         g r)   r  r   s     r-   r   r  k  s    %*"r1   c                   #     U R                  U5         U R                  U5      I S h  vN   g ! [         a=    UR                  S5        SnUR                  U5        [        R
                  " U5         g f = f NO! [        R                   a    U R                  5          g [         a(    [        R
                  " SSS9  U R                  5          g f = f7f)Nr   !Missing/Invalid WebSocket headersz$Malformed WebSocket request receivedTr1  )
_handle_websocket_headersre  r   r   r   r   _accept_connectionasyncioCancelledErrorr2  )r,   r  r   s      r-   r   %WebSocketProtocol13.accept_connectiono  s     	**73	))'222  	s#9GNN7#MM'"	 3%% 	KKM 	MM@4PKKM	s_   C0 A< A:A< CAA74C6A77C:A< <$C C".CCCCc                 \   ^ Sn[        [        U4S jU5      5      (       d  [        S5      eg)zzVerifies 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                 N   > TR                   R                  R                  U 5      $ r)   )r   r   r   )r*  r  s    r-   r   ?WebSocketProtocol13._handle_websocket_headers.<locals>.<lambda>  s    !8!8!<!<Q!?r1   r  N)allr   re  )r,   r  fieldss    ` r-   r  -WebSocketProtocol13._handle_websocket_headers  s0     H3?HII@AA Jr1   rI  c                     [         R                  " 5       nUR                  [        U 5      5        UR                  S5        [	        [
        R                  " UR                  5       5      5      $ )z_Computes 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)rI  r  s     r-   compute_accept_value(WebSocketProtocol13.compute_accept_value  sH    
 ||~DI;<&**4;;=9::r1   c                     [         R                  [        [        UR                  R
                  R                  S5      5      5      $ )Nr  )r  r  r   ra   r   r   r   r$  s     r-   _challenge_response'WebSocketProtocol13._challenge_response  s5    "77goo--112EFG
 	
r1   c           	      *  #    UR                   R                  R                  S5      nU(       a/  UR                  S5       Vs/ s H  o3R	                  5       PM     nnO/ nUR                  U5      U l        U R                  (       a.  U R                  U;   d   eUR                  SU R                  5        U R                  UR                   R                  5      nU H  nUS   S:X  d  M  U R                  c  M  U R                  SUS   U R                  5        SUS   ;   a  US   S   c  US   S	 UR                  S[        R                  " SUS   5      5          O   UR                  S	5        UR                  S
5        UR                  SS5        UR                  SS5        UR                  SU R                  U5      5        UR!                  5         UR#                  5       U l        U R'                  5          UR(                  " UR*                  0 UR,                  D6nUb
  UI S h  vN   U R9                  5       I S h  vN   g s  snf  N"! [.         a5    UR0                  " [2        R4                  " 5       6   U R7                  5          g f = f NN7f)NSec-WebSocket-Protocolr   r   permessage-deflateserver   client_max_window_bitsSec-WebSocket-ExtensionszContent-Typer   r   r   r   zSec-WebSocket-Accept)r   r   r   r   r   r   r   r   _parse_extensions_headerr  _create_compressorsr   _encode_headerclear_headerr   r  r   r  r!  rN  r   r   r   r/  r^   r0  r1  r2  rQ  )r,   r  subprotocol_headerr   r   
extensionsextopen_results           r-   r  &WebSocketProtocol13._accept_connection  sO    $__44889QR/A/G/G/LM/L!GGI/LLMLL$+$>$>|$L!$$,,<<<79R9RS227??3J3JK
C1v--$2K2K2W ((3q64;T;TU,A6A78@ A78"".++,@#a&I ! $ 	^,39k2<3143K3KG3TU,,.	!,,(9(9QW=P=PQK&!!! &&(((] NP " 	!!3<<>2KKM	
 	)sb   A JIBJ*J9DJ<.I *I+I /JJ
JI <JJJJr   c                     UR                  SS5      nU(       aD  UR                  S5       Vs/ s H'  n[        R                  " UR	                  5       5      PM)     sn$ / $ s  snf )Nr  r   r   )r   r   r   _parse_headerr   )r,   r   r  es       r-   r  ,WebSocketProtocol13._parse_extensions_header  sW     [[!;R@
?I?O?OPS?TU?T!H**17795?TUU	 Vs   .A c                 p   US   R                  5       S:X  d   eUS   R                  5       S:X  d   eU R                  U5      nUS   U:X  d   eU R                  U5      nU H:  nUS   S:X  a$  U R                  b  U R	                  S	US
   5        M0  [        SU5      e   UR                  SS5      U l        g)z|Process the headers sent by the server to this client connection.

'key' is the websocket handshake challenge/response key.
r   r   r   r   zSec-Websocket-Acceptr   r  Nclientr  zunsupported extension %rr  )r   r  r  r  r  re  r   r   )r,   rI  r   acceptr  r  s         r-   rK  +WebSocketProtocol13._process_server_headers  s     y!'')[888|$**,	999**3/-.&888227;
C1v--$2K2K2W((3q6: !;SAA	  %,KK0H$$O!r1   sideagreed_parametersru   c                     [        US-   U;  S9nUR                  US-   S5      nUc  [        R                  US'   O[	        U5      US'   X4S'   U$ )z\Converts a websocket agreed_parameters set to keyword arguments
for our compressor objects.
_no_context_takeover)r\  _max_window_bitsNr]  ru   )r   r   rc  rd  r;   )r,   r  r  ru   optionswbits_headers         r-   _get_compressor_options+WebSocketProtocol13._get_compressor_options  sh     55>OO
 ),,T4F-FM#'>>GK #&|#4GK )<%&r1   c           	         1 SknU H  nXT;  d  M
  [        SU-  5      e   US:X  a  SOSn[        S0 U R                  XU5      D6U l        [	        SSU R
                  R                  0U R                  XbU5      D6U l        g )N>   r  server_max_window_bitsclient_no_context_takeoverserver_no_context_takeoverz$unsupported compression parameter %rr  r  rt   r*   )re  r[  r  rl  rz  r  rt   r~  )r,   r  r  ru   allowed_keysrI  
other_sides          r-   r  'WebSocketProtocol13._create_compressors   s    
 %C& !G#!MNN % #'("2X
7 
**4DWX
 < 
![[99
**/B
r1   finopcoder&   flagsr   c                    [        U5      nUS-  (       a#  U(       d  [        S5      eUS:  a  [        S5      eU(       a  U R                  nOSn[        R                  " SXb-  U-  5      nU R
                  (       a  SnOSnUS:  a  U[        R                  " SXX-  5      -  nOCUS	::  a  U[        R                  " S
SU-  U5      -  nOU[        R                  " SSU-  U5      -  nU R
                  (       a$  [        R                  " S5      n	U	[        X5      -   nXs-  nU =R                  [        U5      -  sl	        U R                  R                  U5      $ )Nr_  z$control frames may not be fragmented}   z/control frame payloads may not exceed 125 bytesr   Br  ~   i  z!BHz!BQ      )lenre  FINstructpackr  osurandomr   r  r!  r	  )
r,   r  r  r&   r  data_lenfinbitframemask_bitmasks
             r-   _write_frame WebSocketProtocol13._write_frame  s&    t9C<  !GHH#~ !RSSXXFFC5!89HHc>V[[h&9::EV[[hAAEV[[hAAE::a=D/$55DE
*{{  ''r1   rN   r   c                 >  ^ U(       a  SnOSn[        U[        5      (       a  [        R                  R	                  U5      n[        R                  R                  U5      n[        U[        5      (       d   eU =R                  [        U5      -  sl        SnU R                  (       a)  U R                  R                  U5      nX@R                  -  n U R                  SX1US9mSU4S jjn[        R                   " U" 5       5      $ ! [         a    [        5       ef = f)z9Sends the given message to the client of this Web Socket.   r  r   T)r  c                  X   >#     T I S h  vN   g  N! [          a    [        5       ef = f7fr)   )r   ri   )futs   r-   wrapper2WebSocketProtocol13.write_message.<locals>.wrapperW  s)     -		$ -*,,-s    *  * '*r  )r   r   r   r   r   r   r:   r  r  rl  r.   RSV1r  r   ri   r  ensure_future)r,   rN   r   r  r  r  r   s         @r-   r   !WebSocketProtocol13.write_message<  s     FFgt$$nn009G..%%g.'5))))3w</&&//8GYYE	)##D&#GC	- $$WY// ! 	)&((	)s   D Dc                 X    [        U[        5      (       d   eU R                  SSU5        g)zSend ping frame.T	   N)r   r:   r  r+   s     r-   r   WebSocketProtocol13.write_ping_  s'    $&&&&$T*r1   c                 *  #     U R                   (       d+  U R                  5       I S h  vN   U R                   (       d  M+  U R                  R                  U R                  U R                  5        g  NH! [         a    U R                  5          NOf = f7fr)   )r"  _receive_framer   r2  r  rL   rH   rI   r   s    r-   rQ  'WebSocketProtocol13._receive_frame_loopd  so     	,,))+++ ,,, 	++DOOT=N=NO ,  	KKM	s8   B$A3 A1A3  1B1A3 3BBBBnc                    #    U R                   R                  U5      I S h  vN nU =R                  U-  sl        U$  N7fr)   )r!  
read_bytesr  )r,   r  r&   s      r-   _read_bytesWebSocketProtocol13._read_bytesl  s7     [[++A..q  /s   ?=?c                 z  #    U R                  S5      I S h  vN n[        R                  " SU5      u  p#X R                  -  nX R                  -  nX R
                  -  nUS-  nU R                  b1  US:w  a+  [        XPR                  -  5      U l	        XPR                  ) -  nU(       a  U R                  5         g [        US-  5      nUS-  n	U(       a  U	S:  a  U R                  5         g U	S:  a  Xl        OsU	S:X  a4  U R                  S5      I S h  vN n[        R                  " SU5      S   n	O9U	S:X  a3  U R                  S5      I S h  vN n[        R                  " S	U5      S   n	U	n
U R                  b  U
[        U R                  5      -  n
XR                  R                  :  a#  U R!                  S
S5        U R                  5         g U(       a  U R                  S5      I S h  vN U l        U R                  U	5      I S h  vN nU(       a%  U R"                  c   e[%        U R"                  U5      nU(       a  U(       d  U R                  5         g OUS:X  ai  U R                  c  U R                  5         g U R                  R'                  U5        U(       a(  U R(                  n[+        U R                  5      nS U l        O;U R                  b  U R                  5         g U(       d  X`l        [-        U5      U l        U(       a   U R/                  Xa5      nUb  UI S h  vN   g g g  GN GN GN GNJ GN/ N7f)Nr  BBr_  r   r  r  r  z!Hz!Q  zmessage too bigr  )r  r  unpackr  RSV_MASKOPCODE_MASKr~  r  r  r  r2  r  r  r  r  rt   r   r  r   extendr  r:   	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
  "WebSocketProtocol13._receive_frameq  s    %%a(("(--d";((*.***"SL)fk &*-))*C%DD"iiZ'MKKM4/0	$t+
 s!2KKM!+3))!,,DtT215J3))!,,DtT215J**6s4::;;G[[111JJt./KKM %)%5%5a%88D%%j11##///"4#3#3T:D  " " q[..6++224888T<<=26/..:!28/2;D//!11&?N)$$$ * Y )6 - -  91H %sl   L;L*C:L;L-9L;L0BL;)L3*L;L6DL;"L9#L;-L;0L;3L;6L;9L;zOptional[Future[None]]c                    U R                   (       a  gU R                  (       a+  U R                  c   e U R                  R                  U5      nUS:X  aV  U =R                  [        U5      -  sl         UR                  S5      nU R                  U R                  R                  U5      $ US:X  aD  U =R                  [        U5      -  sl        U R                  U R                  R                  U5      $ US:X  av  SU l         [        U5      S:  a"  [        R                  " S	USS 5      S
   U l        [        U5      S:  a  [#        USS 5      U l        U R                  U R                   5        gUS:X  a;   U R'                  SSU5        U R                  U R                  R*                  U5        gUS:X  a-  SU l        U R                  U R                  R.                  U5      $ U R                  5         g! [         a%    U R                  SS5        U R                  5          gf = f! [         a    U R                  5          gf = f! [(         a    U R                  5          Nf = f)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"  r  r~  rB   rm   r   r2  r  r  decodeUnicodeDecodeErrorr3  r  rQ   r  r  rH   r   rI   r  r   rT   r  rW   )r,   r  r&   decodeds       r-   r  #WebSocketProtocol13._handle_message  s   !!!!%%111))44T: S=""c$i/"++g. %%dll&=&=wGGs]""c$i/"%%dll&=&=tDDs]%)D"4yA~"(--d2Ah"?"B4y1}$.tABx$8!JJt'  s]!!$T2 t||33T:  s]"&D%%dll&:&:DAAKKMQ , 

4!FG & * % s5   G6 4H( 8I 6,H%$H%(III%$I%r   r   c                 Z   U R                   (       dp  U R                  R                  5       (       dJ  Uc  Ub  SnUc  SnO[        R                  " SU5      nUb  U[        U5      -  n U R                  SSU5        SU l         U R                  (       a^  U R                  b6  U R                  R                  R                  U R                  5        SU l
        U R                  R                  5         OgU R                  cZ  U R                  R                  R                  U R                  R                  R                  5       S-   U R                  5      U l
        U R                   (       a"  U R                   R#                  5         SU l        gg! [         a    U R                  5          GN.f = f)z Closes the WebSocket connection.Ni  r1   r%  Tr_     )r#  r!  closedr  r  r   r  r   r2  r"  r  r-  remove_timeoutr   add_timeouttimer  cancel)r,   r   r   
close_datas       r-   r   WebSocketProtocol13.close  sO   %%;;%%''<F$6D<!$J!'T4!8J%$v,.J"%%dC< &*D"!!}}(##224==A $KK]]" !KK//;;##((*Q.DM   '')#'D    ) "KKM"s   (F F*)F*c                     U R                   R                  5       =(       d    U R                  =(       d    U R                  $ )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   WebSocketProtocol13.is_closing  s-     {{!!#Wt'='=WAWAWWr1   rS  c                 :    U R                   R                  U5        g r)   )r!  r   rU  s     r-   r   WebSocketProtocol13.set_nodelay   s    "r1   c                 :    U R                   R                  nUb  U$ g)Nr   )r  rr   )r,   intervals     r-   rr   !WebSocketProtocol13.ping_interval#  s    ;;,,Or1   c           	         U R                   R                  nUbe  U R                  (       aR  XR                  :  aC  [        [        R
                  SU SU R                   SU R                   35        U R                  $ U$ U R                  $ )NzThe websocket_ping_timeout (z5) cannot be longer than the websocket_ping_interval (z").
Setting websocket_ping_timeout=)r  rs   rr   de_dupe_gen_logloggingWARNING)r,   timeouts     r-   rs    WebSocketProtocol13.ping_timeout*  s    ++**!!g0B0B&B OO27) <::>:L:L9M N88<8J8J7KM	 )))N!!!r1   c                     U R                   (       d;  U R                  S:  a*  [        R                  " U R	                  5       5      U l         ggg)z9Start sending periodic pings to keep the connection aliver   N)r  rr   r  create_taskperiodic_pingr   s    r-   rN  !WebSocketProtocol13.start_pinging;  sA     $$""Q&#*#6#6t7I7I7K#LD  ' %r1   c                   #    U R                   nU R                  n[        R                  " U5      I Sh  vN    SU l        [
        R                  " 5       R                  5       nU R                  S5        [        R                  " U5      I Sh  vN   US:  a!  U R                  (       d  U R                  SS9  g[        R                  " [
        R                  " 5       R                  5       U-
  U-   5      I Sh  vN   M   N Nt N
7f)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.
NFr1   r   zping timed out)r   )
rr   rs   r  sleepr  r   currentr0  r   r   )r,   r9  r?  	ping_times       r-   rC  !WebSocketProtocol13.periodic_pingE  s     
 %%##mmH%%%"'D(--/IOOC  --((( {4#6#6

"2
3 -- 0 5 5 7) Ch NOOO  	& ) Ps5   2DDADDA+D:D;DDD)r  rl  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  rH   rI   r  r  r   r#  r!  r)   )r   r  r  r`   )8r6   r7   r8   r9   rk   r  r  RSV2RSV3r  r  r!  r  rp   rx   r  r   ra   r   setterr}   r   r  staticmethodr   r:   r  r  r  r   rY  r   r   r   r  rK  r   r  r  r;   r  r   r   rQ  r  r
  r  r   r   r   r{   rr   rs   rN  rC  r<   r*   r1   r-   r  r  2  sc    CDDDd{T!HKF!$%!$ !$ !	!$
 
!$H *hsm * *   +(3- +D + !+/? D (B1A Bd B ;%U
"3 ; ; ;
+; 
 

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

 

8 AB((!$(,1(:=(	(D JO!0S%c3h78!0BF!0	!0F+u + +
P3 5 
Q%f1c 1 1;S 1f((3- ( (QU (@XD X#T #d # u   "e " " MPr1   r  c                     ^  \ rS rSrSrSrSSSS\SS4S\R                  S\	\
\S\\4   /S4      S\	\\\4      S\	\   S\	\   S	\S
\	\\      S\	\   SS4U 4S jjjrS0S jrS1S\	\   S\	\   SS4S jjrS0U 4S jjr S1S\	\   S\	\   SS4S jjrS\R2                  SS4S jrS\\R8                  \R:                  4   S\R<                  SS4U 4S jjr S2S\\\\\\4   4   S\ SS4S jjr! S3S\	\
S /S4      S\"\S\\4      4S! jjr#S\\\4   S\	\"S      4S" jr$S\S\\4   S\	\"S      4S# jr%S4S$\SS4S% jjr&S$\SS4S& jr'S$\SS4S' jr(S\)4S( jr*\+S\	\   4S) j5       r,S*S+S,\	\-   S-\	\.   SS4S. jr/S/r0U =r1$ )5WebSocketClientConnectionia  z|WebSocket client connection.

This class should not be instantiated directly; use the
`websocket_connect` function instead.
Nr   on_message_callbackru   rr   rs   rt   r   resolverr'   c	           
        > [        5       U l        [        S5      U l        [        R
                  " [        R                  " S5      5      U l        X l	        S U l
        S U l        [        UUUUS9U l        UR                  R                  S5      u  pnSSS.U	   n	X-   U-   Ul        UR                   R#                  SS	[%        U R                  5      S
S.5        Ub  SR'                  U5      UR                   S'   Ub  SUR                   S'   SUl        [+        US9U l        [.        TU ]a  S US U R2                  SU R,                  SS5        g )Nr  r  rw   :httphttps)wswssr   r   r  )r   r   zSec-WebSocket-Keyr   r   r  z*permessage-deflate; client_max_window_bitsr  F)rQ  c                      g r)   r*   r*   r1   r-   r   4WebSocketClientConnection.__init__.<locals>.<lambda>  s    Dr1   i  @i   )r   connect_futurer   
read_queuer  r  r  r  rI  _on_message_callbackrH   rI   rp   r  url	partitionr   r  r   joinfollow_redirectsr   
tcp_clientr   rx   _on_http_response)r,   r   rP  ru   rr   rs   rt   r   rQ  schemeseprestr   s               r-   rx   "WebSocketClientConnection.__init__j  sD    %h(##BJJrN3$7! &'%- 3	
 $KK11#6Tw/7lT)&'%/%9)-		
 #8;8NGOO45* = OO67
 $) #X6""OO		
r1   c                 V    U R                   b  [        R                  " S[        5        g g )Nz"Unclosed WebSocketClientConnection)protocolwarningswarnResourceWarningr   s    r-   __del__!WebSocketClientConnection.__del__  s"    ==$ MM>P %r1   r   r   c                 d    U R                   b#  U R                   R                  X5        SU l         gg)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)rh  r   r   s      r-   r   WebSocketClientConnection.close  s,     ==$MM- DM %r1   c                    > U R                   R                  5       (       d#  U R                   R                  [        5       5        U R	                  S 5        U R
                  R                  5         [        TU ]!  5         g r)   )	rZ  doneset_exceptionr   _on_messagera  r   r   r   r   s    r-   r   -WebSocketClientConnection.on_connection_close  sY    ""''))--.?.AB#%r1   rH   rI   c                 <    Xl         X l        U R                  5         g r)   r   rK   s      r-   rL   0WebSocketClientConnection.on_ws_connection_close  r   r1   responsec                     U R                   R                  5       (       d\  UR                  (       a&  U R                   R                  UR                  5        g U R                   R                  [	        S5      5        g g )NzNon-websocket response)rZ  rq  errorrr  rd   )r,   rw  s     r-   rb  +WebSocketClientConnection._on_http_response  sX    ""''))~~##11(..A##11"#;<	 *r1   
start_liner   c                   >#    [        U[        R                  5      (       d   eUR                  S:w  a  [        TU ]  X5      I S h  vN   g U R                  b,  U R                  R                  U R                  5        S U l        X l	        U R                  5       U l        U R                  R                  U R                  U R                  5        U R                  R                  5       U R                  l        ["        R$                  " 5       R'                  U R                  R(                  5        U R                  R+                  5         S U l        [/        U R0                  U 5        g  GN#7fr   )r   r   ResponseStartLiner   r   headers_received_timeoutr-  r.  r   r   rh  rK  rI  r   r  r!  r   rG  add_callbackrQ  rN  final_callbackr   rZ  )r,   r{  r   r   s      r-   r~  *WebSocketClientConnection.headers_received  s    
 *h&@&@AAAA??c!'*:???==$LL''6 DM335--dhhE#557%%dmm&G&GH##% #*4+>+>E+ @s   AE-E*D#E-rN   r   r   c                 d    U R                   c  [        S5      eU R                   R                  XS9$ )a  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`.
z!Client connection has been closedr   )rh  ri   r   r   s      r-   r   'WebSocketClientConnection.write_message  s2     == &'JKK}}**7*BBr1   r&  zFuture[Union[None, str, bytes]]c                     U R                   R                  5       nUb0  U R                  R                  [        R
                  " U5      U5        U$ )a@  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.  r  r  )r,   r&  	awaitables      r-   read_message&WebSocketClientConnection.read_message  sA     OO'')	LL##G$9$9)$DhOr1   c                 $    U R                  U5      $ r)   )rs  rP   s     r-   rQ   $WebSocketClientConnection.on_message  s    ((r1   c                 ~    U R                   (       a  U R                  U5        g U R                  R                  U5      $ r)   )r\  r[  putrP   s     r-   rs  %WebSocketClientConnection._on_message  s3     $$%%g.??&&w//r1   r&   c                 ~    [        U5      nU R                  c
  [        5       eU R                  R                  U5        g)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   rh  ri   r   r+   s     r-   r   WebSocketClientConnection.ping&  s2     Dz== &((  &r1   c                     g r)   r*   r+   s     r-   rW   !WebSocketClientConnection.on_pong9      r1   c                     g r)   r*   r+   s     r-   rT   !WebSocketClientConnection.on_ping<  r  r1   c                 ,    [        U SU R                  S9$ )NT)r  r  )r  r  r   s    r-   r   0WebSocketClientConnection.get_websocket_protocol?  s    "4tDKKPPr1   c                 .    U R                   R                  $ )z?The subprotocol selected by the server.

.. versionadded:: 5.1
)rh  r   r   s    r-   r   .WebSocketClientConnection.selected_subprotocolB  s     }}111r1   rY   zOptional[Type[BaseException]]rZ   r[   c                 F    Uc   eUc   e[         R                  " SX!X#4S9  g )NzUncaught exception %sr  )r   ry  r]   s       r-   r^   'WebSocketClientConnection.log_exceptionJ  s.        -uU?OPr1   )r\  r  rH   rI   rZ  r  r   rI  r  rh  r[  ra  r  r`   r  r)   r  )2r6   r7   r8   r9   rk   rh  rz   r
   HTTPRequestr   r   r   ra   r:   r   r   r{   r;   r   r   rx   rl  r   r   rL   HTTPResponserb  r   RequestStartLiner}  rY  r~  r  r   r   r  rQ   rs  r   rW   rT   r   r   r  r   rb   r!   r^   r<   r  r  s   @r-   rO  rO  a  s    H
 TX8<)-(, 9,0'+<
''<
 &hdC6F0G/H$/N&OP<
 &d38n5	<

  <
 uo<
 <
 tCy)<
 8$<
 
<
 <
|Q!(3- ! !QU ! & OS#"3-#>Fsm#	#**A*A d F(33X5O5OOPF %%F 
	F> JOCS%c3h78CBFC	C$ SW8%F$G$MNO 
5sE)*	+()%U
"3 )48Q )0T3-.0	)D/	"0' ' '&E d E d Q(9 Q 2hsm 2 2Q,Q &Q ]#	Q
 
Q Qr1   rO  r]  r&  z!Future[WebSocketClientConnection]connect_timeoutrP  ru   rr   rs   rt   r   rQ  z$Awaitable[WebSocketClientConnection]c
                     [        U [        R                  5      (       a-  Ub   eU n
[        R                  " U
R
                  5      U
l        O[        R                  " XS9n
[        [        R                  [        R                  " U
[        R                  R                  5      5      n
[        U
UUUUUUU	S9nUbI  [        R                  " S[        SS9  [        R                  " 5       R                  UR                   U5        UR                   $ )a  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.
)r  )rP  ru   rr   rs   rt   r   rQ  zZThe callback argument to websocket_connect is deprecated. Use the returned Future instead.r  )
stacklevel)r   r
   r  r   rY  r   r   _RequestProxy	_DEFAULTSrO  ri  rj  DeprecationWarningr   rG  r.  rZ  )r]  r&  r  rP  ru   rr   rs   rt   r   rQ  r   conns               r-   websocket_connectr  U  s    ~ #z--..&&& #..w?((N  **@*@*J*JKG %//#!)!	D /		
 	##D$7$7Br1   )Wrk   rW  r  r  	functoolsr  r=  r  r0  r  r   urllib.parser   ri  rc  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$   r>   rF   rz   	lru_cachelogr<  r/  rd   ri   rm   rp   r  RequestHandlerr}   r  ABCr   r[  rz  r  _HTTPConnectionrO  ra   r  r{   r:   r;   r  r*   r1   r-   <module>r     sq         	 
   !   I 7 7 - - ! 8 ( $ %   ' (     *h  X 6 -  %%gkk2	Y 		> 		y 	7 7qw{{11 qh?s ?c ?d ?X$ X$v- -`# #LlP+ lP^qQ 1 A A 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 ] ,]r1   