
    Ng@                     d    d Z ddlmZmZ ddlmZmZmZmZ ddl	m
Z
mZmZ dZ G d d          Zd	S )
zy
h2/frame_buffer
~~~~~~~~~~~~~~~

A data structure that provides a way to iterate over a byte buffer in terms of
frames.
    )InvalidFrameErrorInvalidDataError)FrameHeadersFrameContinuationFramePushPromiseFrame   )ProtocolErrorFrameTooLargeErrorFrameDataMissingError@   c                   8    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d	S )FrameBufferz
    This is a data structure that expects to act as a buffer for HTTP/2 data
    that allows iteraton in terms of H2 frames.
    Fc                 x    d| _         d| _        |rdnd| _        t          | j                  | _        g | _        d S )N    r   s   PRI * HTTP/2.0

SM

)datamax_frame_size	_preamblelen_preamble_len_headers_buffer)selfservers     K/var/www/html/ai-engine/env/lib/python3.11/site-packages/h2/frame_buffer.py__init__zFrameBuffer.__init__"   sC    	@FO<<C 00!r   c                 ,   | j         r|t          |          }t          | j         |          }| j        d|         |d|         k    rt	          d          ||d         }| xj         |z  c_         | j        |d         | _        | xj        |z  c_        dS )zs
        Add more data to the frame buffer.

        :param data: A bytestring containing the byte buffer.
        NzInvalid HTTP/2 preamble.)r   r   minr   r
   r   )r   r   data_lenof_which_preambles       r   add_datazFrameBuffer.add_data)   s      		@4yyH #D$6 A A~0001T:L;L:L5MMM#$>???)**+D"33!^,=,>,>?DN		T				r   c                 N    || j         k    rt          d|| j         fz            dS )zB
        Confirm that the frame is an appropriate length.
        z*Received overlong frame: length %d, max %dN)r   r   )r   lengths     r   _validate_frame_lengthz"FrameBuffer._validate_frame_length<   s?     D'''$<,-.   ('r   c                 x   | j         r| j         d         j        }|duot          |t                    o
|j        |k    }|st	          d          | j                             |           t          | j                   t          k    rt	          d          d|j        v rX| j         d         }|j        	                    d           d
                    d | j         D                       |_        g | _         nDd}nAt          |t          t          f          r%d|j        vr| j                             |           d}|S )z
        Updates the internal header buffer. Returns a frame that should replace
        the current one. May throw exceptions if this frame is invalid.
        r   Nz"Invalid frame during header block.z&Too many continuation frames received.END_HEADERSr   c              3   $   K   | ]}|j         V  d S N)r   ).0xs     r   	<genexpr>z4FrameBuffer._update_header_buffer.<locals>.<genexpr>f   s$      !G!GQ!&!G!G!G!G!G!Gr   )r   	stream_id
isinstancer   r
   appendr   CONTINUATION_BACKLOGflagsaddjoinr   r   r   )r   fr+   valid_frames       r   _update_header_bufferz!FrameBuffer._update_header_bufferF   sU     	,Q/9I )1/00)y( 
  J#$HIII  ''***4'((+???#$LMMM ''(+M***!G!G$2F!G!G!GGG')$$\+;<== 	QW,,  ''***Ar   c                     | S r'    )r   s    r   __iter__zFrameBuffer.__iter__t   s    r   c                    t          | j                  dk     rt                      	 t          j        | j        d d                   \  }}n8# t
          t          f$ r$}t          dt          |          z            d }~ww xY wt          | j                  |dz   k     rt                      | 	                    |           	 |
                    t          | j        dd|z                                 n5# t
          $ r t          d          t          $ r t          d          w xY w| j        d|z   d          | _        |                     |          }||n|                                 S )N	   z&Received frame with invalid header: %sz&Received frame with non-compliant datazFrame data missing or invalid)r   r   StopIterationr   parse_frame_headerr   r   r
   strr#   
parse_body
memoryviewr   r4   __next__)r   r2   r"   es       r   r?   zFrameBuffer.__next__w   s    ty>>A//!	02A2??IAvv "34 	 	 	83q66A  	 ty>>FQJ&&//! 	##F+++	ILLDIa&j$9::;;;; 	J 	J 	J HIII  	I 	I 	I'(GHHH	I
 Iahii(	 &&q)) Mqqt}}6s#   $A BA==B2C7 72D)N)F)
__name__
__module____qualname____doc__r   r    r#   r4   r7   r?   r6   r   r   r   r      s~         " " " "  &  + + +\  )7 )7 )7 )7 )7r   r   N)rD   hyperframe.exceptionsr   r   hyperframe.framer   r   r   r   
exceptionsr
   r   r   r.   r   r6   r   r   <module>rH      s     F E E E E E E E                      C7 C7 C7 C7 C7 C7 C7 C7 C7 C7r   