U
    œËúg   ã                   @   sâ   d Z ddlZddlZddlZddlmZmZmZmZ ddl	m
Z ddlmZ ddlm
Z
 dZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZee
ef eeef dœdd„Zeee dœdd„Zedœdd„Zeedœdd„ZdS )za
wsproto/utilities
~~~~~~~~~~~~~~~~~

Utility functions that do not belong in a separate module.
é    N)ÚDictÚListÚOptionalÚUnion)ÚHeadersé   )ÚEvents$   258EAFA5-E914-47DA-95CA-C5AB0DC85B11c                   @   s   e Zd ZdS )ÚProtocolErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú5/tmp/pip-unpacked-wheel-u83us8q9/wsproto/utilities.pyr	      s   r	   c                   @   s   e Zd ZdZdS )ÚLocalProtocolErrorzÄIndicates an error due to local/programming errors.

    This is raised when the connection is asked to do something that
    is either incompatible with the state or the websocket standard.

    N)r
   r   r   Ú__doc__r   r   r   r   r      s   r   c                       s0   e Zd ZdZdeee ddœ‡ fdd„Z‡  ZS )ÚRemoteProtocolErroram  Indicates an error due to the remote's actions.

    This is raised when processing the bytes from the remote if the
    remote has sent data that is incompatible with the websocket
    standard.

    .. attribute:: event_hint

       This is a suggested wsproto Event to send to the client based
       on the error. It could be None if no hint is available.

    N)ÚmessageÚ
event_hintÚreturnc                    s   || _ tƒ  |¡ d S ©N)r   ÚsuperÚ__init__)Úselfr   r   ©Ú	__class__r   r   r   2   s    zRemoteProtocolError.__init__)N)	r
   r   r   r   Ústrr   r   r   Ú__classcell__r   r   r   r   r   $   s   r   )Úh11_headersr   c                 C   sL   i }| D ]\}}|  |g ¡ |¡ qi }| ¡ D ]\}}d |¡||< q0|S )Ns   , )Ú
setdefaultÚappendÚitemsÚjoin)r   Zname_to_valuesÚnameÚvalueZname_to_normed_valueÚvaluesr   r   r   Únormed_header_dict8   s    r%   )r#   r   c                 C   s   dd„ |   d¡D ƒS )Nc                 S   s   g | ]}|  d ¡ ¡ ‘qS )Úascii)ÚdecodeÚstrip)Ú.0Zpiecer   r   r   Ú
<listcomp>L   s     z&split_comma_header.<locals>.<listcomp>ó   ,)Úsplit)r#   r   r   r   Úsplit_comma_headerK   s    r-   )r   c                   C   s   t  t d¡¡S )Né   )Úbase64Ú	b64encodeÚosÚurandomr   r   r   r   Úgenerate_nonceO   s    r3   )Útokenr   c                 C   s    | t  }t |¡ ¡ }t |¡S r   )ÚACCEPT_GUIDÚhashlibÚsha1Údigestr/   r0   )r4   Zaccept_tokenr   r   r   Úgenerate_accept_tokenU   s    r9   )r   r/   r6   r1   Útypingr   r   r   r   Zh11._headersr   Z
H11HeadersÚeventsr   r5   Ú	Exceptionr	   r   r   Úbytesr%   r   r-   r3   r9   r   r   r   r   Ú<module>   s    