
    g-                         d Z ddlZddlmZ ddlZddlmZ ddlmZ ddl	m
Z
  G d dej                        Zd	 Z G d
 d      Z G d de      Zd Zy)a  
h2/settings
~~~~~~~~~~~

This module contains a HTTP/2 settings object. This object provides a simple
API for manipulating HTTP/2 settings, keeping track of both the current active
state of the settings and the unacknowledged future values of the settings.
    N)MutableMapping)SettingsFrame)
ErrorCodes)InvalidSettingsValueErrorc                       e Zd ZdZej
                  Zej                  Zej                  Zej                  Zej                  Z	ej                  Z
ej                  Zy)SettingCodeszF
    All known HTTP/2 setting codes.

    .. versionadded:: 2.6.0
    N)__name__
__module____qualname____doc__r   HEADER_TABLE_SIZEENABLE_PUSHMAX_CONCURRENT_STREAMSINITIAL_WINDOW_SIZEMAX_FRAME_SIZEMAX_HEADER_LIST_SIZEENABLE_CONNECT_PROTOCOL     @/var/www/openai/venv/lib/python3.12/site-packages/h2/settings.pyr   r      sg     &77  ++K +AA (;; #11N )== ,CCr   r   c                 <    	 t        |       S # t        $ r | cY S w xY w)z
    Given an integer setting code, returns either one of :class:`SettingCodes
    <h2.settings.SettingCodes>` or, if not present in the known set of codes,
    returns the integer directly.
    )r   
ValueError)codes    r   _setting_code_from_intr   :   s&    D!! s   
 c                       e Zd Zd Zd Zy)ChangedSettingc                 .    || _         || _        || _        y Nsettingoriginal_value	new_value)selfr    r!   r"   s       r   __init__zChangedSetting.__init__H   s    
  - #r   c                 V    d| j                   d| j                  d| j                  dS )NzChangedSetting(setting=z, original_value=z, new_value=)r   r#   s    r   __repr__zChangedSetting.__repr__U   s%    
 LLNN
 	
r   N)r	   r
   r   r$   r(   r   r   r   r   r   F   s    #
r   r   c                      e Zd ZdZddZd Zed        Zej                  d        Zed        Z	e	j                  d        Z	ed	        Z
e
j                  d
        Z
ed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d        Zd Zd Zd Zd Zd Zd Zd Zy)Settingsam  
    An object that encapsulates HTTP/2 settings state.

    HTTP/2 Settings are a complex beast. Each party, remote and local, has its
    own settings and a view of the other party's settings. When a settings
    frame is emitted by a peer it cannot assume that the new settings values
    are in place until the remote peer acknowledges the setting. In principle,
    multiple settings changes can be "in flight" at the same time, all with
    different values.

    This object encapsulates this mess. It provides a dict-like interface to
    settings, which return the *current* values of the settings in question.
    Additionally, it keeps track of the stack of proposed values: each time an
    acknowledgement is sent/received, it updates the current values with the
    stack of proposed values. On top of all that, it validates the values to
    make sure they're allowed, and raises :class:`InvalidSettingsValueError
    <h2.exceptions.InvalidSettingsValueError>` if they are not.

    Finally, this object understands what the default values of the HTTP/2
    settings are, and sets those defaults appropriately.

    .. versionchanged:: 2.2.0
       Added the ``initial_values`` parameter.

    .. versionchanged:: 2.5.0
       Added the ``max_header_list_size`` property.

    :param client: (optional) Whether these settings should be defaulted for a
        client implementation or a server implementation. Defaults to ``True``.
    :type client: ``bool``
    :param initial_values: (optional) Any initial values the user would like
        set, rather than RFC 7540's defaults.
    :type initial_vales: ``MutableMapping``
    Nc                 H   t         j                  t        j                  dg      t         j                  t        j                  t        |      g      t         j                  t        j                  dg      t         j                  t        j                  dg      t         j                  t        j                  dg      i| _	        |\|j                         D ]H  \  }}t        ||      }|rt        d||fz  |      t        j                  |g      | j                  |<   J y y )Ni   i   @  r   Setting %d has invalid value %d
error_code)r   r   collectionsdequer   intr   r   r   	_settingsitems_validate_settingr   )r#   clientinitial_valueskeyvalueinvalids         r   r$   zSettings.__init__   s     **K,=,=tf,E$$k&7&7V&F,,k.?.?.H''):):E7)C00+2C2CQC2H
 %,224
U+C739S%LH#*  '2&7&7&@s# 5 &r   c                     i }| j                   j                         D ]9  \  }}t        |      dkD  s|j                         }|d   }t	        |||      ||<   ; |S )z
        The settings have been acknowledged, either by the user (remote
        settings) or by the remote peer (local settings).

        :returns: A dict of {setting: ChangedSetting} that were applied.
           r   )r3   r4   lenpopleftr   )r#   changed_settingskvold_settingnew_settings         r   acknowledgezSettings.acknowledge   se      NN((*DAq1vziikd&4{K' #	 +  r   c                 (    | t         j                     S )z
        The current value of the :data:`HEADER_TABLE_SIZE
        <h2.settings.SettingCodes.HEADER_TABLE_SIZE>` setting.
        r   r   r'   s    r   header_table_sizezSettings.header_table_size   s     L2233r   c                 *    || t         j                  <   y r   rF   r#   r9   s     r   rG   zSettings.header_table_size   s    /4\++,r   c                 (    | t         j                     S )zv
        The current value of the :data:`ENABLE_PUSH
        <h2.settings.SettingCodes.ENABLE_PUSH>` setting.
        r   r   r'   s    r   enable_pushzSettings.enable_push   s     L,,--r   c                 *    || t         j                  <   y r   rK   rI   s     r   rL   zSettings.enable_push   s    ).\%%&r   c                 (    | t         j                     S )z
        The current value of the :data:`INITIAL_WINDOW_SIZE
        <h2.settings.SettingCodes.INITIAL_WINDOW_SIZE>` setting.
        r   r   r'   s    r   initial_window_sizezSettings.initial_window_size   s     L4455r   c                 *    || t         j                  <   y r   rO   rI   s     r   rP   zSettings.initial_window_size   s    16\--.r   c                 (    | t         j                     S )z|
        The current value of the :data:`MAX_FRAME_SIZE
        <h2.settings.SettingCodes.MAX_FRAME_SIZE>` setting.
        r   r   r'   s    r   max_frame_sizezSettings.max_frame_size   s     L//00r   c                 *    || t         j                  <   y r   rS   rI   s     r   rT   zSettings.max_frame_size   s    ,1\(()r   c                 B    | j                  t        j                  d      S )z
        The current value of the :data:`MAX_CONCURRENT_STREAMS
        <h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS>` setting.
        l       )getr   r   r'   s    r   max_concurrent_streamszSettings.max_concurrent_streams   s     xx;;WEEr   c                 *    || t         j                  <   y r   )r   r   rI   s     r   rX   zSettings.max_concurrent_streams   s    49\001r   c                 B    | j                  t        j                  d      S )z
        The current value of the :data:`MAX_HEADER_LIST_SIZE
        <h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE>` setting. If not set,
        returns ``None``, which means unlimited.

        .. versionadded:: 2.5.0
        N)rW   r   r   r'   s    r   max_header_list_sizezSettings.max_header_list_size   s     xx994@@r   c                 *    || t         j                  <   y r   )r   r   rI   s     r   r[   zSettings.max_header_list_size   s    27\../r   c                 (    | t         j                     S )z
        The current value of the :data:`ENABLE_CONNECT_PROTOCOL
        <h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL>` setting.
        r   r   r'   s    r   enable_connect_protocolz Settings.enable_connect_protocol   s     L8899r   c                 *    || t         j                  <   y r   r^   rI   s     r   r_   z Settings.enable_connect_protocol  s    5:\112r   c                 :    | j                   |   d   }|t        |S )Nr   )r3   KeyError)r#   r8   vals      r   __getitem__zSettings.__getitem__	  s&    nnS!!$ ;N
r   c                     t        ||      }|rt        d||fz  |      	 | j                  |   }|j                  |       y # t        $ r( t	        j
                  d g      }|| j                  |<   Y Bw xY w)Nr-   r.   )r5   r   r3   rb   r0   r1   append)r#   r8   r9   r:   r4   s        r   __setitem__zSettings.__setitem__  s    #C/+1S%L@" 
	(NN3'E
 	U	  	(%%tf-E"'DNN3	(s   A .A43A4c                     | j                   |= y r   )r3   )r#   r8   s     r   __delitem__zSettings.__delitem__#  s    NN3r   c                 6    | j                   j                         S r   )r3   __iter__r'   s    r   rk   zSettings.__iter__&  s    ~~&&((r   c                 ,    t        | j                        S r   )r=   r3   r'   s    r   __len__zSettings.__len__)  s    4>>""r   c                 `    t        |t              r| j                  |j                  k(  S t        S r   )
isinstancer*   r3   NotImplementedr#   others     r   __eq__zSettings.__eq__,  s%    eX&>>U__44!!r   c                 :    t        |t              r| |k(   S t        S r   )ro   r*   rp   rq   s     r   __ne__zSettings.__ne__2  s    eX&u}$$!!r   )TN)r	   r
   r   r   r$   rD   propertyrG   setterrL   rP   rT   rX   r[   r_   rd   rg   ri   rk   rm   rs   ru   r   r   r   r*   r*   `   s|   !DA0 , 4 4 5 5 . . / / 6 6 7  7 1 1 2 2 F F "": #: A A   8 !8 : : ##; $;  )#""r   r*   c                    | t         j                  k(  r|dvrt        j                  S y| t         j                  k(  r-d|cxk  rdk  s!t        j
                  S  t        j
                  S y| t         j                  k(  r-d|cxk  rdk  s!t        j                  S  t        j                  S y| t         j                  k(  r|dk  rt        j                  S y| t         j                  k(  r|dvrt        j                  S y)z
    Confirms that a specific setting has a well-formed value. If the setting is
    invalid, returns an error code. Otherwise, returns 0 (NO_ERROR).
    )r   r<   r   ir,   i )	r   r   r   PROTOCOL_ERRORr   FLOW_CONTROL_ERRORr   r   r   )r    r9   s     r   r5   r5   9  s    
 ,***,,,  
L44	4E'Z'000 (000  
L//	/)),,, *,,,  
L55	519,,,
 	 
L88	8,,,r   )r   r0   collections.abcr   enumhyperframe.framer   	h2.errorsr   h2.exceptionsr   IntEnumr   r   r   r*   r5   r   r   r   <module>r      sT     *  *   3#D4<< #DL	
 
4V"~ V"rr   