B
    ]bd                 @  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 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)List   )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:
        str: ``key`` that must be passed to :func:`check_response`.

    	websocketUpgrade
ConnectionzSec-WebSocket-Key13zSec-WebSocket-Version)r   )r   key r   :lib/python3.7/site-packages/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|y| d }W nX tk
r } ztd|W dd}~X Y n. t	k
r } ztdd|W dd}~X Y nX yt
j| dd}W n2 tjk
r0 } ztd||W dd}~X Y nX t|dkrJtd|y| d }W n\ tk
r } ztd|W dd}~X Y n0 t	k
r } ztdd|W dd}~X Y nX |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  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)lower)r   r   r   r   r   	<genexpr>>   s    z check_request.<locals>.<genexpr>z, c             S  s   g | ]}t |qS r   )r   )r   r   r   r   r   r    B   s    r      r   r   zSec-WebSocket-KeyNz,more than one Sec-WebSocket-Key header foundT)Zvalidate   zSec-WebSocket-Versionz0more than one Sec-WebSocket-Version header foundr   )sumget_allanyr	   joinlenr"   KeyErrorr   r   base64Z	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   zSec-WebSocket-AcceptN)accept)r   r   r   r   r   r   j   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|y| d }W nX tk
r } ztd|W dd}~X Y n. t	k
r } ztdd|W d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   r       s    z"check_response.<locals>.<listcomp>r   c             s  s   | ]}|  d kV  qdS )r!   N)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   r       s    r   r$   r   r   z, zSec-WebSocket-AcceptNz/more than one Sec-WebSocket-Accept header found)r&   r'   r(   r	   r)   r*   r"   r+   r   r   r3   r   )r   r   r0   r!   Z
s_w_acceptr1   r   r   r   r   z   s$    )Z
__future__r   r,   r.   typingr   Zdatastructuresr   r   
exceptionsr   r   r	   r   r
   r   r   r   Zutilsr   r3   r   __all__r   r   r   r   r   r   r   r   <module>   s   D