
    &thd                        d dl mZ d dlZd dlZd dlmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZmZ ddlmZmZ dd	lmZmZ g d
ZddZddZddZddZdS )    )annotationsN)List   )HeadersMultipleValuesError)InvalidHeaderInvalidHeaderValueInvalidUpgrade)parse_connectionparse_upgrade)ConnectionOptionUpgradeProtocol)
accept_keygenerate_key)build_requestcheck_requestbuild_responsecheck_responseheadersr   returnstrc                J    t                      }d| d<   d| d<   || d<   d| d<   |S )z
    Build a handshake request to send to the server.

    Update request headers passed in argument.

    Args:
        headers: handshake request headers.

    Returns:
        str: ``key`` that must be passed to :func:`check_response`.

    	websocketUpgrade
ConnectionSec-WebSocket-Key13Sec-WebSocket-Version)r   r   keys     o/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/websockets/legacy/handshake.pyr   r      s<     ..C$GI%GL#&G '+G#$J    c                   t          d |                     d          D             g           }t          d |D                       s#t          dd                    |                    t          d |                     d          D             g           }t          |          dk    r|d                                         d	k    s#t          dd                    |                    	 | d
         }n@# t          $ r}t          d
          |d}~wt          $ r}t          d
d          |d}~ww xY w	 t          j        |                                d          }n(# t          j        $ r}t          d
|          |d}~ww xY wt          |          dk    rt          d
|          	 | d         }n@# t          $ r}t          d          |d}~wt          $ r}t          dd          |d}~ww xY w|dk    rt          d|          |S )ad  
    Check a handshake request received from the client.

    This function doesn't verify that the request is an HTTP/1.1 or higher GET
    request and doesn't perform ``Host`` and ``Origin`` checks. These controls
    are usually performed earlier in the HTTP request handling code. They're
    the responsibility of the caller.

    Args:
        headers: handshake request headers.

    Returns:
        str: ``key`` that must be passed to :func:`build_response`.

    Raises:
        InvalidHandshake: if the handshake request is invalid;
            then the server must return 400 Bad Request error.

    c                ,    g | ]}t          |          S  r   .0values     r!   
<listcomp>z!check_request.<locals>.<listcomp>;   !    LLLU	%	 	 LLLr"   r   c              3  F   K   | ]}|                                 d k    V  dS upgradeNlowerr'   s     r!   	<genexpr>z check_request.<locals>.<genexpr>>   /      BBeu{{}}	)BBBBBBr"   , c                ,    g | ]}t          |          S r%   r   r'   s     r!   r*   z!check_request.<locals>.<listcomp>B        FFF%u		FFFr"   r      r   r   r   Nz,more than one Sec-WebSocket-Key header foundT)validate   r   z0more than one Sec-WebSocket-Version header foundr   )sumget_allanyr
   joinlenr0   KeyErrorr   r   base64	b64decodeencodebinasciiErrorr	   )r   
connectionr.   s_w_keyexcraw_keys_w_versions          r!   r   r   &   sq   ( *-LLgool.K.KLLLb* *J BBzBBBBB B\499Z+@+@AAA%(FF7??9+E+EFFF& &G LLA'!*"2"2"4"4"C"CY		'(:(:;;;-. : : :/00c9   !O
 
	
H"7>>#3#3dCCC> H H H !4g>>CGH
7||r !4g>>>56 > > >344#=   #%W
 
	
 d !8+FFFNs`   ,C5 5
D2?DD2D--D26(E F.E??F+F4 4
G1>GG1G,,G1r    Nonec                >    d| d<   d| d<   t          |          | d<   dS )z
    Build a handshake response to send to the client.

    Update response headers passed in argument.

    Args:
        headers: handshake response headers.
        key: returned by :func:`check_request`.

    r   r   r   Sec-WebSocket-AcceptN)acceptr   s     r!   r   r   j   s/     %GI%GL&,SkkG"###r"   c                   t          d |                     d          D             g           }t          d |D                       s#t          dd                    |                    t          d |                     d          D             g           }t          |          dk    r|d                                         d	k    s#t          dd
                    |                    	 | d         }n@# t          $ r}t          d          |d}~wt          $ r}t          dd          |d}~ww xY w|t          |          k    rt          d|          dS )a  
    Check a handshake response received from the server.

    This function doesn't verify that the response is an HTTP/1.1 or higher
    response with a 101 status code. These controls are the responsibility of
    the caller.

    Args:
        headers: handshake response headers.
        key: returned by :func:`build_request`.

    Raises:
        InvalidHandshake: if the handshake response is invalid.

    c                ,    g | ]}t          |          S r%   r&   r'   s     r!   r*   z"check_response.<locals>.<listcomp>   r+   r"   r   c              3  F   K   | ]}|                                 d k    V  dS r-   r/   r'   s     r!   r1   z!check_response.<locals>.<genexpr>   r2   r"    c                ,    g | ]}t          |          S r%   r5   r'   s     r!   r*   z"check_response.<locals>.<listcomp>   r6   r"   r   r7   r   r   r3   rL   Nz/more than one Sec-WebSocket-Accept header found)r:   r;   r<   r
   r=   r>   r0   r?   r   r   rM   r	   )r   r    rE   r.   
s_w_acceptrG   s         r!   r   r   z   s     *-LLgool.K.KLLLb* *J BBzBBBBB A\388J+?+?@@@%(FF7??9+E+EFFF& &G LLA'!*"2"2"4"4"C"CY		'(:(:;;;34

 = = =233<   "$U
 
	
 VC[[   !7DDD ! s$   ,C5 5
D2?DD2D--D2)r   r   r   r   )r   r   r    r   r   rJ   )
__future__r   r@   rC   typingr   datastructuresr   r   
exceptionsr   r	   r
   r   r   r   r   r   utilsr   rM   r   __all__r   r   r   r   r%   r"   r!   <module>rZ      s,   " " " " " "         9 9 9 9 9 9 9 9 J J J J J J J J J J 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 Q
P
P   *A A A AH2 2 2 2 +E +E +E +E +E +Er"   