o
    Ð­j¥  ã                   @  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   úX/var/www/html/CropPilot/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r”td|ƒ‚z| d }W n" ty« } ztdƒ|‚d}~w t	y¼ } ztddƒ|‚d}~w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                 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    


@