o
    KK&h                     @  s   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dZdddZdddZd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                 C  s*   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   key r   N/var/www/html/venv/lib/python3.10/site-packages/websockets/legacy/handshake.pyr      s   r   c              
   C  s  t dd | dD g }tdd |D stdd|t dd | dD g }t|d	kr9|d
  dksAtdd|z| d }W n" tyX } ztd|d}~w t	yi } ztdd|d}~ww zt
j| dd}W n tjy } ztd||d}~ww t|dkrtd|z| d }W n" ty } ztd|d}~w t	y } ztdd|d}~ww |dkrt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                 S     g | ]}t |qS r   r	   .0valuer   r   r   
<listcomp>:       z!check_request.<locals>.<listcomp>r   c                 s      | ]	}|  d kV  qdS upgradeNlowerr"   r   r   r   	<genexpr>=       z check_request.<locals>.<genexpr>, c                 S  r    r   r
   r"   r   r   r   r%   A   r&   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_versionr   r   r   r   %   sN   



r   r   Nonec                 C  s    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   r   r   r   r   e   s   r   c              
   C  s   t dd | dD g }tdd |D stdd|t dd | dD g }t|d	kr9|d
  dksAtdd|z| d }W n" tyX } ztd|d}~w t	yi } ztdd|d}~ww |t
|krut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                 S  r    r   r!   r"   r   r   r   r%      r&   z"check_response.<locals>.<listcomp>r   c                 s  r'   r(   r*   r"   r   r   r   r,      r-   z!check_response.<locals>.<genexpr> c                 S  r    r   r/   r"   r   r   r   r%      r&   r   r0   r   r   r.   rE   Nr1   )r4   r5   r6   r   r7   r8   r+   r9   r   r   rF   r   )r   r   r?   r)   
s_w_acceptrA   r   r   r   r   u   s,   

r   )r   r   r   r   )r   r   r   r   r   rD   )
__future__r   r:   r=   datastructuresr   r   
exceptionsr   r   r   r   r	   r
   typingr   r   utilsr   rF   r   __all__r   r   r   r   r   r   r   r   <module>   s    


@