
    g                        d dl mZ d dlZd dlZddlmZmZ ddlmZm	Z	m
Z
 ddlmZmZ 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   )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:
        ``key`` that must be passed to :func:`check_response`.

    	websocketUpgrade
ConnectionSec-WebSocket-Key13Sec-WebSocket-Version)r   r   keys     g/var/www/html/nodeJS/PythonScripts/venv3.11/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 )ab  
    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:
        ``key`` that must be passed to :func:`build_response`.

    Raises:
        InvalidHandshake: If the handshake request is invalid.
            Then, the server must return a 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>A        FFF%u		FFFr!   r      r   r   r   Nmultiple valuesT)validate   r   r   )sumget_allanyr	   joinlenr/   KeyErrorr   r   base64	b64decodeencodebinasciiErrorr   )r   
connectionr-   s_w_keyexcraw_keys_w_versions          r    r   r   %   sm   ( *-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		'(:(:;;;M-. : : :/00c9 M M M/1BCCLMH"7>>#3#3dCCC> H H H !4g>>CGH
7||r !4g>>>Q56 > > >344#= Q Q Q35FGGSPQ 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   e   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    r0   z!check_response.<locals>.<genexpr>   r1   r!    c                ,    g | ]}t          |          S r$   r4   r&   s     r    r)   z"check_response.<locals>.<listcomp>   r5   r!   r   r6   r   r   r2   rL   Nr7   )r:   r;   r<   r	   r=   r>   r/   r?   r   r   rM   r   )r   r   rE   r-   
s_w_acceptrG   s         r    r   r   u   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		'(:(:;;;P34

 = = =233< P P P24EFFCOP 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   datastructuresr   r   
exceptionsr   r   r	   r   r
   r   typingr   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   *= = = =@2 2 2 2 )E )E )E )E )E )Er!   