
    g@                     \    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y	)
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                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
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.
    c                 x    d| _         d| _        |rdnd| _        t        | j                        | _        g | _        y )N    r   s   PRI * HTTP/2.0

SM

)datamax_frame_size	_preamblelen_preamble_len_headers_buffer)selfservers     D/var/www/openai/venv/lib/python3.12/site-packages/h2/frame_buffer.py__init__zFrameBuffer.__init__"   s6    	@F<C 0!r   c                 $   | j                   rot        |      }t        | j                   |      }| j                  d| |d| k7  rt	        d      ||d }| xj                   |z  c_         | j                  |d | _        | xj
                  |z  c_        y)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     4yH #D$6$6 A~~001T:L;L5MM#$>??)*+D"33!^^,=,>?DN		T	r   c                 V    || j                   kD  rt        d|| j                   fz        y)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                    | j                   r| j                   d   j                  }|duxr! t        |t              xr |j                  |k(  }|st	        d      | j                   j                  |       t        | j                         t        kD  rt	        d      d|j                  v rZ| j                   d   }|j                  j                  d       dj                  d | j                   D              |_        g | _         |S d}|S t        |t        t        f      r+d|j                  vr| j                   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   4   K   | ]  }|j                     y wN)r   ).0xs     r   	<genexpr>z4FrameBuffer._update_header_buffer.<locals>.<genexpr>f   s     !G2FQ!&&2Fs   )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   s<    ,,Q/99I )1/0)y( 
 #$HII   ''*4''(+??#$LMM '((+M*!G$2F2F!GG')$    \+;<=QWW,   ''*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       \  }}t        | j                        |dz   k  r
t               | j                  |       	 |j                  t        | j                  dd|z                 | j                  d|z   d  | _        | j                  |      }||S | j                         S # t
        t        f$ r}t        dt        |      z        d }~ww xY w# t
        $ r t        d      t        $ r t        d      w xY w)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   s9    tyy>A/!	002A?IAv tyy>FQJ&/! 	##F+	ILLDIIa&$9:; IIahi(	 &&q) Mq6t}}6C !"34 	83q6A 	    	J HII  	I'(GHH	Is#   %C&  *D &D5DD(D<N)F)
__name__
__module____qualname____doc__r   r    r#   r4   r7   r?   r6   r   r   r   r      s&    "&+\)7r   r   N)rD   hyperframe.exceptionsr   r   hyperframe.framer   r   r   r   
exceptionsr
   r   r   r.   r   r6   r   r   <module>rH      s8    F    C7 C7r   