U
    g                     @  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 d	d
ddg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   ?/tmp/pip-unpacked-wheel-dx_q7dq3/websockets/legacy/handshake.pyr      s    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 nX tk
r } ztd|W 5 d}~X Y n. t	k
r } ztdd|W 5 d}~X Y nX zt
j| dd}W n2 tjk
r0 } ztd||W 5 d}~X Y nX t|dkrJtd|z| d }W n\ tk
r } ztd|W 5 d}~X Y n0 t	k
r } ztdd|W 5 d}~X Y nX |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>:   s     z!check_request.<locals>.<listcomp>r   c                 s  s   | ]}|  d kV  qdS upgradeNlowerr!   r   r   r   	<genexpr>=   s     z check_request.<locals>.<genexpr>, c                 S  s   g | ]}t |qS r   r
   r!   r   r   r   r$   A   s     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   %   sB      


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    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 nX tk
r } ztd|W 5 d}~X Y n. t	k
r } ztdd|W 5 d}~X Y nX |t
|krt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$      s     z"check_response.<locals>.<listcomp>r   c                 s  s   | ]}|  d kV  qdS r%   r'   r!   r   r   r   r)      s     z!check_response.<locals>.<genexpr> c                 S  s   g | ]}t |qS r   r+   r!   r   r   r   r$      s     r   r,   r   r   r*   r>   Nr-   )r0   r1   r2   r   r3   r4   r(   r5   r   r   r?   r   )r   r   r;   r&   Z
s_w_acceptr<   r   r   r   r   u   s(      )
__future__r   r6   r9   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   @