o
    nkjA                     @  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
 d dlmZ ddlmZmZ ddlmZ zddlmZ W n eyR   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&j0de&j1de&j2de&j3de&j4de&j5diZ6de7d< e&j'e&j(e&j)e&j*e&j-e&j.e&j/e&j0e&j1e&j2e&j3e&j4hZ8e&j'e&j(e&j+hZ9ej:G dd  d Z;ej:G d!d" d"Z<	#	#d-d.d*d+Z=dd,l>m?Z? dS )/    )annotationsN)	GeneratorSequence)Callable   )PayloadTooBigProtocolError)	BytesLike)
apply_mask)OpcodeOP_CONTOP_TEXT	OP_BINARYOP_CLOSEOP_PINGOP_PONGDATA_OPCODESCTRL_OPCODES	CloseCodeFrame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%   O/home/djax/ivt_ai_plugin/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 )r   z-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eded	ediZd)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   opcoder	   dataTboolfinFrsv1rsv2rsv3WEBSOCKETS_MAX_LOG_SIZE75returnstrc                 C  s   | j | j}|  \}}||krdn|rdnd}t| j dt| jdkr(dnd }| jr1dnd}dtd	|||g}| jj	 d
| d| dS )ax  
        Return a human-readable representation of a frame.

        This function is intended for logging and debugging. It doesn't aim to
        support round-tripping because payloads can be too long for displaying
        conveniently. Instead, it shows the beginning and the end. It's robust
        to incorrect data.

        It attempts to decode UTF-8 payloads whenever possible, even for binary
        frames and control frames, because those frequently contain UTF-8 data.
        It applies the same logic to continuation frames, because we don't know
        if they continue a text frame or a binary frame.

         textbinaryz byter   s	continuedz, N z [])
DEFAULT_IS_TEXTgetr8   
_data_reprlenr9   r;   joinfiltername)selfexpect_text	data_repris_text	data_typelength	non_finalmetadatar%   r%   r&   __str__   s   &zFrame.__str__tuple[str, bool | None]c              	   C  s  | j sd| j| jfS | jtu r(ztt| j dfW S  tt	fy'   Y nw t
| j d| j krkt| j dd| j d  }t| j d| j d d }t|| jtkdo]t|| jd	}|rjt|| jd
d}nt| j }t|| jtk| jd}|rt|jd
d}|s| j }t
|| jd kr| jd d d }d|dd|  d|| d g}ddd |D }t
|| jkr| jd d }|dd|  d || d  }||fS )z
        Return a human-readable representation of the payload.

        Also returns whether the payload is text.

        The representation is elided to fit ``MAX_LOG_SIZE``.

        This is a helper for the __str__ method.

        z''T   Nr      )must_start_clean)must_end_cleanreplace)errors)r^   r_   r       r   s     rH   c                 s  s    | ]}|d V  qdS )02xNr%   ).0byter%   r%   r&   	<genexpr>   s    z#Frame._data_repr.<locals>.<genexpr>z...)r9   rJ   rK   r8   r   rB   r   parser   UnicodeDecodeErrorrM   MAX_LOG_SIZEbytesis_utf8_fragmentr   r;   reprdecoderN   )rQ   
data_startdata_endrT   rS   r9   rE   cutr%   r%   r&   rL      sR   

&"zFrame._data_reprN)max_size
extensions
read_exact9Callable[[int], Generator[None, None, bytes | bytearray]]maskrq   
int | Nonerr   %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yI } ztd|d}~ww |d@ rPdnd|krXtd|d@ }|dkro|dE dH }t d|\}n|dkr|dE dH }t d|\}|dur||krt|||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.
            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!Qr[   )rq   )
structunpackr   
ValueErrorr   r   r
   reversedrm   check)clsrs   ru   rq   rr   r9   head1head2r;   r<   r=   r>   r8   excrV   
mask_bytesframe	extensionr%   r%   r&   rg      sH   


zFrame.parserr   rj   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.

        Nrz   r   r{   r|   r}   r   ry   i   z!BBHz!BBQr   r[   )r   encodeioBytesIOr;   r<   r=   r>   r8   rM   r9   writer   packsecretstoken_bytesr
   getvalue)
rQ   ru   rr   r   outputr   r   rV   r   r9   r%   r%   r&   	serializeC  s>   



zFrame.serializeNonec                 C  sP   | 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=   r>   r   r8   r   rM   r9   r;   rQ   r%   r%   r&   r     s   
zFrame.checkrA   rB   )rA   rZ   )
rs   rt   ru   r:   rq   rv   rr   rw   rA   rx   )ru   r:   rr   rw   rA   rj   rA   r   )r   r   r   r   __annotations__r;   r<   r=   r>   intosenvironrK   ri   r   r   r   rJ   rY   rL   classmethodrg   r   r   r%   r%   r%   r&   r   z   s&   
 

HM<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.

    zCloseCode | intcoderB   reasonrA   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 ()rH   )r   r7   rK   r   )rQ   explanationresultr%   r%   r&   rY     s   zClose.__str__r9   r	   c                 C  sz   t |tr	tdt|dkr-td|dd \}|dd  }| ||}|  |S t|dkr9| 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.

        z.only compressed outgoing frames use memoryviewr   r   Nr   rC   zclose frame too short)
isinstance
memoryviewAssertionErrorrM   r   r   rm   r   r   r+   r   )r   r9   r   r   closer%   r%   r&   rg     s   

zClose.parserj   c                 C  s    |    td| j| j  S )z:
        Serialize the payload of a close frame.

        r   )r   r   r   r   r   r   r   r%   r%   r&   r     s   zClose.serializer   c                 C  s8   | j tv sd| j   krdk std 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     s
   zClose.checkNr   )r9   r	   rA   r   )rA   rj   r   )
r   r   r   r   r   rY   r   rg   r   r   r%   r%   r%   r&   r     s   
 	

r   Fr9   rj   r^   r:   r_   rA   c           
      C  s(  dt | }}|s$tdt | }||k r$| | }|d@ dkr#|d7 }q	 |sy|d8 }tt | d |}||kry| | }|d@ dkrF|d8 }q3|d@ dkrOd}n|d@ dkrXd}n|d	@ dkrad}n|d
@ d	krjd}nd}|t | | krxt | }	 z
| ||  }	W n
 ty   Y dS w dt|	vS )z*Guess if data is a fragment of UTF-8 text.r   r\      rz   r   r[      r         Fz\x)rM   minmaxrm   rh   rl   )
r9   r^   r_   startend	max_startre   min_endseq_lenrD   r%   r%   r&   rk     sF   rk   r   )FF)r9   rj   r^   r:   r_   r:   rA   r:   )@
__future__r   dataclassesenumr   r   r   r   collections.abcr   r   typingr   
exceptionsr   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-   r.   r/   r0   r1   r2   r3   r4   r5   r7   r   r   OK_CLOSE_CODES	dataclassr   r   rk   rC   rr   r%   r%   r%   r&   <module>   s    

  OI