o
    mh7                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZ ddlmZmZ ddlmZ zddlmZ W n eyN   ddlmZ Y nw g dZG d	d
 d
ejZejZejZejZejZejZ ej!Z"eeefZ#ee e"fZ$G dd dejZ%e%j&de%j'de%j(de%j)de%j*de%j+de%j,de%j-de%j.de%j/de%j0de%j1de%j2de%j3de%j4diZ5de6d< e%j&e%j'e%j(e%j)e%j,e%j-e%j.e%j/e%j0e%j1e%j2e%j3hZ7e%j&e%j'e%j*hZ8e9e:e;fZ<ej=G dd dZ>d+d$d%Z?d,d'd(Z@ej=G d)d* d*ZAdS )-    )annotationsN)Callable	GeneratorSequence   )
exceptions
extensions)Data)
apply_mask)OpcodeOP_CONTOP_TEXT	OP_BINARYOP_CLOSEOP_PINGOP_PONGDATA_OPCODESCTRL_OPCODESFrameprepare_dataprepare_ctrlClosec                   @  s$   e Zd ZdZd\ZZZd\ZZZ	dS )r   z#Opcode values for WebSocket frames.)r   r      )   	   
   N)
__name__
__module____qualname____doc__CONTTEXTBINARYCLOSEPINGPONG r&   r&   I/var/www/html/zoom/venv/lib/python3.10/site-packages/websockets/frames.pyr   &   s    
r   c                   @  sL   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdS )	CloseCodez-Close code values for WebSocket close frames.i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  N)r   r   r   r   NORMAL_CLOSURE
GOING_AWAYPROTOCOL_ERRORUNSUPPORTED_DATANO_STATUS_RCVDABNORMAL_CLOSUREINVALID_DATAPOLICY_VIOLATIONMESSAGE_TOO_BIGMANDATORY_EXTENSIONINTERNAL_ERRORSERVICE_RESTARTTRY_AGAIN_LATERBAD_GATEWAYTLS_HANDSHAKEr&   r&   r&   r'   r(   8   s"    r(   OKz
going awayzprotocol errorzunsupported datazno status received [internal]zabnormal closure [internal]zinvalid frame payload datazpolicy violationzmessage too bigzmandatory extensionzinternal errorzservice restartztry again laterzbad gatewayz TLS handshake failure [internal]zdict[int, str]CLOSE_CODE_EXPLANATIONSc                   @  s   e Zd ZU dZded< ded< dZded< d	Zded
< d	Zded< d	Zded< e	e
jddZd%ddZedddd&ddZddd'd d!Zd(d#d$ZdS ))r   aN  
    WebSocket frame.

    Attributes:
        opcode: Opcode.
        data: Payload data.
        fin: FIN bit.
        rsv1: RSV1 bit.
        rsv2: RSV2 bit.
        rsv3: RSV3 bit.

    Only these fields are needed. The MASK bit, payload length and masking-key
    are handled on the fly when parsing and serializing frames.

    r   opcodebytesdataTboolfinFrsv1rsv2rsv3WEBSOCKETS_MAX_LOG_SIZE75returnstrc              
   C  s  d}t | j dt | jdkrdnd }| jrdnd}| jtu r)t| j }n| jtu ra| j}t || jd krV| jd d d }d	|dd	|  d
|| d g}d	dd |D }n^| jt
u rott| j}nP| jrzt| j }d}W nA ttfy   | j}t || jd kr| jd d d }d	|dd	|  d
|| d g}d	dd |D }d}Y nw d}t || jkr| jd d }|dd	|  d || d  }d	td|||g}| jj d| d| dS )zE
        Return a human-readable representation of a frame.

        Nz byter    s	continued       r   s      c                 s      | ]}|d V  qdS 02xNr&   .0byter&   r&   r'   	<genexpr>       z Frame.__str__.<locals>.<genexpr>textc                 s  rL   rM   r&   rO   r&   r&   r'   rR      rS   binaryz''z...z, z [])lenr<   r>   r:   r   reprdecoder   MAX_LOG_SIZEjoinr   rE   r   parseUnicodeDecodeErrorAttributeErrorfiltername)selfcodinglength	non_finalr<   rU   cutmetadatar&   r&   r'   __str__   s>   &

&
&"zFrame.__str__N)max_sizer   
read_exact-Callable[[int], Generator[None, None, bytes]]maskrh   
int | Noner   %Sequence[extensions.Extension] | NoneGenerator[None, None, Frame]c             
   c  s   |dE dH }t d|\}}|d@ rdnd}|d@ rdnd}	|d@ r&dnd}
|d	@ r.dnd}zt|d
@ }W n tyJ } ztd|d}~ww |d@ rQdnd|krZtd|d@ }|dkrq|dE dH }t d|\}n|dkr|dE dH }t d|\}|dur||krtd| d| d|r|dE dH }||E dH }|rt||}| ||||	|
|}|du rg }t|D ]	}|j	||d}q|
  |S )a&  
        Parse a WebSocket frame.

        This is a generator-based coroutine.

        Args:
            read_exact: Generator-based coroutine that reads the requested
                bytes or raises an exception if there isn't enough data.
            mask: Whether the frame should be masked i.e. whether the read
                happens on the server side.
            max_size: Maximum payload size in bytes.
            extensions: List of extensions, applied in reverse order.

        Raises:
            EOFError: If the connection is closed without a full WebSocket frame.
            UnicodeDecodeError: If the frame contains invalid UTF-8.
            PayloadTooBig: If the frame's payload size exceeds ``max_size``.
            ProtocolError: If the frame contains incorrect values.

        r   N!BB   TF@             zinvalid opcodezincorrect masking   ~   !Hr   z!Qzover size limit (z > z bytes)   )rh   )structunpackr   
ValueErrorr   ProtocolErrorPayloadTooBigr
   reversedrY   check)clsri   rk   rh   r   r<   head1head2r>   r?   r@   rA   r:   excrc   
mask_bytesframe	extensionr&   r&   r'   r\      sL   

zFrame.parse)r   c          
      C  s&  |    |du r
g }|D ]}|| } qt }| jrdnd| jr#dndB | jr*dndB | jr1dndB | jB }|r;dnd}t	| j
}|dk rS|td|||B  n|d	k re|td
||dB | n|td||dB | |r~td}|| |rt| j
|}	n| j
}	||	 | S )aH  
        Serialize a WebSocket frame.

        Args:
            mask: Whether the frame should be masked i.e. whether the write
                happens on the client side.
            extensions: List of extensions, applied in order.

        Raises:
            ProtocolError: If the frame contains incorrect values.

        Nrp   r   rq   rr   rs   rv   ro   i   z!BBHz!BBQru   rx   )r   encodeioBytesIOr>   r?   r@   rA   r:   rW   r<   writery   packsecretstoken_bytesr
   getvalue)
ra   rk   r   r   outputr   r   rc   r   r<   r&   r&   r'   	serialize  s>   



zFrame.serializeNonec                 C  sV   | j s	| js	| jrtd| jtv r't| jdkrtd| j	s)tddS dS )z
        Check that reserved bits and opcode have acceptable values.

        Raises:
            ProtocolError: If a reserved bit or the opcode is invalid.

        zreserved bits must be 0}   zcontrol frame too longzfragmented control frameN)
r?   r@   rA   r   r|   r:   r   rW   r<   r>   ra   r&   r&   r'   r   S  s   



zFrame.checkrD   rE   )
ri   rj   rk   r=   rh   rl   r   rm   rD   rn   )rk   r=   r   rm   rD   r;   rD   r   )r   r   r   r   __annotations__r>   r?   r@   rA   intosenvirongetrZ   rg   classmethodr\   r   r   r&   r&   r&   r'   r   }   s"   
 
2P;r   r<   r	   rD   tuple[int, bytes]c                 C  s0   t | trt|  fS t | trt| fS td)a  
    Convert a string or byte-like object to an opcode and a bytes-like object.

    This function is designed for data frames.

    If ``data`` is a :class:`str`, return ``OP_TEXT`` and a :class:`bytes`
    object encoding ``data`` in UTF-8.

    If ``data`` is a bytes-like object, return ``OP_BINARY`` and a bytes-like
    object.

    Raises:
        TypeError: If ``data`` doesn't have a supported type.

    data must be str or bytes-like)
isinstancerE   r   r   	BytesLiker   	TypeErrorr<   r&   r&   r'   r   e  s
   

r   r;   c                 C  s,   t | tr	|  S t | trt| S td)ai  
    Convert a string or byte-like object to bytes.

    This function is designed for ping and pong frames.

    If ``data`` is a :class:`str`, return a :class:`bytes` object encoding
    ``data`` in UTF-8.

    If ``data`` is a bytes-like object, return a :class:`bytes` object.

    Raises:
        TypeError: If ``data`` doesn't have a supported type.

    r   )r   rE   r   r   r;   r   r   r&   r&   r'   r   }  s
   

r   c                   @  sN   e Zd ZU dZded< ded< dddZedddZdddZdddZ	dS )r   z
    Code and reason for WebSocket close frames.

    Attributes:
        code: Close code.
        reason: Close reason.

    r   coderE   reasonrD   c                 C  sz   d| j   krdk rn nd}nd| j   krdk r n nd}nt| j d}| j  d| d}| jr;| d	| j }|S )
zU
        Return a human-readable representation of a close code and reason.

          i  
registered  zprivate useunknownz ()rK   )r   r9   r   r   )ra   explanationresultr&   r&   r'   rg     s   zClose.__str__r<   r;   c                 C  sj   t |dkr$td|dd \}|dd  }| ||}|  |S t |dkr0| tjdS td)z
        Parse the payload of a close frame.

        Args:
            data: Payload of the close frame.

        Raises:
            ProtocolError: If data is ill-formed.
            UnicodeDecodeError: If the reason isn't valid UTF-8.

        r   rw   Nr   rF   zclose frame too short)	rW   ry   rz   rY   r   r(   r-   r   r|   )r   r<   r   r   closer&   r&   r'   r\     s   

zClose.parsec                 C  s    |    td| j| j  S )z:
        Serialize the payload of a close frame.

        rw   )r   ry   r   r   r   r   r   r&   r&   r'   r     s   zClose.serializer   c                 C  s4   | j tv sd| j   krdk sn tddS dS )z
        Check that the close code has a valid value for a close frame.

        Raises:
            ProtocolError: If the close code is invalid.

        r   r   zinvalid status codeN)r   EXTERNAL_CLOSE_CODESr   r|   r   r&   r&   r'   r     s   "
zClose.checkNr   )r<   r;   rD   r   )rD   r;   r   )
r   r   r   r   r   rg   r   r\   r   r   r&   r&   r&   r'   r     s   
 	

r   )r<   r	   rD   r   )r<   r	   rD   r;   )B
__future__r   dataclassesenumr   r   r   ry   typingr   r   r   rF   r   r   r	   speedupsr
   ImportErrorutils__all__IntEnumr   r    r   r!   r   r"   r   r#   r   r$   r   r%   r   r   r   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r9   r   r   OK_CLOSE_CODESr;   	bytearray
memoryviewr   	dataclassr   r   r   r   r&   r&   r&   r'   <module>   s    


 
h
