
    g|                     J   U d Z ddlZddlZddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZ dZdZd	Zd
ZdZ ej0                  d      Z ej0                  d      Z ej0                  d      Z ej0                  d      Z ej0                  d      Z ej0                  d      Z ej0                  d      Z G d d      Z  G d d      Z! G d d      Z" G d de!e       Z# G d de"e       Z$ G d de       Z% G d d e       Z& G d! d"e!e       Z' G d# d$e       Z( G d% d&e       Z) G d' d(e       Z* G d) d*e!e"e       Z+ G d+ d,e       Z, G d- d.e       Z- G d/ d0e       Z.d1ee/   d2e0fd3Z1e#e+e$e%e&e'e(e)e*e,e-gZ2eee       e3d4<   e2D  ci c]  } | jh                  |  c} Z5yc c} w )5z
hyperframe/frame
~~~~~~~~~~~~~~~~

Defines framing logic for HTTP/2. Provides both classes to represent framed
data and logic for aiding the connection when it comes to reading from the
socket.
    N   )UnknownFrameErrorInvalidPaddingErrorInvalidFrameErrorInvalidDataError)FlagFlags)OptionalTupleListIterableAnyDictTypei @  i z
has-streamz	no-streameitherz>HBBBLz>LLz>HLz>LBz>Lz>Hz>Bc            
          e Zd ZU dZg Zee   ed<   dZe	e
   ed<   dZe	e   ed<   dde
dee   ddfd	Zdefd
ZdefdZededed e
f   fd       Zeddededed e
f   fd       Zde
defdZdefdZdefdZdeddfdZy)Framez/
    The base class for all HTTP/2 frames.
    defined_flagsNtypestream_association	stream_idflagsreturnc                    || _         t        | j                        | _        d| _        |D ]  }| j                  j                  |        | j                   s@| j                  t        k(  r-t        dj                  t        |       j                              | j                   rL| j                  t        k(  r8t        dj                  t        |       j                  | j                               y y )Nr   z!Stream ID must be non-zero for {}z/Stream ID must be zero for {} with stream_id={})r   r	   r   r   body_lenaddr   _STREAM_ASSOC_HAS_STREAMr   formatr   __name___STREAM_ASSOC_NO_STREAM)selfr   r   flags       E/var/www/openai/venv/lib/python3.12/site-packages/hyperframe/frame.py__init__zFrame.__init__8   s     # 4--.
 DJJNN4   ""&>>"3::J'' 
 NN""&=="AHHJ''NN  >     c                     dj                  t        |       j                  | j                  t	        | j
                        | j                               S )Nz{}(stream_id={}, flags={}): {})r   r   r   r   reprr   
_body_reprr!   s    r#   __repr__zFrame.__repr__V   s>    ,
&JNNOO	
	
r%   c                 4    t        | j                               S N)_raw_data_reprserialize_bodyr)   s    r#   r(   zFrame._body_repr`   s     d11344r%   datac                     t         j                  | dd       \  }}|j                  | dd|z           t        |       ||fS )a>  
        Takes a bytestring and tries to parse a single frame and print it.

        This function is only provided for debugging purposes.

        :param data: A memoryview object containing the raw data of at least
                     one complete frame (header and body).

        .. versionadded:: 6.0.0
        N	   )r   parse_frame_header
parse_bodyprint)r/   framelengths      r#   explainzFrame.explaine   sH     00bq:vaF
+,ef}r%   headerstrictc                 N   	 t         j                  |       }|d   dz  |d   z   }|d   }|d   }|d   dz  }	 t        |   |      }|j                  |       ||fS # t        j                  $ r t	        d      w xY w# t        $ r |rt        ||      t        ||	      }Y ]w xY w)
a  
        Takes a 9-byte frame header and returns a tuple of the appropriate
        Frame object and the length that needs to be read from the socket.

        This populates the flags field, and determines how long the body is.

        :param header: A memoryview object containing the 9-byte frame header
                       data of a frame. Must not contain more or less.

        :param strict: Whether to raise an exception when encountering a frame
            not defined by spec and implemented by hyperframe.

        :raises hyperframe.exceptions.UnknownFrameError: If a frame of unknown
            type is received.

        .. versionchanged:: 5.0.0
            Added :param:`strict` to accommodate :class:`ExtensionFrame`
        zInvalid frame headerr      r            )r   r   )
_STRUCT_HBBBLunpackstructerrorr   FRAMESKeyErrorr   ExtensionFrameparse_flags)r8   r9   fieldsr6   r   r   r   r5   s           r#   r2   zFrame.parse_frame_headerv   s    (	<"))&1F
 )q.F1I-ayq	1I
*		C4L+E 	% v# || 	<#$:;;	<  	C'f55"	BE	Cs   A A= A:=$B$#B$	flag_bytec                     | j                   D ]&  \  }}||z  s| j                  j                  |       ( | j                  S r,   )r   r   r   )r!   rI   r"   flag_bits       r#   rG   zFrame.parse_flags   s:    "00ND(8#

t$ 1 zzr%   c                 :   | j                         }t        |      | _        d}| j                  D ]  \  }}|| j                  v s||z  } t
        j                  | j                  dz	  dz  | j                  dz  | j                  || j                  dz        }||z   S )zk
        Convert a frame into a bytestring, representing the serialized form of
        the frame.
        r   r;        r?   )	r.   lenr   r   r   r@   packr   r   )r!   bodyr   r"   rK   r8   s         r#   	serializezFrame.serialize   s    
 ""$D	 "00ND(tzz!! 1 ##]]a6)MMD IINNZ'
 }r%   c                     t               r,   NotImplementedErrorr)   s    r#   r.   zFrame.serialize_body   s    !##r%   c                     t               )a  
        Given the body of a frame, parses it into frame data. This populates
        the non-header parts of the frame: that is, it does not populate the
        stream ID or flags.

        :param data: A memoryview object containing the body data of the frame.
                     Must not contain *more* data than the length returned by
                     :meth:`parse_frame_header
                     <hyperframe.frame.Frame.parse_frame_header>`.
        rT   r!   r/   s     r#   r3   zFrame.parse_body   s     "##r%   ) )F)r   
__module____qualname____doc__r   r   r   __annotations__r   r
   intr   strr   r$   r*   r(   staticmethod
memoryviewr   r7   boolr2   r	   rG   bytesrR   r.   r3   rX   r%   r#   r   r   *   s    !#M4:" D(3- )-,# hsm T <
# 
5C 5
 j U7C<%8    &: &t &gWZlH[ & &PS U 5 4$ $$z $d $r%   r   c            	       h     e Zd ZdZddedededdf fdZdefdZd	e	defd
Z
edefd       Z xZS )Paddingz
    Mixin for frames that contain padding. Defines extra fields that can be
    used and set by frames that can be padded.
    r   
pad_lengthkwargsr   Nc                 4    t        |   |fi | || _        y r,   )superr$   re   )r!   r   re   rf   	__class__s       r#   r$   zPadding.__init__       -f- %r%   c                 ^    d| j                   v rt        j                  | j                        S y)NPADDEDr%   )r   	_STRUCT_BrP   re   r)   s    r#   serialize_padding_datazPadding.serialize_padding_data   s$    tzz!>>$//22r%   r/   c                     d| j                   v r#	 t        j                  d|d d       d   | _        yy# t        j                  $ r t        d      w xY w)Nrl   z!Br   r   zInvalid Padding data)r   rB   rA   re   rC   r   rW   s     r#   parse_padding_datazPadding.parse_padding_data   s\    tzz!@"(--d2Ah"?"B  << @'(>??@s	   !3 Ac                 N    dd l }|j                  dt               | j                  S )Nr   z:total_padding contains the same information as pad_length.)warningswarnDeprecationWarningre   )r!   rr   s     r#   total_paddingzPadding.total_padding   s#    H	
 r%   r   )r   rY   rZ   r[   r]   r   r$   rb   rn   r`   rp   propertyru   __classcell__ri   s   @r#   rd   rd      sc    %# %3 %c %d % 
z c  s  r%   rd   c                   `     e Zd ZdZ	 	 	 ddedededededdf fd	Zdefd
Z	de
defdZ xZS )Priorityz
    Mixin for frames that contain priority data. Defines extra fields that can
    be used and set by frames that contain priority data.
    r   
depends_onstream_weight	exclusiverf   r   Nc                 P    t        |   |fi | || _        || _        || _        y r,   )rh   r$   r|   r}   r~   )r!   r   r|   r}   r~   rf   ri   s         r#   r$   zPriority.__init__   s3     	-f- % + #r%   c                 x    t         j                  | j                  | j                  rdndz   | j                        S )Nl        r   )
_STRUCT_LBrP   r|   r~   r}   r)   s    r#   serialize_priority_dataz Priority.serialize_priority_data  s0    OOT^^zC
 	
r%   r/   c                     	 t         j                  |d d       \  | _        | _        | j                  dz	  rdnd| _        | xj                  dz  c_        y# t        j
                  $ r t        d      w xY w)N   zInvalid Priority data   TFr?   )r   rA   r|   r}   rB   rC   r   r~   rW   s     r#   parse_priority_datazPriority.parse_priority_data  so    	=2<2C2CD!H2M/DOT/ "&B!6E:% || 	=#$;<<	=s   %A A4)r   r   F)r   rY   rZ   r[   r]   ra   r   r$   rb   r   r`   r   rx   ry   s   @r#   r{   r{      so     $'&)#(	## # !$# !	#
 #
 $(#"
 

 s r%   r{   c            	            e Zd ZdZ edd       edd      gZdZeZdde	de
d	ed
df fdZd
e
fdZded
dfdZed
e	fd       Z xZS )	DataFramez
    DATA frames convey arbitrary, variable-length sequences of octets
    associated with a stream. One or more DATA frames are used, for instance,
    to carry HTTP request or response payloads.
    
END_STREAMr   rl   r;   r   r   r/   rf   r   Nc                 4    t        |   |fi | || _        y r,   rh   r$   r/   r!   r   r/   rf   ri   s       r#   r$   zDataFrame.__init__.      -f- 	r%   c                     | j                         }d| j                  z  }t        | j                  t              r| j                  j                         | _        dj                  || j                  |g      S N    r%   )rn   re   
isinstancer/   r`   tobytesjoin)r!   padding_datapaddings      r#   r.   zDataFrame.serialize_body4  sZ    224$//)dii,		))+DIxxtyy':;;r%   c                    | j                  |      }||t        |      | j                  z
   j                         | _        t        |      | _        | j                  r%| j                  | j
                  k\  rt        d      y y )NPadding is too long.)rp   rO   re   r   r/   r   r   r!   r/   padding_data_lengths      r#   r3   zDataFrame.parse_body;  sr    "55d;$SYt%>?GGI 		 D	??t$--?%&<==  @?r%   c                 p    d}d| j                   v r| j                  dz   }t        | j                        |z   S )zo
        The length of the frame that needs to be accounted for when considering
        flow control.
        r   rl   r   )r   re   rO   r/   )r!   padding_lens     r#   flow_controlled_lengthz DataFrame.flow_controlled_lengthE  s8     tzz! //A-K499~++r%   r%   )r   rY   rZ   r[   r   r   r   r   r   r]   rb   r   r$   r.   r`   r3   rw   r   rx   ry   s   @r#   r   r     s     	\4 XtM D1# U C D < <>z >d > 
, 
, 
,r%   r   c                   V    e Zd ZU dZg Zee   ed<   dZe	Z
defdZdefdZdeddfd	Zy)
PriorityFramez
    The PRIORITY frame specifies the sender-advised priority of a stream. It
    can be sent at any time for an existing stream. This enables
    reprioritisation of existing streams.
    r   r<   r   c                 d    dj                  | j                  | j                  | j                        S )Nz-exclusive={}, depends_on={}, stream_weight={})r   r~   r|   r}   r)   s    r#   r(   zPriorityFrame._body_repra  s,    >EENNOO
 	
r%   c                 "    | j                         S r,   )r   r)   s    r#   r.   zPriorityFrame.serialize_bodyh  s    ++--r%   r/   Nc                 ~    t        |      dkD  rt        dt        |      z        | j                  |       d| _        y )Nr   z1PRIORITY must have 5 byte body: actual length %s.)rO   r   r   r   rW   s     r#   r3   zPriorityFrame.parse_bodyk  s@    t9q=#CD	 
 	  &r%   )r   rY   rZ   r[   r   r   r   r\   r   r   r   r^   r(   rb   r.   r`   r3   rX   r%   r#   r   r   S  sO     !#M4:" D1
C 
. .z d r%   r   c            	       |     e Zd ZU dZg Zee   ed<   dZe	Z
ddedededdf fd	Zdefd
ZdefdZdeddfdZ xZS )RstStreamFramea  
    The RST_STREAM frame allows for abnormal termination of a stream. When sent
    by the initiator of a stream, it indicates that they wish to cancel the
    stream or that an error condition has occurred. When sent by the receiver
    of a stream, it indicates that either the receiver is rejecting the stream,
    requesting that the stream be cancelled or that an error condition has
    occurred.
    r   r=   r   
error_coderf   r   Nc                 4    t        |   |fi | || _        y r,   )rh   r$   r   )r!   r   r   rf   ri   s       r#   r$   zRstStreamFrame.__init__  rj   r%   c                 8    dj                  | j                        S )Nzerror_code={})r   r   r)   s    r#   r(   zRstStreamFrame._body_repr  s    %%OO
 	
r%   c                 @    t         j                  | j                        S r,   )	_STRUCT_LrP   r   r)   s    r#   r.   zRstStreamFrame.serialize_body  s    ~~doo..r%   r/   c                     t        |      dk7  rt        dt        |      z        	 t        j                  |      d   | _        d| _        y # t
        j                  $ r t        d      w xY w)Nr>   z3RST_STREAM must have 4 byte body: actual length %s.r   zInvalid RST_STREAM body)rO   r   r   rA   r   rB   rC   r   rW   s     r#   r3   zRstStreamFrame.parse_body  so    t9>#ED	 
	?'..t4Q7DO  || 	?#$=>>	?s   A A+rv   )r   rY   rZ   r[   r   r   r   r\   r   r   r   r]   r   r$   r^   r(   rb   r.   r`   r3   rx   ry   s   @r#   r   r   v  sq     !#M4:" D1%# %3 %c %d %
C 

/ /z d r%   r   c            	            e Zd ZdZ edd      gZdZeZdZ	dZ
dZdZdZdZd	Zddedeeeef      dedd
f fdZdefdZdefdZdedd
fdZ xZS )SettingsFramea  
    The SETTINGS frame conveys configuration parameters that affect how
    endpoints communicate. The parameters are either constraints on peer
    behavior or preferences.

    Settings are not negotiated. Settings describe characteristics of the
    sending peer, which are used by the receiving peer. Different values for
    the same setting can be advertised by each peer. For example, a client
    might set a high initial flow control window, whereas a server might set a
    lower value to conserve resources.
    ACKr   r>   r<   r=   r      r;   Nr   settingsrf   r   c                 ~    t        |   |fi | |rd|j                  dd      v rt        d      |xs i | _        y )Nr   r   rX   z*Settings must be empty if ACK flag is set.)rh   r$   getr   r   )r!   r   r   rf   ri   s       r#   r$   zSettingsFrame.__init__  sG    -f-GR!88"< 
 !Br%   c                 8    dj                  | j                        S )Nzsettings={})r   r   r)   s    r#   r(   zSettingsFrame._body_repr  s    ##MM
 	
r%   c           
          dj                  | j                  j                         D cg c]  \  }}t        j	                  |dz  |        c}}      S c c}}w )Nr%   rN   )r   r   items
_STRUCT_HLrP   )r!   settingvalues      r#   r.   zSettingsFrame.serialize_body  sX    xx/3}}/B/B/DF/D^We $4?/DF G 	G Fs   #A
r/   c                 \   d| j                   v r%t        |      dkD  rt        dt        |      z        d}t        dt        |      d      D ]5  }	 t        j                  |||dz          \  }}|| j                  |<   |dz  }7 || _
        y # t        j                  $ r t        d      w xY w)Nr   r   z6SETTINGS ack frame must not have payload: got %s bytesr   zInvalid SETTINGS body)r   rO   r   ranger   rA   rB   rC   r   r   r   )r!   r/   r   inamer   s         r#   r3   zSettingsFrame.parse_body  s    DJJ3t9q="HD	 
 q#d)Q'AA(//Qqs<e #(DMM$MH ( ! << A'(?@@As   BB+)r   N)r   rY   rZ   r[   r   r   r   r    r   HEADER_TABLE_SIZEENABLE_PUSHMAX_CONCURRENT_STREAMSINITIAL_WINDOW_SIZEMAX_FRAME_SIZEMAX_HEADER_LIST_SIZEENABLE_CONNECT_PROTOCOLr]   r
   r   r   r$   r^   r(   rb   r.   r`   r3   rx   ry   s   @r#   r   r     s    
 %&'M D0
 K!N"	'# 	'Xd38n5M 	'`c 	'hl 	'
C 

G G!z !d !r%   r   c                        e Zd ZdZ edd       edd      gZdZeZdde	de	d	e
d
eddf
 fdZdefdZde
fdZd	eddfdZ xZS )PushPromiseFramez~
    The PUSH_PROMISE frame is used to notify the peer endpoint in advance of
    streams the sender intends to initiate.
    END_HEADERSr>   rl   r;   r   r   promised_stream_idr/   rf   r   Nc                 B    t        |   |fi | || _        || _        y r,   )rh   r$   r   r/   )r!   r   r   r/   rf   ri   s        r#   r$   zPushPromiseFrame.__init__  s(    -f- #5 	r%   c                 `    dj                  | j                  t        | j                              S )Nzpromised_stream_id={}, data={})r   r   r-   r/   r)   s    r#   r(   zPushPromiseFrame._body_repr  s*    /66##499%
 	
r%   c                     | j                         }d| j                  z  }t        j                  | j                        }dj                  ||| j                  |g      S r   )rn   re   r   rP   r   r   r/   )r!   r   r   r/   s       r#   r.   zPushPromiseFrame.serialize_body  sN    224$//)~~d556xxtTYY@AAr%   c                    | j                  |      }	 t        j                  |||dz          d   | _        ||dz   t        |      | j                  z
   j                         | _
        t        |      | _        | j                  dk(  s| j                  dz  dk7  rt        d| j                  z        | j                  r%| j                  | j                  k\  rt        d      y y # t        j
                  $ r t        d      w xY w)Nr>   r   zInvalid PUSH_PROMISE bodyr<   z+Invalid PUSH_PROMISE promised stream id: %sr   )rp   r   rA   r   rB   rC   r   rO   re   r   r/   r   r   r   r   s      r#   r3   zPushPromiseFrame.parse_body  s   "55d;	A&/&6&6()<q)@A''D# $q(T4??)BCKKM 		 D	""a'4+B+BQ+F!+K"=''( 
 ??t$--?%&<==  @? || 	A#$?@@	As   #C" "Dr   r%   r   rY   rZ   r[   r   r   r   r   r   r]   rb   r   r$   r^   r(   r.   r`   r3   rx   ry   s   @r#   r   r     s     	]D!XtM D1# 3 % ad im 
C 
B B>z >d >r%   r   c            	       z     e Zd ZdZ edd      gZdZeZdde	de
dedd	f fd
ZdefdZde
fdZdedd	fdZ xZS )	PingFramez
    The PING frame is a mechanism for measuring a minimal round-trip time from
    the sender, as well as determining whether an idle connection is still
    functional. PING frames can be sent from any endpoint.
    r   r   r   r   opaque_datarf   r   Nc                 4    t        |   |fi | || _        y r,   )rh   r$   r   )r!   r   r   rf   ri   s       r#   r$   zPingFrame.__init__>  s    -f- 'r%   c                 8    dj                  | j                        S )Nzopaque_data={!r})r   r   r)   s    r#   r(   zPingFrame._body_reprD  s    !((
 	
r%   c                     t        | j                        dkD  rt        d| j                  z        | j                  }|ddt        | j                        z
  z  z  }|S )Nr;   z9PING frame may not have more than 8 bytes of data, got %rr   )rO   r   r   rW   s     r#   r.   zPingFrame.serialize_bodyI  sd    t 1$#K  ! 
 1s4#3#34455r%   r/   c                     t        |      dk7  rt        dt        |      z        |j                         | _        d| _        y )Nr;   z*PING frame must have 8 byte length: got %s)rO   r   r   r   r   rW   s     r#   r3   zPingFrame.parse_bodyT  s=    t9>#<s4yH   <<>r%   r   )r   rY   rZ   r[   r   r   r   r    r   r]   rb   r   r$   r^   r(   r.   r`   r3   rx   ry   s   @r#   r   r   0  ss     %&'M D0'# ' 's 'W[ '
C 

	 	z d r%   r   c                        e Zd ZU dZg Zee   ed<   dZe	Z
	 	 	 	 ddededededed	d
f fdZd	efdZd	efdZded	d
fdZ xZS )GoAwayFramez
    The GOAWAY frame informs the remote peer to stop creating streams on this
    connection. It can be sent from the client or the server. Once sent, the
    sender will ignore frames sent on new streams for the remainder of the
    connection.
    r      r   last_stream_idr   additional_datarf   r   Nc                 P    t        |   |fi | || _        || _        || _        y r,   )rh   r$   r   r   r   )r!   r   r   r   r   rf   ri   s         r#   r$   zGoAwayFrame.__init__m  s4     	-f- - %  /r%   c                 d    dj                  | j                  | j                  | j                        S )Nz6last_stream_id={}, error_code={}, additional_data={!r})r   r   r   r   r)   s    r#   r(   zGoAwayFrame._body_repr~  s.    GNNOO  
 	
r%   c                 ~    t         j                  | j                  dz  | j                        }|| j                  z  }|S Nr?   )
_STRUCT_LLrP   r   r   r   rW   s     r#   r.   zGoAwayFrame.serialize_body  s<    *,OO
 	$$$r%   r/   c                    	 t         j                  |d d       \  | _        | _        t        |      | _        t        |      dkD  r|dd  j                         | _
        y y # t        j
                  $ r t        d      w xY w)Nr;   zInvalid GOAWAY body.)r   rA   r   r   rB   rC   r   rO   r   r   r   rW   s     r#   r3   zGoAwayFrame.parse_body  s    	<3=3D3DRa40D D	t9q=#'8#3#3#5D   || 	<#$:;;	<s   %A A>)r   r   r   r%   )r   rY   rZ   r[   r   r   r   r\   r   r    r   r]   rb   r   r$   r^   r(   r.   r`   r3   rx   ry   s   @r#   r   r   ^  s     !#M4:" D0 #$'(#$*-	//!$/ !/ #(	/
 /
 $(/"
C 
 6z 6d 6r%   r   c            	       |     e Zd ZU dZg Zee   ed<   dZe	Z
ddedededdf fd	Zdefd
ZdefdZdeddfdZ xZS )WindowUpdateFramea  
    The WINDOW_UPDATE frame is used to implement flow control.

    Flow control operates at two levels: on each individual stream and on the
    entire connection.

    Both types of flow control are hop by hop; that is, only between the two
    endpoints. Intermediaries do not forward WINDOW_UPDATE frames between
    dependent connections. However, throttling of data transfer by any receiver
    can indirectly cause the propagation of flow control information toward the
    original sender.
    r   r;   r   window_incrementrf   r   Nc                 4    t        |   |fi | || _        y r,   )rh   r$   r   )r!   r   r   rf   ri   s       r#   r$   zWindowUpdateFrame.__init__  s    -f- !1r%   c                 8    dj                  | j                        S )Nzwindow_increment={})r   r   r)   s    r#   r(   zWindowUpdateFrame._body_repr  s    $++!!
 	
r%   c                 F    t         j                  | j                  dz        S r   )r   rP   r   r)   s    r#   r.   z WindowUpdateFrame.serialize_body  s    ~~d33j@AAr%   r/   c                 4   t        |      dkD  rt        dt        |      z        	 t        j                  |      d   | _        d| j                  cxk  rdk  st        d       t        d      d| _        y # t
        j                  $ r t        d      w xY w)Nr>   z3WINDOW_UPDATE frame must have 4 byte length: got %sr   zInvalid WINDOW_UPDATE bodyr   r?   z3WINDOW_UPDATE increment must be between 1 to 2^31-1)	rO   r   r   rA   r   rB   rC   r   r   rW   s     r#   r3   zWindowUpdateFrame.parse_body  s    t9q=#ED	 
	B$-$4$4T$:1$=D! D))4W4"E  5"E   || 	B#$@AA	Bs   A8 8Brv   )r   rY   rZ   r[   r   r   r   r\   r   _STREAM_ASSOC_EITHERr   r]   r   r$   r^   r(   rb   r.   r`   r3   rx   ry   s   @r#   r   r     st     !#M4:" D-1# 1 1C 1TX 1
C 

B Bz d r%   r   c            	            e Zd ZdZ edd       edd       edd       edd	      gZdZeZdd
e	de
deddf fdZdefdZde
fdZdeddfdZ xZS )HeadersFramea"  
    The HEADERS frame carries name-value pairs. It is used to open a stream.
    HEADERS frames can be sent on a stream in the "open" or "half closed
    (remote)" states.

    The HeadersFrame class is actually basically a data frame in this
    implementation, because of the requirement to control the sizes of frames.
    A header block fragment that doesn't fit in an entire HEADERS frame needs
    to be followed with CONTINUATION frames. From the perspective of the frame
    building code the header block is an opaque data segment.
    r   r   r   r>   rl   r;   PRIORITY    r   r/   rf   r   Nc                 4    t        |   |fi | || _        y r,   r   r   s       r#   r$   zHeadersFrame.__init__  r   r%   c                     dj                  | j                  | j                  | j                  t	        | j
                              S )Nz6exclusive={}, depends_on={}, stream_weight={}, data={})r   r~   r|   r}   r-   r/   r)   s    r#   r(   zHeadersFrame._body_repr  s8    GNNNNOO499%	
 	
r%   c                     | j                         }d| j                  z  }d| j                  v r| j                         }nd}dj	                  ||| j
                  |g      S )Nr   r   r%   )rn   re   r   r   r   r/   )r!   r   r   priority_datas       r#   r.   zHeadersFrame.serialize_body  sY    224$//)# 88:MMxx}diiIJJr%   c                 R   | j                  |      }||d  }d| j                  v r| j                  |      }nd}t        |      | _        ||t        |      | j
                  z
   j                         | _        | j
                  r%| j
                  | j                  k\  rt        d      y y )Nr   r   r   )	rp   r   r   rO   r   re   r   r/   r   )r!   r/   r   priority_data_lengths       r#   r3   zHeadersFrame.parse_body  s    "55d;'()##'#;#;D#A #$ D	%c$i&?@HHJ 		 ??t$--?%&<==  @?r%   r   r   ry   s   @r#   r   r     s    
 	\4 ]D!XtZ	M D1# U C D 
C 
	K 	K>z >d >r%   r   c            	       z     e Zd ZdZ edd      gZdZeZdde	de
dedd	f fd
ZdefdZde
fdZdedd	fdZ xZS )ContinuationFramea  
    The CONTINUATION frame is used to continue a sequence of header block
    fragments. Any number of CONTINUATION frames can be sent on an existing
    stream, as long as the preceding frame on the same stream is one of
    HEADERS, PUSH_PROMISE or CONTINUATION without the END_HEADERS flag set.

    Much like the HEADERS frame, hyper treats this as an opaque data frame with
    different flags and a different type.
    r   r>   r1   r   r/   rf   r   Nc                 4    t        |   |fi | || _        y r,   r   r   s       r#   r$   zContinuationFrame.__init__)  r   r%   c                 J    dj                  t        | j                              S )Nzdata={})r   r-   r/   r)   s    r#   r(   zContinuationFrame._body_repr/  s!    499%
 	
r%   c                     | j                   S r,   )r/   r)   s    r#   r.   z ContinuationFrame.serialize_body4  s    yyr%   c                 N    |j                         | _        t        |      | _        y r,   )r   r/   rO   r   rW   s     r#   r3   zContinuationFrame.parse_body7      LLN	D	r%   r   r   ry   s   @r#   r   r     sr     -./M D1# U C D 
C 

 "z "d "r%   r   c                   j     e Zd ZdZdZeZddededede	ddf
 fd	Z
defd
ZdefdZdeddfdZ xZS )AltSvcFramea  
    The ALTSVC frame is used to advertise alternate services that the current
    host, or a different one, can understand. This frame is standardised as
    part of RFC 7838.

    This frame does no work to validate that the ALTSVC field parameter is
    acceptable per the rules of RFC 7838.

    .. note:: If the ``stream_id`` of this frame is nonzero, the origin field
              must have zero length. Conversely, if the ``stream_id`` of this
              frame is zero, the origin field must have nonzero length. Put
              another way, a valid ALTSVC frame has ``stream_id != 0`` XOR
              ``len(origin) != 0``.
    
   r   originfieldrf   r   Nc                     t        |   |fi | t        |t              st	        d      t        |t              st	        d      || _        || _        y )Nz!AltSvc origin must be bytestring.z"AltSvc field must be a bytestring.)rh   r$   r   rb   r   r   r   )r!   r   r   r   rf   ri   s        r#   r$   zAltSvcFrame.__init__O  sO    -f-&%("#FGG%'"#GHH
r%   c                 N    dj                  | j                  | j                        S )Nzorigin={!r}, field={!r})r   r   r   r)   s    r#   r(   zAltSvcFrame._body_reprY  s#    (//KKJJ
 	
r%   c                     t         j                  t        | j                              }dj	                  || j                  | j
                  g      S )Nr%   )	_STRUCT_HrP   rO   r   r   r   )r!   
origin_lens     r#   r.   zAltSvcFrame.serialize_body_  s7    ^^C$45
xxT[[$**=>>r%   r/   c                 ^   	 t         j                  |dd       d   }|dd|z    j                         | _        t	        | j                        |k7  rt        d      |d|z   d  j                         | _        t	        |      | _
        y # t        j                  t        f$ r t        d      w xY w)Nr   r<   zInvalid ALTSVC frame body.)r   rA   r   r   rO   r   r   rB   rC   
ValueErrorr   )r!   r/   r  s      r#   r3   zAltSvcFrame.parse_bodyc  s    		B"))$q)4Q7Jq:.668DK4;;:-'(DEEa
lm,446DJ D	 j) 	B#$@AA	Bs   A4B %B,)r%   r%   )r   rY   rZ   r[   r   r   r   r]   rb   r   r$   r^   r(   r.   r`   r3   rx   ry   s   @r#   r   r   <  sj     D-# u 5 Z] bf 
C 
? ?"z "d "r%   r   c                   z     e Zd ZdZeZddedededededdf fd	Z	de
fd
ZdeddfdZdeddfdZdefdZ xZS )rF   aM  
    ExtensionFrame is used to wrap frames which are not natively interpretable
    by hyperframe.

    Although certain byte prefixes are ordained by specification to have
    certain contextual meanings, frames with other prefixes are not prohibited,
    and may be used to communicate arbitrary meaning between HTTP/2 peers.

    Thus, hyperframe, rather than raising an exception when such a frame is
    encountered, wraps it in a generic frame to be properly acted upon by
    upstream consumers which might have additional context on how to use it.

    .. versionadded:: 5.0.0
    r   r   rI   rQ   rf   r   Nc                 P    t        |   |fi | || _        || _        || _        y r,   )rh   r$   r   rI   rQ   )r!   r   r   rI   rQ   rf   ri   s         r#   r$   zExtensionFrame.__init__  s*    -f-	"	r%   c                 v    dj                  | j                  | j                  t        | j                              S )Nztype={}, flag_byte={}, body={})r   r   rI   r-   rQ   r)   s    r#   r(   zExtensionFrame._body_repr  s/    /66IINN499%
 	
r%   c                     || _         y)zW
        For extension frames, we parse the flags by just storing a flag byte.
        N)rI   )r!   rI   s     r#   rG   zExtensionFrame.parse_flags  s     #r%   r/   c                 N    |j                         | _        t        |      | _        y r,   )r   rQ   rO   r   rW   s     r#   r3   zExtensionFrame.parse_body  r   r%   c                     | j                   }t        j                  | j                  dz	  dz  | j                  dz  | j                  || j
                  dz        }|| j                  z   S )a  
        A broad override of the serialize method that ensures that the data
        comes back out exactly as it came in. This should not be used in most
        user code: it exists only as a helper method if frames need to be
        reconstituted.
        r;   rM   rN   r?   )rI   r@   rP   r   r   r   rQ   )r!   r   r8   s      r#   rR   zExtensionFrame.serialize  sa     ##]]a6)MMD IINNZ'
 		!!r%   r   )r   rY   rZ   r[   r   r   r]   rb   r   r$   r^   r(   rG   r`   r3   rR   rx   ry   s   @r#   rF   rF   r  s     .S S S e eh mq 
C 
#S #T #"z "d ""5 "r%   rF   r/   r   c                     | syt        j                  |       j                  d      }t        |      dkD  r|d d dz   }d|z   dz   S )NNoneascii   z...z<hex:>)binasciihexlifydecoderO   )r/   rs     r#   r-   r-     sL    %%g.A
1v{crFUNQ;r%   _FRAME_CLASSES)6r[   rB   r  
exceptionsr   r   r   r   r   r   r	   typingr
   r   r   r   r   r   r   FRAME_MAX_LENFRAME_MAX_ALLOWED_LENr   r    r   Structr@   r   r   r   r   r   rm   r   rd   r{   r   r   r   r   r   r   r   r   r   r   r   rF   rb   r^   r-   r  r\   r   rD   )clss   0r#   <module>r     s       C C C
  &  ( %   h'V]]5!
V]]5!
V]]5!
FMM$	FMM$	FMM$	d$ d$N" "J$ $N3, 3,l He  F+U +\J!E J!Z<>w <>~+ +\;6% ;6|4 4nA>7He A>H"" ""J3"% 3"l<"U <"~% S  %T%[!  $2	2>C#((C->	2	2s   F 