o
    _c`                     @  s   d dl mZ d dlZd dlZd dlmZmZmZmZ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mZ g d	Zd
Zdd eddD ZG dd deZd/ddZd0d d!ZG d"d# d#eZd1d'd(ZG d)d* d*eZd2d-d.ZdS )3    )annotationsN)AnyDictListOptionalSequenceTupleUnion   )
exceptionsframes)ExtensionNameExtensionParameter   )ClientExtensionFactory	ExtensionServerExtensionFactory)PerMessageDeflateClientPerMessageDeflateFactory enable_client_permessage_deflateServerPerMessageDeflateFactory enable_server_permessage_deflates     c                 C  s   g | ]}t |qS  )str).0bitsr   r   S/usr/local/lib/python3.10/dist-packages/websockets/extensions/permessage_deflate.py
<listcomp>   s    r         c                   @  sJ   e Zd ZdZedZ	ddddZdddZdddddZd ddZ	dS )!r   z)
    Per-Message Deflate extension.

    permessage-deflateNremote_no_context_takeoverboollocal_no_context_takeoverremote_max_window_bitsintlocal_max_window_bitscompress_settingsOptional[Dict[Any, Any]]returnNonec                 C  s   |du ri }|dv sJ |dv sJ d|  krdks J  J d|  kr+dks.J  J d|vs4J || _ || _|| _|| _|| _| j sOtj| j d| _| js`tjdd| j i| j| _	d| _
dS )	z?
        Configure the Per-Message Deflate extension.

        N)FTr      wbitsr,   Fr   )r!   r#   r$   r&   r'   zlibdecompressobjdecodercompressobjencoderdecode_cont_data)selfr!   r#   r$   r&   r'   r   r   r   __init__!   s*   
zPerMessageDeflate.__init__r   c              	   C  s&   d| j  d| j d| j d| j d	S )Nz-PerMessageDeflate(remote_no_context_takeover=z, local_no_context_takeover=z, remote_max_window_bits=z, local_max_window_bits=))r!   r#   r$   r&   r4   r   r   r   __repr__J   s   zPerMessageDeflate.__repr__)max_sizeframeframes.Framer9   Optional[int]c             
   C  s  |j tjv r|S |j tju r| js|S |jrd| _n|js|S tj|dd}|js,d| _| j	r8t
j| j d| _|j}|jrB|t7 }|du rHdn|}z	| j||}W n t
jyf } ztd|d}~ww | jjrttd| d	|jr|| j	r|| `tj||d
S )z,
        Decode an incoming frame.

        Frsv1Tr-   Nr   zdecompression failedzover size limit (? > z bytes)data)opcoder   CTRL_OPCODESOP_CONTr3   finr>   dataclassesreplacer!   r.   r/   r$   r0   r@   _EMPTY_UNCOMPRESSED_BLOCK
decompresserrorr   ProtocolErrorunconsumed_tailZPayloadTooBig)r4   r:   r9   r@   
max_lengthexcr   r   r   decodeS   s<   zPerMessageDeflate.decodec                 C  s   |j tjv r|S |j tjur&tj|dd}| jr&tjdd| j	 i| j
| _| j|j| jtj }|jrB|trB|dd }|jrJ| jrJ| `tj||dS )z,
        Encode an outgoing frame.

        Tr=   r,   Nr?   r   )rA   r   rB   rC   rE   rF   r#   r.   r1   r&   r'   r2   compressr@   flushZ_SYNC_FLUSHrD   endswithrG   )r4   r:   r@   r   r   r   encode   s    zPerMessageDeflate.encodeN)r!   r"   r#   r"   r$   r%   r&   r%   r'   r(   r)   r*   )r)   r   )r:   r;   r9   r<   r)   r;   )r:   r;   r)   r;   )
__name__
__module____qualname____doc__r   namer5   r8   rN   rT   r   r   r   r   r      s    
):r   server_no_context_takeoverr"   client_no_context_takeoverserver_max_window_bitsr<   client_max_window_bitsOptional[Union[int, bool]]r)   List[ExtensionParameter]c                 C  sf   g }| r	| d |r| d |r| dt|f |du r&| d |S |r1| dt|f |S )zS
    Build a list of ``(name, value)`` pairs for some compression parameters.

    )r[   N)r\   Nr]   T)r^   Nr^   )appendr   )r[   r\   r]   r^   paramsr   r   r   _build_parameters   s   



rc   rb   Sequence[ExtensionParameter]	is_server<Tuple[bool, bool, Optional[int], Optional[Union[int, bool]]]c                C  s  d}d}d}d}| D ]y\}}|dkr&|rt ||du r d}q
t |||dkr>|r1t ||du r8d}q
t |||dkrZ|durKt ||tv rTt|}q
t |||dkr|durgt ||rp|du rpd}q
|tv ryt|}q
t ||t |||||fS )z
    Extract compression parameters from a list of ``(name, value)`` pairs.

    If ``is_server`` is :obj:`True`, ``client_max_window_bits`` may be
    provided without a value. This is only allowed in handshake requests.

    FNr[   Tr\   r]   r^   )r   ZDuplicateParameterZInvalidParameterValue_MAX_WINDOW_BITS_VALUESr%   ZInvalidParameterName)rb   re   r[   r\   r]   r^   rZ   valuer   r   r   _extract_parameters   sJ   







ri   c                   @  sB   e Zd ZdZedZ					ddddZdddZd ddZdS )!r   a  
    Client-side extension factory for the Per-Message Deflate extension.

    Parameters behave as described in `section 7.1 of RFC 7692`_.

    .. _section 7.1 of RFC 7692: https://www.rfc-editor.org/rfc/rfc7692.html#section-7.1

    Set them to :obj:`True` to include them in the negotiation offer without a
    value or to an integer value to include them with this value.

    Args:
        server_no_context_takeover: prevent server from using context takeover.
        client_no_context_takeover: prevent client from using context takeover.
        server_max_window_bits: maximum size of the server's LZ77 sliding window
            in bits, between 8 and 15.
        client_max_window_bits: maximum size of the client's LZ77 sliding window
            in bits, between 8 and 15, or :obj:`True` to indicate support without
            setting a limit.
        compress_settings: additional keyword arguments for :func:`zlib.compressobj`,
            excluding ``wbits``.

    r    FNTr[   r"   r\   r]   r<   r^   r_   r'   Optional[Dict[str, Any]]r)   r*   c                 C  s   |du sd|  krdkst d t d|du s0|du s0d|  kr+dks0t d t d|dur<d|v r<t d|| _|| _|| _|| _|| _dS )	G
        Configure the Per-Message Deflate extension factory.

        Nr   r+   /server_max_window_bits must be between 8 and 15T/client_max_window_bits must be between 8 and 15r,   zLcompress_settings must not include wbits, set client_max_window_bits instead)
ValueErrorr[   r\   r]   r^   r'   )r4   r[   r\   r]   r^   r'   r   r   r   r5     s0   
z'ClientPerMessageDeflateFactory.__init__r`   c                 C  s   t | j| j| j| jS )z,
        Build request parameters.

        )rc   r[   r\   r]   r^   r7   r   r   r   get_request_params>  s   z1ClientPerMessageDeflateFactory.get_request_paramsrb   rd   accepted_extensionsSequence[Extension]r   c                   s   t  fdd|D rtd j t|dd\}}}} jr(|s(td jr/|s/d} jdu r5n|du r>td	| jkrHtd
 jdu rW|durVtdn jdu r]n|du re j}n
| jkrotdt	|||pud|pxd j
S )zV
        Process response parameters.

        Return an extension instance.

        c                 3      | ]	}|j  j kV  qd S rU   rZ   r   otherr7   r   r   	<genexpr>U      zIClientPerMessageDeflateFactory.process_response_params.<locals>.<genexpr>zreceived duplicate Fre   z#expected server_no_context_takeoverTNzexpected server_max_window_bitsz"unsupported server_max_window_bitsz!unexpected client_max_window_bitsz"unsupported client_max_window_bitsr+   )anyr   NegotiationErrorrZ   ri   r[   r\   r]   r^   r   r'   r4   rb   rp   r[   r\   r]   r^   r   r7   r   process_response_paramsJ  sJ   











z6ClientPerMessageDeflateFactory.process_response_params)FFNTN)r[   r"   r\   r"   r]   r<   r^   r_   r'   rj   r)   r*   )r)   r`   )rb   rd   rp   rq   r)   r   )	rV   rW   rX   rY   r   rZ   r5   ro   r|   r   r   r   r   r     s    
 r   
extensions*Optional[Sequence[ClientExtensionFactory]] Sequence[ClientExtensionFactory]c                 C  s:   | du rg } t dd | D st| tddidg } | S )z
    Enable Per-Message Deflate with default settings in client extensions.

    If the extension is already present, perhaps with non-default settings,
    the configuration isn't changed.

    Nc                 s      | ]	}|j tj kV  qd S rU   )rZ   r   )r   Zextension_factoryr   r   r   rv     
    

z3enable_client_permessage_deflate.<locals>.<genexpr>memLevel   )r'   )ry   listr   r}   r   r   r   r     s   
r   c                   @  s:   e Zd ZdZedZ						ddddZdddZdS )r   a  
    Server-side extension factory for the Per-Message Deflate extension.

    Parameters behave as described in `section 7.1 of RFC 7692`_.

    .. _section 7.1 of RFC 7692: https://www.rfc-editor.org/rfc/rfc7692.html#section-7.1

    Set them to :obj:`True` to include them in the negotiation offer without a
    value or to an integer value to include them with this value.

    Args:
        server_no_context_takeover: prevent server from using context takeover.
        client_no_context_takeover: prevent client from using context takeover.
        server_max_window_bits: maximum size of the server's LZ77 sliding window
            in bits, between 8 and 15.
        client_max_window_bits: maximum size of the client's LZ77 sliding window
            in bits, between 8 and 15.
        compress_settings: additional keyword arguments for :func:`zlib.compressobj`,
            excluding ``wbits``.
        require_client_max_window_bits: do not enable compression at all if
            client doesn't advertise support for ``client_max_window_bits``;
            the default behavior is to enable compression without enforcing
            ``client_max_window_bits``.

    r    FNr[   r"   r\   r]   r<   r^   r'   rj   require_client_max_window_bitsr)   r*   c                 C  s   |du sd|  krdkst d t d|du s,d|  kr'dks,t d t d|dur8d|v r8t d|du rB|rBt d|| _|| _|| _|| _|| _|| _dS )	rk   Nr   r+   rl   rm   r,   zLcompress_settings must not include wbits, set server_max_window_bits insteadzVrequire_client_max_window_bits is enabled, but client_max_window_bits isn't configured)rn   r[   r\   r]   r^   r'   r   )r4   r[   r\   r]   r^   r'   r   r   r   r   r5     s,   
z'ServerPerMessageDeflateFactory.__init__rb   rd   rp   rq   2Tuple[List[ExtensionParameter], PerMessageDeflate]c                   s   t  fdd|D rtd j t|dd\}}}} jr%|s%d} jr,|s,d} jdu r2n|du r: j}n| jkrB j} jdu rO|du rN j}n|du r\ j	r[tdn|du rd j}n j|k rl j}t
||||t|||pxd|p{d jfS )	zi
        Process request parameters.

        Return response params and an extension instance.

        c                 3  rr   rU   rs   rt   r7   r   r   rv     rw   zHServerPerMessageDeflateFactory.process_request_params.<locals>.<genexpr>zskipped duplicate Trx   Nzrequired client_max_window_bitsr+   )ry   r   rz   rZ   ri   r[   r\   r]   r^   r   rc   r   r'   r{   r   r7   r   process_request_params  s\   





z5ServerPerMessageDeflateFactory.process_request_params)FFNNNF)r[   r"   r\   r"   r]   r<   r^   r<   r'   rj   r   r"   r)   r*   )rb   rd   rp   rq   r)   r   )rV   rW   rX   rY   r   rZ   r5   r   r   r   r   r   r     s    #r   *Optional[Sequence[ServerExtensionFactory]] Sequence[ServerExtensionFactory]c                 C  s>   | du rg } t dd | D st| tddddidg } | S )z
    Enable Per-Message Deflate with default settings in server extensions.

    If the extension is already present, perhaps with non-default settings,
    the configuration isn't changed.

    Nc                 s  r   rU   )rZ   r   )r   Zext_factoryr   r   r   rv     r   z3enable_server_permessage_deflate.<locals>.<genexpr>   r   r   )r]   r^   r'   )ry   r   r   r   r   r   r   r   ~  s   
r   )
r[   r"   r\   r"   r]   r<   r^   r_   r)   r`   )rb   rd   re   r"   r)   rf   )r}   r~   r)   r   )r}   r   r)   r   ) 
__future__r   rE   r.   typingr   r   r   r   r   r   r	    r   r   r   r   baser   r   r   __all__rG   rangerg   r   rc   ri   r   r   r   r   r   r   r   r   <module>   s(    $ 

> 
1 3