a
    њ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ddZd
ddddZd
dddddZd
dd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_responser   str)headersreturnc                 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   V/wd/webapps/venvs/v2025_4um/lib/python3.9/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rr|d
  dkstdd|z| d }W n\ ty } ztd|W Y d}~n8d}~0  t	y } ztdd|W Y d}~n
d}~0 0 zt
j| dd}W n4 tjy6 } ztd||W Y d}~n
d}~0 0 t|dkrPtd|z| d }W n` ty } ztd|W Y d}~n:d}~0  t	y } ztdd|W Y d}~n
d}~0 0 |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  s   g | ]}t |qS r   r	   .0valuer   r   r   
<listcomp>:       z!check_request.<locals>.<listcomp>r   c                 s  s   | ]}|  d kV  qdS upgradeNlowerr!   r   r   r   	<genexpr>=   r%   z check_request.<locals>.<genexpr>, c                 S  s   g | ]}t |qS 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'   Zs_w_keyexcZraw_keyZs_w_versionr   r   r   r   %   s>    ""
"

r   None)r   r   r   c                 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rr|d
  dkstdd|z| d }W n\ ty } ztd|W Y d}~n8d}~0  t	y } ztdd|W Y d}~n
d}~0 0 |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                 S  s   g | ]}t |qS r   r    r!   r   r   r   r$      r%   z"check_response.<locals>.<listcomp>r   c                 s  s   | ]}|  d kV  qdS r&   r(   r!   r   r   r   r*      r%   z!check_response.<locals>.<genexpr> c                 S  s   g | ]}t |qS r   r,   r!   r   r   r   r$      r%   r   r-   r   r   r+   r?   Nr.   )r1   r2   r3   r   r4   r5   r)   r6   r   r   r@   r   )r   r   r<   r'   Z
s_w_acceptr=   r   r   r   r   u   s$    "r   )
__future__r   r7   r:   Zdatastructuresr   r   
exceptionsr   r   r   r   r	   r
   typingr   r   utilsr   r@   r   __all__r   r   r   r   r   r   r   r   <module>   s   @