o
    [h3e                     @  s  d dl m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mZmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZmZmZmZm Z  ddl!m"Z"m#Z# dd	l$m%Z% dd
l&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddlm0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8m9Z9 ddl.m:Z: ddl;m<Z< g dZ=G dd de<Z>G dd dZ?e?Z@		d!d"dd ZAdS )#    )annotationsN)TracebackType)
AnyAsyncIteratorCallable	GeneratorListOptionalSequenceTupleTypecast   )HeadersHeadersLike)InvalidHandshakeInvalidHeaderInvalidMessageInvalidStatusCodeNegotiationErrorRedirectHandshakeSecurityError)ClientExtensionFactory	Extension) enable_client_permessage_deflate)build_authorization_basicbuild_extension
build_hostbuild_subprotocolparse_extensionparse_subprotocolvalidate_subprotocols)
USER_AGENT)ExtensionHeader
LoggerLikeOriginSubprotocol)WebSocketURI	parse_uri   )build_requestcheck_response)read_response)WebSocketCommonProtocol)connectunix_connectWebSocketClientProtocolc                      s|   e Zd ZdZdZdZddddddd+ fddZd,ddZd-ddZe	d.d!d"Z
e	d/d%d&Z				d0d1d)d*Z  ZS )2r0   a  
    WebSocket client connection.

    :class:`WebSocketClientProtocol` provides :meth:`recv` and :meth:`send`
    coroutines for receiving and sending messages.

    It supports asynchronous iteration to receive incoming messages::

        async for message in websocket:
            await process(message)

    The iterator exits normally when the connection is closed with close code
    1000 (OK) or 1001 (going away). It raises
    a :exc:`~websockets.exceptions.ConnectionClosedError` when the connection
    is closed with any other code.

    See :func:`connect` for the documentation of ``logger``, ``origin``,
    ``extensions``, ``subprotocols``, and ``extra_headers``.

    See :class:`~websockets.legacy.protocol.WebSocketCommonProtocol` for the
    documentation of ``ping_interval``, ``ping_timeout``, ``close_timeout``,
    ``max_size``, ``max_queue``, ``read_limit``, and ``write_limit``.

    TclientN)loggerorigin
extensionssubprotocolsextra_headersr2   Optional[LoggerLike]r3   Optional[Origin]r4   *Optional[Sequence[ClientExtensionFactory]]r5   Optional[Sequence[Subprotocol]]r6   Optional[HeadersLike]kwargsr   returnNonec                  sD   |d u r	t d}t jdd|i| || _|| _|| _|| _d S )Nwebsockets.clientr2    )logging	getLoggersuper__init__r3   available_extensionsavailable_subprotocolsr6   )selfr2   r3   r4   r5   r6   r<   	__class__r@   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/websockets/legacy/client.pyrD   T   s   


z WebSocketClientProtocol.__init__pathstrheadersr   c                 C  sn   || _ || _| jr!| jd| | D ]\}}| jd|| qd| d}|t|7 }| j|  dS )zF
        Write request line and headers to the HTTP request.

        z> GET %s HTTP/1.1z> %s: %szGET z HTTP/1.1
N)	rK   request_headersdebugr2   	raw_itemsrL   	transportwriteencode)rG   rK   rM   keyvaluerequestr@   r@   rJ   write_http_requestf   s   z*WebSocketClientProtocol.write_http_requestTuple[int, Headers]c              
     s   zt | jI dH \}}}W n tjy     ty' } ztd|d}~ww | jrD| jd|| | D ]\}}| jd|| q7|| _	|| j	fS )a<  
        Read status line and headers from the HTTP response.

        If the response contains a body, it may be read from ``self.reader``
        after this coroutine returns.

        Raises:
            InvalidMessage: if the HTTP message is malformed or isn't an
                HTTP/1.1 GET response.

        Nz%did not receive a valid HTTP responsez< HTTP/1.1 %d %sz< %s: %s)
r,   readerasyncioCancelledError	Exceptionr   rO   r2   rP   response_headers)rG   status_codereasonrM   excrT   rU   r@   r@   rJ   read_http_responsez   s   

z*WebSocketClientProtocol.read_http_responserE   List[Extension]c           	   
   C  s   g }|  d}|rO|du rtdtdd |D g }|D ]1\}}|D ] }|j|kr+q#z|||}W n	 ty<   Y q#w ||  n
td| d| q|S )a/  
        Handle the Sec-WebSocket-Extensions HTTP response header.

        Check that each extension is supported, as well as its parameters.

        Return the list of accepted extensions.

        Raise :exc:`~websockets.exceptions.InvalidHandshake` to abort the
        connection.

        :rfc:`6455` leaves the rules up to the specification of each
        :extension.

        To provide this level of flexibility, for each extension accepted by
        the server, we check for a match with each extension available in the
        client configuration. If no match is found, an exception is raised.

        If several variants of the same extension are accepted by the server,
        it may be configured several times, which won't make sense in general.
        Extensions must implement their own requirements. For this purpose,
        the list of previously accepted extensions is provided.

        Other requirements, for example related to mandatory extensions or the
        order of extensions, may be implemented by overriding this method.

        Sec-WebSocket-ExtensionsNzno extensions supportedc                 S     g | ]}t |qS r@   )r   .0header_valuer@   r@   rJ   
<listcomp>       z>WebSocketClientProtocol.process_extensions.<locals>.<listcomp>zUnsupported extension: name = z, params = )get_allr   sumnameZprocess_response_paramsr   append)	rM   rE   Zaccepted_extensionsheader_valuesparsed_header_valuesrl   Zresponse_paramsextension_factory	extensionr@   r@   rJ   process_extensions   s<   



z*WebSocketClientProtocol.process_extensionsrF   Optional[Subprotocol]c                 C  s|   d}|  d}|r<|du rtdtdd |D g }t|dkr-d|}td| |d	 }||vr<td
| |S )z
        Handle the Sec-WebSocket-Protocol HTTP response header.

        Check that it contains exactly one supported subprotocol.

        Return the selected subprotocol.

        NSec-WebSocket-Protocolzno subprotocols supportedc                 S  rd   r@   )r    re   r@   r@   rJ   rh      ri   z?WebSocketClientProtocol.process_subprotocol.<locals>.<listcomp>r)   z, zmultiple subprotocols: r   zunsupported subprotocol: )rj   r   rk   lenjoinr   )rM   rF   subprotocolrn   ro   r5   r@   r@   rJ   process_subprotocol   s   

z+WebSocketClientProtocol.process_subprotocolwsurir'   c                   s.  t  }t|j|j|j|d< |jrt|j |d< |dur!||d< t|}|dur6tdd |D }||d< |durBt	|}	|	|d< | j
durM|| j
 |d	t | |j| |  I dH \}
}|
d
v rud|vrotdt|d |
dkr~t|
|t|| | ||| _| ||| _|   dS )a6  
        Perform the client side of the opening handshake.

        Args:
            wsuri: URI of the WebSocket server.
            origin: value of the ``Origin`` header.
            available_extensions: list of supported extensions, in order in
                which they should be tried.
            available_subprotocols: list of supported subprotocols, in order
                of decreasing preference.
            extra_headers: arbitrary HTTP headers to add to the request.

        Raises:
            InvalidHandshake: if the handshake fails.

        HostAuthorizationNr%   c                 S  s   g | ]	}|j | fqS r@   )rl   Zget_request_params)rf   rp   r@   r@   rJ   rh   0  s    z5WebSocketClientProtocol.handshake.<locals>.<listcomp>rc   rt   z
User-Agent)i-  i.  i/  i3  i4  ZLocatione   )r   r   hostportsecureZ	user_infor   r*   r   r   r6   update
setdefaultr"   rW   resource_namera   r   r   r   r+   rr   r4   rx   rw   Zconnection_open)rG   ry   r3   rE   rF   r6   rN   rT   Zextensions_headerZprotocol_headerr^   r]   r@   r@   rJ   	handshake  sN   


z!WebSocketClientProtocol.handshake)r2   r7   r3   r8   r4   r9   r5   r:   r6   r;   r<   r   r=   r>   )rK   rL   rM   r   r=   r>   )r=   rX   )rM   r   rE   r9   r=   rb   )rM   r   rF   r:   r=   rs   )NNNN)ry   r'   r3   r8   rE   r9   rF   r:   r6   r;   r=   r>   )__name__
__module____qualname____doc__Z	is_clientZsiderD   rW   ra   staticmethodrr   rx   r   __classcell__r@   r@   rH   rJ   r0   7   s*    

K&r0   c                   @  s   e Zd ZdZdZdddddddddddddddd	dFd)d*ZdGd+d,Zd-Zd.Zd/Z	d0Z
dHd2d3ZdId5d6ZdJd=d>ZdKd@dAZdIdBdCZdIdDdEZeZdS )LConnectaw  
    Connect to the WebSocket server at ``uri``.

    Awaiting :func:`connect` yields a :class:`WebSocketClientProtocol` which
    can then be used to send and receive messages.

    :func:`connect` can be used as a asynchronous context manager::

        async with websockets.connect(...) as websocket:
            ...

    The connection is closed automatically when exiting the context.

    :func:`connect` can be used as an infinite asynchronous iterator to
    reconnect automatically on errors::

        async for websocket in websockets.connect(...):
            try:
                ...
            except websockets.ConnectionClosed:
                continue

    The connection is closed automatically after each iteration of the loop.

    If an error occurs while establishing the connection, :func:`connect`
    retries with exponential backoff. The backoff delay starts at three
    seconds and increases up to one minute.

    If an error occurs in the body of the loop, you can handle the exception
    and :func:`connect` will reconnect with the next iteration; or you can
    let the exception bubble up and break out of the loop. This lets you
    decide which errors trigger a reconnection and which errors are fatal.

    Args:
        uri: URI of the WebSocket server.
        create_protocol: factory for the :class:`asyncio.Protocol` managing
            the connection; defaults to :class:`WebSocketClientProtocol`; may
            be set to a wrapper or a subclass to customize connection handling.
        logger: logger for this connection;
            defaults to ``logging.getLogger("websockets.client")``;
            see the :doc:`logging guide <../topics/logging>` for details.
        compression: shortcut that enables the "permessage-deflate" extension
            by default; may be set to :obj:`None` to disable compression;
            see the :doc:`compression guide <../topics/compression>` for details.
        origin: value of the ``Origin`` header. This is useful when connecting
            to a server that validates the ``Origin`` header to defend against
            Cross-Site WebSocket Hijacking attacks.
        extensions: list of supported extensions, in order in which they
            should be tried.
        subprotocols: list of supported subprotocols, in order of decreasing
            preference.
        extra_headers: arbitrary HTTP headers to add to the request.
        open_timeout: timeout for opening the connection in seconds;
            :obj:`None` to disable the timeout

    See :class:`~websockets.legacy.protocol.WebSocketCommonProtocol` for the
    documentation of ``ping_interval``, ``ping_timeout``, ``close_timeout``,
    ``max_size``, ``max_queue``, ``read_limit``, and ``write_limit``.

    Any other keyword arguments are passed the event loop's
    :meth:`~asyncio.loop.create_connection` method.

    For example:

    * You can set ``ssl`` to a :class:`~ssl.SSLContext` to enforce TLS
      settings. When connecting to a ``wss://`` URI, if ``ssl`` isn't
      provided, a TLS context is created
      with :func:`~ssl.create_default_context`.

    * You can set ``host`` and ``port`` to connect to a different host and
      port from those found in ``uri``. This only changes the destination of
      the TCP connection. The host name from ``uri`` is still used in the TLS
      handshake for secure connections and in the ``Host`` header.

    Returns:
        WebSocketClientProtocol: WebSocket connection.

    Raises:
        InvalidURI: if ``uri`` isn't a valid WebSocket URI.
        InvalidHandshake: if the opening handshake fails.
        ~asyncio.TimeoutError: if the opening handshake times out.

    
   Ndeflate   i       i   )create_protocolr2   compressionr3   r4   r5   r6   open_timeoutping_intervalping_timeoutclose_timeoutmax_size	max_queue
read_limitwrite_limiturirL   r   2Optional[Callable[[Any], WebSocketClientProtocol]]r2   r7   r   Optional[str]r3   r8   r4   r9   r5   r:   r6   r;   r   Optional[float]r   r   r   r   Optional[int]r   r   intr   r<   r   r=   r>   c                K  sB  | dd }|d u rd}ntdt |d u r|}| dd }|d u r&t}ntdt |d u r2|}| dd}| dd }|d u rGt }n|}td	t t|}|jr]|	d
d n|
d
d urhtd|dkrqt|}n|d ur|td| |d urt| tj|fi d|d|d|d|d|d|
d|d|d|d|d|d|d|jd|jd|jd|d|}| ddr| dd }tj|j||fi |}n,|
d d u r|j|j}}nd!\}}| d|}| d|}tj|j|||fi |}|	| _|d u rtd"}|| _|| _|| _|| _d S )#Ntimeoutr   zrename timeout to close_timeoutklasszrename klass to create_protocollegacy_recvFloopzremove loop argumentsslTzQconnect() received a ssl argument for a ws:// URI, use a wss:// URI to enable TLSr   zunsupported compression: r2   r3   r4   r5   r6   r   r   r   r   r   r   r   r}   r~   r   unixrK   sock)NNr?   )popwarningswarnDeprecationWarningr0   rZ   Zget_event_loopr(   r   r   get
ValueErrorr   r!   	functoolspartialr}   r~   Zcreate_unix_connectioncreate_connectionr   rA   rB   r2   _create_connection_uri_wsuri)rG   r   r   r2   r   r3   r4   r5   r6   r   r   r   r   r   r   r   r   r<   r   r   r   _loopr   ry   factoryrK   r   r}   r~   r@   r@   rJ   rD     s   

	




zConnect.__init__c                 C  s   | j }| j}tj||}t|}|jr|jstd|j|jko&|j	|j	k}|s[| j
jd }tj|jg|jR i t|j|j|j	d}tj| j
jg||j|j	fR i | j
j| _
|| _ || _d S )Nzredirect from WSS to WSr   )r}   r~   )r   r   urllibparseurljoinr(   r   r   r}   r~   r   argsr   r   funcdictkeywords)rG   r   Zold_uriZ	old_wsuriZnew_uriZ	new_wsuriZsame_originr   r@   r@   rJ   handle_redirect&  s4   
zConnect.handle_redirectgQ?g      N@gS?   &AsyncIterator[WebSocketClientProtocol]c              	   C s   | j }	 z!| 4 I d H }|V  W d   I d H  n1 I d H s w   Y  W nO tjy/     tyu   || j krSt | j }| jjd|dd t|I d H  n| jjdt	|dd tt	|I d H  || j
 }t|| j}Y qw | j }q)NTz.! connect failed; reconnecting in %.1f seconds)exc_infoz.! connect failed again; retrying in %d seconds)BACKOFF_MINrZ   r[   r\   randomBACKOFF_INITIALr2   infosleepr   BACKOFF_FACTORminBACKOFF_MAX)rG   Zbackoff_delayprotocolZinitial_delayr@   r@   rJ   	__aiter__R  s>   (

zConnect.__aiter__r0   c                   s   | I d H S Nr@   rG   r@   r@   rJ   
__aenter__x  s   
zConnect.__aenter__exc_typeOptional[Type[BaseException]]	exc_valueOptional[BaseException]	tracebackOptional[TracebackType]c                   s   | j  I d H  d S r   )r   close)rG   r   r   r   r@   r@   rJ   	__aexit__{  s   zConnect.__aexit__-Generator[Any, None, WebSocketClientProtocol]c                 C  s   |    S r   )__await_impl_timeout__	__await__r   r@   r@   rJ   r     s   zConnect.__await__c                   s   t |  | jI d H S r   )rZ   wait_for__await_impl__r   r   r@   r@   rJ   r     s   zConnect.__await_impl_timeout__c                   s   t | jD ]X}|  I d H \}}tt|}z0z|j| j|j|j|j	|j
dI d H  W n ty>   |  | I d H   w || _|W   S  ty^ } z| |j W Y d }~qd }~ww td)N)r3   rE   rF   r6   ztoo many redirects)rangeMAX_REDIRECTS_ALLOWEDr   r   r0   r   r   r3   rE   rF   r6   r\   Zfail_connectionZwait_closedr   r   r   r   r   )rG   Z	redirectsrQ   r   r`   r@   r@   rJ   r     s2   

zConnect.__await_impl__)$r   rL   r   r   r2   r7   r   r   r3   r8   r4   r9   r5   r:   r6   r;   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r<   r   r=   r>   )r   rL   r=   r>   )r=   r   )r=   r0   )r   r   r   r   r   r   r=   r>   )r=   r   )r   r   r   r   r   rD   r   r   r   r   r   r   r   r   r   r   r   __iter__r@   r@   r@   rJ   r   W  s>    T
x'

&




r   ws://localhost/rK   r   r   rL   r<   r   r=   c                 K  s   t d|| dd|S )a  
    Similar to :func:`connect`, but for connecting to a Unix socket.

    This function builds upon the event loop's
    :meth:`~asyncio.loop.create_unix_connection` method.

    It is only available on Unix.

    It's mainly useful for debugging servers listening on Unix sockets.

    Args:
        path: file system path to the Unix socket.
        uri: URI of the WebSocket server; the host is used in the TLS
            handshake for secure connections and in the ``Host`` header.

    T)r   rK   r   Nr@   )r.   )rK   r   r<   r@   r@   rJ   r/     s   r/   )Nr   )rK   r   r   rL   r<   r   r=   r   )B
__future__r   rZ   r   rA   r   urllib.parser   r   typesr   typingr   r   r   r   r   r	   r
   r   r   r   Zdatastructuresr   r   
exceptionsr   r   r   r   r   r   r   r4   r   r   Zextensions.permessage_deflater   rM   r   r   r   r   r   r    r!   httpr"   r#   r$   r%   r&   r   r'   r(   r   r*   r+   r,   r   r-   __all__r0   r   r.   r/   r@   r@   r@   rJ   <module>   s>    0$	$	  "  V