o
    KK&h                     @  s   d dl m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 dd	lmZ zdd
lmZ W n eyO   dd
lmZ Y nw G dd de	Zd ddZd!ddZeZddlmZ d"ddZd#ddZdS )$    )annotationsN)	AwaitableSequence)AnyCallable
NamedTuple   )
extensionsframes)PayloadTooBigProtocolError)	BytesLike)Data)
apply_maskc                   @  s   e Zd ZU ded< ded< ded< dZded< dZded	< dZded
< ed%ddZd&ddZ	d'ddZ
edddd(ddZdd d)d#d$ZdS )*Frameboolfinzframes.OpcodeopcodebytesdataFrsv1rsv2rsv3returnframes.Framec                 C  s    t | j| j| j| j| j| jS N)r
   r   r   r   r   r   r   r   self r   L/var/www/html/venv/lib/python3.10/site-packages/websockets/legacy/framing.py	new_frame   s   zFrame.new_framestrc                 C  s
   t | jS r   )r!   r    r   r   r   r   __str__&      
zFrame.__str__Nonec                 C  s
   | j  S r   )r    checkr   r   r   r   r%   )   r#   zFrame.checkN)max_sizer	   reader!Callable[[int], Awaitable[bytes]]maskr&   
int | Noner	   %Sequence[extensions.Extension] | Nonec             
     s  |dI 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rYtd|d@ }|dkrp|dI dH }t d|\}n|dkr|dI dH }t d|\}|dur||krt|||r|dI dH }||I dH }|rt||}t||||	|
|}|du rg }t	|D ]	}|j
||d}q|  | |j|j|j|j|j|jS )a@  
        Read a WebSocket frame.

        Args:
            reader: Coroutine that reads exactly the requested number of
                bytes, unless the end of file is reached.
            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:
            PayloadTooBig: If the frame exceeds ``max_size``.
            ProtocolError: If the frame contains incorrect values.

        r   Nz!BB   TF@             zinvalid opcodezincorrect masking   ~   z!H   z!Q   )r&   )structunpackr
   Opcode
ValueErrorr   r   r   r   reverseddecoder%   r   r   r   r   r   r   )clsr'   r)   r&   r	   r   head1head2r   r   r   r   r   exclength	mask_bitsr    	extensionr   r   r   read,   sV   


z
Frame.read)r	   writeCallable[[bytes], Any]c                C  s   || j j||d dS )a  
        Write a WebSocket frame.

        Args:
            frame: Frame to write.
            write: Function that writes bytes.
            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.

        )r)   r	   N)r    	serialize)r   rC   r)   r	   r   r   r   rC   {   s   zFrame.write)r   r   )r   r!   )r   r$   )
r'   r(   r)   r   r&   r*   r	   r+   r   r   )rC   rD   r)   r   r	   r+   r   r$   )__name__
__module____qualname____annotations__r   r   r   propertyr    r"   r%   classmethodrB   rC   r   r   r   r   r      s"   
 


Sr   r   r   r   tuple[int, bytes]c                 C  s8   t | trtjj|  fS t | trtjj| 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)	
isinstancer!   r
   r7   TEXTencoder   BINARY	TypeErrorr   r   r   r   prepare_data   s
   

rT   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.

    rM   )rN   r!   rP   r   r   rR   rS   r   r   r   prepare_ctrl   s
   

rU   )Closetuple[int, str]c                 C  s   t | }|j|jfS )z
    Parse the payload from a close frame.

    Returns:
        Close code and reason.

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

    )rV   parsecodereason)r   closer   r   r   parse_close   s   
r\   rY   intrZ   r!   c                 C  s   t | | S )z3
    Serialize the payload for a close frame.

    )rV   rE   )rY   rZ   r   r   r   serialize_close   s   r^   )r   r   r   rL   )r   r   r   r   )r   r   r   rW   )rY   r]   rZ   r!   r   r   )
__future__r   r5   collections.abcr   r   typingr   r   r    r	   r
   
exceptionsr   r   r   r   speedupsr   ImportErrorutilsr   rT   rU   encode_datarV   r\   r^   r   r   r   r   <module>   s*     


