o
    _cm.                     @  s  d dl mZ d dlmZmZmZmZ ddl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 ddlmZmZ ddl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+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1T dgZ2G dd deZ3dS )    )annotations)	GeneratorListOptionalSequence   )CLIENT
CONNECTINGOPEN
ConnectionState)HeadersMultipleValuesError)InvalidHandshakeInvalidHeaderInvalidHeaderValueInvalidStatusInvalidUpgradeNegotiationError)ClientExtensionFactory	Extension)build_authorization_basicbuild_extension
build_hostbuild_subprotocolparse_connectionparse_extensionparse_subprotocolparse_upgrade)RequestResponse)ConnectionOptionExtensionHeader
LoggerLikeOriginSubprotocolUpgradeProtocol)WebSocketURI)
accept_keygenerate_key)*ClientConnectionc                      sp   e Zd ZdZdddeddfd+ fddZd,ddZd-ddZd.d d!Zd/d#d$Z	d0d&d'Z
d1 fd)d*Z  ZS )2r+   a  
    Sans-I/O implementation of a WebSocket client connection.

    Args:
        wsuri: URI of the WebSocket server, parsed
            with :func:`~websockets.uri.parse_uri`.
        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.
        state: initial state of the WebSocket connection.
        max_size: maximum size of incoming messages in bytes;
            :obj:`None` to disable the limit.
        logger: logger for this connection;
            defaults to ``logging.getLogger("websockets.client")``;
            see the :doc:`logging guide <../topics/logging>` for details.

    Ni   wsurir'   originOptional[Origin]
extensions*Optional[Sequence[ClientExtensionFactory]]subprotocolsOptional[Sequence[Subprotocol]]stater   max_sizeOptional[int]loggerOptional[LoggerLike]c                   s8   t  jt|||d || _|| _|| _|| _t | _d S )N)Zsider3   r4   r6   )	super__init__r   r,   r-   available_extensionsavailable_subprotocolsr)   key)selfr,   r-   r/   r1   r3   r4   r6   	__class__ </usr/local/lib/python3.10/dist-packages/websockets/client.pyr9   E   s   
zClientConnection.__init__returnr   c                 C  s   t  }t| jj| jj| jj|d< | jjrt| jj |d< | jdur'| j|d< d|d< d|d< | j	|d< d	|d
< | j
durKtdd | j
D }||d< | jdurYt| j}||d< t| jj|S )a:  
        Create a handshake request to open a connection.

        You must send the handshake request with :meth:`send_request`.

        You can modify it before sending it, for example to add HTTP headers.

        Returns:
            Request: WebSocket handshake request event to send to the server.

        HostAuthorizationNr$   	websocketUpgrader   zSec-WebSocket-KeyZ13zSec-WebSocket-Versionc                 S  s   g | ]	}|j | fqS r@   )nameZget_request_params).0extension_factoryr@   r@   rA   
<listcomp>z   s    z,ClientConnection.connect.<locals>.<listcomp>Sec-WebSocket-ExtensionsSec-WebSocket-Protocol)r   r   r,   hostportsecureZ	user_infor   r-   r<   r:   r   r;   r   r   resource_name)r=   headersZextensions_headerZprotocol_headerr@   r@   rA   connect[   s.   





zClientConnection.connectresponser    Nonec              
   C  s4  |j dkr	t||j}tdd |dD g }tdd |D s/td|r,d|dtd	d |d
D g }t|dkrJ|d 	 dksWtd
|rTd|dz|d }W n" t
yn } ztd|d}~w ty } ztdd|d}~ww |t| jkrtd|| || _| || _dS )z
        Check a handshake response.

        Args:
            request: WebSocket handshake response received from the server.

        Raises:
            InvalidHandshake: if the handshake response is invalid.

        e   c                 S     g | ]}t |qS r@   )r   rH   valuer@   r@   rA   rJ          z5ClientConnection.process_response.<locals>.<listcomp>r   c                 s  s    | ]	}|  d kV  qdS )upgradeN)lowerrW   r@   r@   rA   	<genexpr>   s    z4ClientConnection.process_response.<locals>.<genexpr>, Nc                 S  rV   r@   )r   rW   r@   r@   rA   rJ      rY   rF   r   r   rE   zSec-WebSocket-Acceptz/more than one Sec-WebSocket-Accept header found)status_coder   rQ   sumget_allanyr   joinlenr[   KeyErrorr   r   r(   r<   r   process_extensionsr/   process_subprotocolsubprotocol)r=   rS   rQ   
connectionrZ   Z
s_w_acceptexcr@   r@   rA   process_response   sF   


z!ClientConnection.process_responserQ   r   List[Extension]c           	   
   C  s   g }| d}|rQ| jdu rtdtdd |D g }|D ]2\}}| j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.

        :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.

        Args:
            headers: WebSocket handshake response headers.

        Returns:
            List[Extension]: List of accepted extensions.

        Raises:
            InvalidHandshake: to abort the handshake.

        rK   Nzno extensions supportedc                 S  rV   r@   )r   rH   header_valuer@   r@   rA   rJ      rY   z7ClientConnection.process_extensions.<locals>.<listcomp>zUnsupported extension: name = z, params = )r`   r:   r   r_   rG   Zprocess_response_paramsr   append)	r=   rQ   Zaccepted_extensionsr/   Zparsed_extensionsrG   Zresponse_paramsrI   	extensionr@   r@   rA   re      s<   





z#ClientConnection.process_extensionsOptional[Subprotocol]c                 C  s   d}| d}|r>| jdu rtdtdd |D g }t|dkr.d|}td| |d	 }|| jvr>td
| |S )a:  
        Handle the Sec-WebSocket-Protocol HTTP response header.

        If provided, check that it contains exactly one supported subprotocol.

        Args:
            headers: WebSocket handshake response headers.

        Returns:
           Optional[Subprotocol]: Subprotocol, if one was selected.

        NrL   zno subprotocols supportedc                 S  rV   r@   )r   rl   r@   r@   rA   rJ     rY   z8ClientConnection.process_subprotocol.<locals>.<listcomp>r   r]   zmultiple subprotocols: r   zunsupported subprotocol: )r`   r;   r   r_   rc   rb   r   )r=   rQ   rg   r1   Zparsed_subprotocolsZsubprotocols_displayr@   r@   rA   rf     s   



z$ClientConnection.process_subprotocolrequestc                 C  sN   | j r| j d|j |j D ]\}}| j d|| q| j|  dS )z
        Send a handshake request to the server.

        Args:
            request: WebSocket handshake request event.

        z> GET %s HTTP/1.1z> %s: %sN)debugr6   pathrQ   	raw_itemsZwritesrn   	serialize)r=   rq   r<   rX   r@   r@   rA   send_request,  s
   zClientConnection.send_requestGenerator[None, None, None]c              
   #  s.   | j tu rt| jj| jj| jjE d H }| jrI|j	|j
}}| jd|| |j D ]\}}| jd|| q-|jd urI| jdt|j z<z| | W n" tys } z||_|| _|  | _t| j W Y d }~nd }~ww | j tu s{J t| _ W | j| n| j| w t  E d H  d S )Nz< HTTP/1.1 %d %sz< %s: %sz< [body] (%d bytes))r3   r	   r    parsereaderZ	read_lineZ
read_exactZread_to_eofrr   r^   Zreason_phraser6   rQ   rt   bodyrc   rj   r   
_exceptionhandshake_excdiscardparsernextr
   eventsrn   r8   )r=   rS   codephraser<   rX   ri   r>   r@   rA   rx   ;  s8   



zClientConnection.parse)r,   r'   r-   r.   r/   r0   r1   r2   r3   r   r4   r5   r6   r7   )rB   r   )rS   r    rB   rT   )rQ   r   rB   rk   )rQ   r   rB   rp   )rq   r   rB   rT   )rB   rw   )__name__
__module____qualname____doc__r	   r9   rR   rj   re   rf   rv   rx   __classcell__r@   r@   r>   rA   r+   .   s    

,
4
L
%N)4
__future__r   typingr   r   r   r   rh   r   r	   r
   r   r   Zdatastructuresr   r   
exceptionsr   r   r   r   r   r   r/   r   r   rQ   r   r   r   r   r   r   r   r   Zhttp11r   r    r!   r"   r#   r$   r%   r&   urir'   utilsr(   r)   Zlegacy.client__all__r+   r@   r@   r@   rA   <module>   s     (
 