o
    [hd                     @  s   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dZdddZdddZd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                 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:
        str: ``key`` that must be passed to :func:`check_response`.

    	websocketUpgrade
ConnectionSec-WebSocket-Key13Sec-WebSocket-Version)r   r   key r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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 )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                 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&   B   r'   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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   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   j   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   r0   r#   r   r   r    r&      r'   r   r1   r   r   r/   rB   Nz/more than one Sec-WebSocket-Accept header found)r4   r5   r6   r	   r7   r8   r,   r9   r   r   rC   r   )r   r   r?   r*   Z
s_w_acceptr@   r   r   r    r   z   s4   

r   )r   r   r   r   )r   r   r   r   r   rA   )
__future__r   r:   r=   typingr   Zdatastructuresr   r   
exceptionsr   r   r	   r   r
   r   r   r   utilsr   rC   r   __all__r   r   r   r   r   r   r   r    <module>   s    


D