
    g                     &   d Z ddlmZmZ ddlm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mZmZmZmZmZmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. dd
l/m0Z0  G d de      Z1 G d de      Z2 G d de      Z3 e4d e5e1            D  cg c]  } d c} Z6de6e1jn                  <   de6e1jp                  <   de6e1jr                  <    G d d      Z:i e1jv                  e2jx                  fe:jz                  e1jn                  fe1jv                  e2j|                  fe:j~                  e1jn                  fe1jv                  e2j                  fe:j                  e1j                  fe1jv                  e2j                  fe:j                  e1j                  fe1jv                  e2j                  fe:j                  e1j                  fe1jv                  e2j                  fde1jv                  fe1jv                  e2j                  fe:jz                  e1jp                  fe1jv                  e2j                  fe:j~                  e1jr                  fe1j                  e2jx                  fe:j                  e1jr                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fde1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fde1j                  fe1j                  e2j|                  fe:j                  e1jp                  fi e1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fde1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1jn                  e2jx                  fe:j                  e1jn                  fe1jn                  e2j|                  fe:j                  e1jn                  fe1jn                  e2j                  fde1jn                  fe1jn                  e2j                  fe:j                  e1jn                  fe1jn                  e2j                  fde1jp                  fe1jn                  e2j                  fe:j                  e1jr                  fe1jn                  e2j                  fde1jn                  fe1jn                  e2j                  fe:j                  e1jn                  fe1jn                  e2j                  fe:j                  e1j                  fe1jn                  e2j                  fe:j                  e1j                  fe1jn                  e2j                  fe:j                  e1jn                  fi e1jn                  e2j                  fe:j                  e1jn                  fe1jn                  e2j                  fe:j                  e1jn                  fe1jn                  e2j                  fe:j                  e1jn                  fe1jn                  e2j                  fe:j                  e1jn                  fe1jn                  e2j                  fe:j                  e1jn                  fe1jr                  e2jx                  fe:j                  e1jr                  fe1jr                  e2j|                  fe:j                  e1j                  fe1jr                  e2j                  fde1jr                  fe1jr                  e2j                  fe:j                  e1j                  fe1jr                  e2j                  fe:j                  e1j                  fe1jr                  e2j                  fde1jr                  fe1jr                  e2j                  fe:j                  e1jr                  fe1jr                  e2j                  fe:j                  e1j                  fe1jr                  e2j                  fe:j                  e1j                  fe1jr                  e2j                  fe:j                  e1jr                  fe1jr                  e2j                  fe:j                  e1j                  fe1jr                  e2j                  fe:j                  e1jr                  fi e1jr                  e2j                  fe:j                  e1jr                  fe1jr                  e2j                  fe:j                  e1jr                  fe1jp                  e2j|                  fe:j                  e1jp                  fe1jp                  e2j                  fe:j                  e1jp                  fe1jp                  e2j                  fe:j                  e1j                  fe1jp                  e2j                  fde1jp                  fe1jp                  e2j                  fe:j                  e1jp                  fe1jp                  e2j                  fe:j                  e1j                  fe1jp                  e2j                  fe:j                  e1j                  fe1jp                  e2j                  fe:j                  e1jp                  fe1jp                  e2j                  fe:j                  e1jp                  fe1jp                  e2j                  fe:j                  e1jp                  fe1jp                  e2j                  fe:j                  e1jp                  fe1j                  e2j                  fde1j                  fe1j                  e2j                  fde1j                  fe1j                  e2j|                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fde1j                  fe1j                  e2j                  fde1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2jx                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fe1j                  e2j                  fe:j                  e1j                  fi	Zi G d d      Zjd Zkyc c} w )z<
h2/stream
~~~~~~~~~

An implementation of a HTTP/2 stream.
    )EnumIntEnum)HeaderTuple)HeadersFrameContinuationFrame	DataFrameWindowUpdateFrameRstStreamFramePushPromiseFrameAltSvcFrame   )
ErrorCodes_error_code_from_int)RequestReceivedResponseReceivedDataReceivedWindowUpdatedStreamEndedPushedStreamReceivedStreamResetTrailersReceivedInformationalResponseReceivedAlternativeServiceAvailable_ResponseSent_RequestSent_TrailersSent_PushedRequestSent)ProtocolErrorStreamClosedErrorInvalidBodyLengthErrorFlowControlError)	guard_increment_windowis_informational_responseauthority_from_headersvalidate_headersvalidate_outbound_headersnormalize_outbound_headersHeaderValidationFlagsextract_method_headernormalize_inbound_headers)WindowManagerc                   (    e Zd ZdZdZdZdZdZdZdZ	y)	StreamStater   r                  N)
__name__
__module____qualname__IDLERESERVED_REMOTERESERVED_LOCALOPENHALF_CLOSED_REMOTEHALF_CLOSED_LOCALCLOSED     >/var/www/openai/venv/lib/python3.12/site-packages/h2/stream.pyr-   r-   !   s'    DONDFr>   r-   c                   X    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZy)StreamInputsr   r   r.   r/   r0   r1   r2         	   
                           N)r3   r4   r5   SEND_HEADERSSEND_PUSH_PROMISESEND_RST_STREAM	SEND_DATASEND_WINDOW_UPDATESEND_END_STREAMRECV_HEADERSRECV_PUSH_PROMISERECV_RST_STREAM	RECV_DATARECV_WINDOW_UPDATERECV_END_STREAMRECV_CONTINUATIONSEND_INFORMATIONAL_HEADERSRECV_INFORMATIONAL_HEADERSSEND_ALTERNATIVE_SERVICERECV_ALTERNATIVE_SERVICEUPGRADE_CLIENTUPGRADE_SERVERr=   r>   r?   rA   rA   +   sj    LOIOLOIO!#!#!!NNr>   rA   c                       e Zd ZdZdZdZdZy)StreamClosedByr   r   r.   r/   N)r3   r4   r5   rS   rY   rP   rV   r=   r>   r?   rb   rb   A   s    OOOOr>   rb   FTc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)H2StreamStateMachinea  
    A single HTTP/2 stream state machine.

    This stream object implements basically the state machine described in
    RFC 7540 section 5.1.

    :param stream_id: The stream ID of this stream. This is stored primarily
        for logging purposes.
    c                     t         j                  | _        || _        d | _        d | _        d | _        d | _        d | _        d | _	        y )N)
r-   r6   state	stream_idclientheaders_senttrailers_sentheaders_receivedtrailers_receivedstream_closed_by)selfrg   s     r?   __init__zH2StreamStateMachine.__init__]   sK     %%
"  !! $!% !%r>   c                    t        |t              st        d      	 t        | j                  |f   \  }}| j                  }|| _        |
	  || |      S g S # t
        $ r t        j                  | _         t        $ r%}t        j                  | _        t        |      d}~ww xY w# t        $ r3 | j                  }t        j                  | _        t        d|d|      w xY w)z@
        Process a specific input in the state machine.
        z)Input must be an instance of StreamInputsNzInvalid input z
 in state )

isinstancerA   
ValueError_transitionsrf   r   r-   r<   AssertionErrorKeyError)rn   input_functarget_stateprevious_statee	old_states          r?   process_inputz"H2StreamStateMachine.process_inputm   s     &,/HII	!-tzz6.B!CD, "ZZN%DJ+n55 I % !,!3!3DJ% +!,!3!3DJ'**+  	

I$++DJ28)D 	s#   B% A 'B"= BB"%<C!c                 8    d| _         d| _        t               }|gS )z/
        Fires when a request is sent.
        T)rh   ri   r   rn   ry   events      r?   request_sentz!H2StreamStateMachine.request_sent   s!      wr>   c                     | j                   s9| j                  du s| j                  t        d      d| _         t               }|gS | j                  rJ d| _        t               }|gS )z{
        Fires when something that should be a response is sent. This 'response'
        may actually be trailers.
        TzClient cannot send responses.)ri   rh   r   r   rj   r   r~   s      r?   response_sentz"H2StreamStateMachine.response_sent   sm    
   {{d"dkk&9#$CDD $D!OE w	 ))))!%D!OEwr>   c                     | j                   rJ | j                  rJ d| _        d| _         t               }| j                  |_        |gS )z3
        Fires when a request is received.
        FT)rk   rl   rh   r   rg   r~   s      r?   request_receivedz%H2StreamStateMachine.request_received   sK     (((()))) $!..wr>   c                     | j                   s"| j                  du sJ d| _         t               }n| j                  rJ d| _        t	               }| j
                  |_        |gS )zo
        Fires when a response is received. Also disambiguates between responses
        and trailers.
        T)rk   rh   r   rl   r   rg   r~   s      r?   response_receivedz&H2StreamStateMachine.response_received   s`    
 $$;;$&&&$(D!$&E----%)D"$&E..wr>   c                 l    | j                   st        d      t               }| j                  |_        |gS )z.
        Fires when data is received.
        z"cannot receive data before headers)rk   r   r   rg   r~   s      r?   data_receivedz"H2StreamStateMachine.data_received   s2     $$ DEE..wr>   c                 >    t               }| j                  |_        |gS )z?
        Fires when a window update frame is received.
        )r   rg   r~   s      r?   window_updatedz#H2StreamStateMachine.window_updated   s     ..wr>   c                 >    t               }| j                  |_        |gS )z
        Fires when an END_STREAM flag is received in the OPEN state,
        transitioning this stream to a HALF_CLOSED_REMOTE state.
        )r   rg   r~   s      r?   stream_half_closedz'H2StreamStateMachine.stream_half_closed   s    
 ..wr>   c                 h    t         j                  | _        t               }| j                  |_        |gS )z7
        Fires when a stream is cleanly ended.
        )rb   rY   rm   r   rg   r~   s      r?   stream_endedz!H2StreamStateMachine.stream_ended   +     !/ > >..wr>   c                 h    t         j                  | _        t               }| j                  |_        |gS )z:
        Fired when a stream is forcefully reset.
        )rb   rV   rm   r   rg   r~   s      r?   stream_resetz!H2StreamStateMachine.stream_reset   r   r>   c                 >    | j                   J d| _         d| _        g S )z
        Fires on the newly pushed stream, when pushed by the local peer.

        No event here, but definitionally this peer must be a server.
        FT)rh   rk   rn   ry   s     r?   send_new_pushed_streamz+H2StreamStateMachine.send_new_pushed_stream   s(     {{""" $	r>   c                 >    | j                   J d| _         d| _        g S )z
        Fires on the newly pushed stream, when pushed by the remote peer.

        No event here, but definitionally this peer must be a client.
        T)rh   ri   r   s     r?   recv_new_pushed_streamz+H2StreamStateMachine.recv_new_pushed_stream   s(     {{""" 	r>   c                 N    | j                   du rt        d      t               }|gS )z
        Fires on the already-existing stream when a PUSH_PROMISE frame is sent.
        We may only send PUSH_PROMISE frames if we're a server.
        Tz&Cannot push streams from client peers.)rh   r   r   r~   s      r?   send_push_promisez&H2StreamStateMachine.send_push_promise  s+    
 ;;$ HII"$wr>   c                     | j                   s&| j                   d}t        |      d}t        |      t               }| j                  |_        |gS )z
        Fires on the already-existing stream when a PUSH_PROMISE frame is
        received. We may only receive PUSH_PROMISE frames if we're a client.

        Fires a PushedStreamReceived event.
        z"Idle streams cannot receive pushesz)Cannot receive pushed streams as a server)rh   r   r   rg   parent_stream_id)rn   ry   msgr   s       r?   recv_push_promisez&H2StreamStateMachine.recv_push_promise  sU     {{{{":  $$ B$$$&!%wr>   c                 .    t         j                  | _        y)zl
        Called when an attempt is made to send END_STREAM in the
        HALF_CLOSED_REMOTE state.
        N)rb   rS   rm   r   s     r?   send_end_streamz$H2StreamStateMachine.send_end_stream%      
 !/ > >r>   c                 .    t         j                  | _        y)zi
        Called when an attempt is made to send RST_STREAM in a non-closed
        stream state.
        N)rb   rP   rm   r   s     r?   send_reset_streamz&H2StreamStateMachine.send_reset_stream,  r   r>   c                     t         j                  | _        t        | j                        }t               }| j                  |_        t        j                  |_        d|_	        |g|_
        |)a  
        Called when we need to forcefully emit another RST_STREAM frame on
        behalf of the state machine.

        If this is the first time we've done this, we should also hang an event
        off the StreamClosedError so that the user can be informed. We know
        it's the first time we've done this if the stream is currently in a
        state other than CLOSED.
        F)rb   rP   rm   r   rg   r   r   STREAM_CLOSED
error_coderemote_reset_events)rn   ry   errorr   s       r?   reset_stream_on_errorz*H2StreamStateMachine.reset_stream_on_error3  sV     !/ > >!$..1..%33"r>   c                 ,    t        | j                        )aw  
        Called when an unexpected frame is received on an already-closed
        stream.

        An endpoint that receives an unexpected frame should treat it as
        a stream error or connection error with type STREAM_CLOSED, depending
        on the specific frame. The error handling is done at a higher level:
        this just raises the appropriate error.
        r   rg   r   s     r?   recv_on_closed_streamz*H2StreamStateMachine.recv_on_closed_streamH        //r>   c                 ,    t        | j                        )a[  
        Called when an attempt is made to send data on an already-closed
        stream.

        This essentially overrides the standard logic by throwing a
        more-specific error: StreamClosedError. This is a ProtocolError, so it
        matches the standard API of the state machine, but provides more detail
        to the user.
        r   r   s     r?   send_on_closed_streamz*H2StreamStateMachine.send_on_closed_streamT  r   r>   c                     | j                   J | j                   t        j                  k(  rt        | j                        t        d      )a  
        Called when a PUSH_PROMISE frame is received on a full stop
        stream.

        If the stream was closed by us sending a RST_STREAM frame, then we
        presume that the PUSH_PROMISE was in flight when we reset the parent
        stream. Rathen than accept the new stream, we just reset it.
        Otherwise, we should call this a PROTOCOL_ERROR: pushing a stream on a
        naturally closed stream is a real problem because it creates a brand
        new stream that the remote peer now believes exists.
        #Attempted to push on closed stream.)rm   rb   rP   r   rg   r   r   s     r?   recv_push_on_closed_streamz/H2StreamStateMachine.recv_push_on_closed_stream`  sE     $$000  N$B$BB#DNN33 EFFr>   c                     t        d      )a  
        Called when an attempt is made to push on an already-closed stream.

        This essentially overrides the standard logic by providing a more
        useful error message. It's necessary because simply indicating that the
        stream is closed is not enough: there is now a new stream that is not
        allowed to be there. The only recourse is to tear the whole connection
        down.
        r   )r   r   s     r?   send_push_on_closed_streamz/H2StreamStateMachine.send_push_on_closed_streams  s     ABBr>   c                 J    | j                   rt        d      t               }|gS )z
        Called when an informational header block is sent (that is, a block
        where the :status header has a 1XX value).

        Only enforces that these are sent *before* final headers are sent.
        z)Information response after final response)ri   r   r   r~   s      r?   send_informational_responsez0H2StreamStateMachine.send_informational_response  s'      KLLwr>   c                 l    | j                   rt        d      t               }| j                  |_        |gS )z
        Called when an informational header block is received (that is, a block
        where the :status header has a 1XX value).
        z+Informational response after final response)rk   r   r   rg   r~   s      r?   recv_informational_responsez0H2StreamStateMachine.recv_informational_response  s3    
    MNN-/..wr>   c                 T    | j                   du rg S | j                  rg S t               gS )ae  
        Called when receiving an ALTSVC frame.

        RFC 7838 allows us to receive ALTSVC frames at any stream state, which
        is really absurdly overzealous. For that reason, we want to limit the
        states in which we can actually receive it. It's really only sensible
        to receive it after we've sent our own headers and before the server
        has sent its header block: the server can't guarantee that we have any
        state around after it completes its header block, and the server
        doesn't know what origin we're talking about before we've sent ours.

        For that reason, this function applies a few extra checks on both state
        and some of the little state variables we keep around. If those suggest
        an unreasonable situation for the ALTSVC frame to have been sent in,
        we quietly ignore it (as RFC 7838 suggests).

        This function is also *not* always called by the state machine. In some
        states (IDLE, RESERVED_LOCAL, CLOSED) we don't bother to call it,
        because we know the frame cannot be valid in that state (IDLE because
        the server cannot know what origin the stream applies to, CLOSED
        because the server cannot assume we still have state around,
        RESERVED_LOCAL because by definition if we're in the RESERVED_LOCAL
        state then *we* are the server).
        F)rh   rk   r   r   s     r?   recv_alt_svcz!H2StreamStateMachine.recv_alt_svc  s3    6 ;;%I
   I ,-..r>   c                 2    | j                   rt        d      y)a  
        Called when sending an ALTSVC frame on this stream.

        For consistency with the restrictions we apply on receiving ALTSVC
        frames in ``recv_alt_svc``, we want to restrict when users can send
        ALTSVC frames to the situations when we ourselves would accept them.

        That means: when we are a server, when we have received the request
        headers, and when we have not yet sent our own response headers.
        z2Cannot send ALTSVC after sending response headers.N)ri   r   r   s     r?   send_alt_svcz!H2StreamStateMachine.send_alt_svc  s"     D  	r>   N)r3   r4   r5   __doc__ro   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r>   r?   rd   rd   S   s    % <""			$??*
0
0G&
C
&/Pr>   rd   Nc                       e Zd ZdZd Zd Zed        Zed        Zed        Z	ed        Z
d Zd!d	Zd
 Zd Zd"dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd#dZd Zd Zd Zd Zd Zd Z d Z!d  Z"y)$H2Streamaf  
    A low-level HTTP/2 stream object. This handles building and receiving
    frames and maintains per-stream state.

    This wraps a HTTP/2 Stream state machine implementation, ensuring that
    frames can only be sent/received when the stream is in a valid state.
    Attempts to create frames that cannot be sent will raise a
    ``ProtocolError``.
    c                     t        |      | _        || _        d | _        d | _        || _        t        |      | _        d | _        d| _	        d | _
        || _        y )Nr   )rd   state_machinerg   max_outbound_frame_sizerequest_methodoutbound_flow_control_windowr+   _inbound_window_manager_expected_content_length_actual_content_length
_authorityconfig)rn   rg   r   inbound_window_sizeoutbound_window_sizes        r?   ro   zH2Stream.__init__  si    
 2)<"'+$" -A) (55H'I$ )-% '(#  r>   c                 t    dt        |       j                  | j                  | j                  j                  fz  S )Nz<%s id:%d state:%r>)typer3   rg   r   rf   rn   s    r?   __repr__zH2Stream.__repr__	  s7    $JNN$$(
 
 	
r>   c                 .    | j                   j                  S )a+  
        The size of the inbound flow control window for the stream. This is
        rarely publicly useful: instead, use :meth:`remote_flow_control_window
        <h2.stream.H2Stream.remote_flow_control_window>`. This shortcut is
        largely present to provide a shortcut to this data.
        )r   current_window_sizer   s    r?   inbound_flow_control_windowz$H2Stream.inbound_flow_control_window  s     ++???r>   c                 <    t         | j                  j                     S )z
        Whether the stream is 'open' in any sense: that is, whether it counts
        against the number of concurrent streams.
        )STREAM_OPENr   rf   r   s    r?   openzH2Stream.open  s     4--3344r>   c                 P    | j                   j                  t        j                  k(  S )z/
        Whether the stream is closed.
        )r   rf   r-   r<   r   s    r?   closedzH2Stream.closed'  s!    
 !!'';+=+===r>   c                 .    | j                   j                  S )zN
        Returns how the stream was closed, as one of StreamClosedBy.
        )r   rm   r   s    r?   	closed_byzH2Stream.closed_by.  s    
 !!222r>   c                     | j                   j                  j                  d|        | j                  dk(  sJ |rt        j
                  nt        j                  }| j                  j                  |       y)z
        Called by the connection to indicate that this stream is the initial
        request/response of an upgraded connection. Places the stream into an
        appropriate state.
        zUpgrading %rr   N)	r   loggerdebugrg   rA   r_   r`   r   r|   )rn   client_siderv   s      r?   upgradezH2Stream.upgrade5  sb     	  6~~"""+6L'',, 	 	((0r>   c                    | j                   j                  j                  d||        t        j                  }| j
                  j                  s(t        |      r|rt        d      t        j                  }| j
                  j                  |      }t        | j                        }| j                  |      }| j                  ||||      }|rG| j
                  j                  t        j                         |d   j                   j#                  d       | j
                  j$                  r|st        d      | j
                  j                  r| j&                  t)        |      | _        t+        |      | _        |S )zn
        Returns a list of HEADERS/CONTINUATION frames to emit as either headers
        or trailers.
        zSend headers %s on %rz1Cannot set END_STREAM on informational responses.r   
END_STREAMz"Trailers must have END_STREAM set.)r   r   r   rA   rN   r   rh   r#   r   r[   r|   r   rg   _build_hdr_validation_flags_build_headers_framesrS   flagsaddrj   r   r$   r)   r   )	rn   headersencoder
end_streamrv   eventshfhdr_validation_flagsframess	            r?   send_headerszH2Stream.send_headersG  s@   
 	  !8'4H **##**)'2#G  "<<F##11&9$..)#??G++Wb"6
  ,,\-I-IJ1IOO-++J DEE$$)@4W=DO 4G<r>   c                 &   | j                   j                  j                  d|        | j                  j	                  t
        j                        }t        | j                        }||_	        | j                  |      }| j                  ||||      }|S )z
        Returns a list of PUSH_PROMISE/CONTINUATION frames to emit as a pushed
        stream header. Called on the stream that has the PUSH_PROMISE frame
        sent on it.
        zPush stream %r)r   r   r   r   r|   rA   rO   r   rg   promised_stream_idr   r   )rn   related_stream_idr   r   r   ppfr   r   s           r?   push_stream_in_bandzH2Stream.push_stream_in_bandx  s     	  !148
 ##11**
 t~~.!2#??G++Wc#7
 r>   c                 `    | j                   j                  t        j                        }|rJ g S )z
        Mark this stream as one that was pushed by this peer. Must be called
        immediately after initialization. Sends no frames, simply updates the
        state machine.
        )r   r|   rA   rO   )rn   r   s     r?   locally_pushedzH2Stream.locally_pushed  s2     ##11**
 z	r>   Nc                    | j                   j                  j                  d| |       | j                  j	                  t
        j                         t        | j                        }||_	        |rD| j                  j	                  t
        j                         |j                  j                  d       |"|j                  j                  d       ||_        | xj                  |j                  z  c_        | j                  dk\  sJ |gS )z
        Prepare some data frames. Optionally end the stream.

        .. warning:: Does not perform flow control checks.
        z)Send data on %r with end stream set to %sr   PADDEDr   )r   r   r   r   r|   rA   rQ   r   rg   datarS   r   r   
pad_lengthr   flow_controlled_length)rn   r   r   r   dfs        r?   	send_datazH2Stream.send_data  s     	  7z	
 	(()?)?@t~~&,,\-I-IJHHLL&!HHLL"&BM 	))R-F-FF)00A555tr>   c                    | j                   j                  j                  d|        | j                  j	                  t
        j                         t        | j                        }|j                  j                  d       |gS )z4
        End a stream without sending data.
        zEnd stream %rr   )r   r   r   r   r|   rA   rS   r   rg   r   r   )rn   r   s     r?   r   zH2Stream.end_stream  s]     	  $7(()E)EFt~~&
\"tr>   c                     | j                   j                  j                  d||        | j                  j	                  t
        j                         t        | j                        }||_	        |gS )z
        Advertise an RFC 7838 alternative service. The semantics of this are
        better documented in the ``H2Connection`` class.
        z*Advertise alternative service of %r for %r)
r   r   r   r   r|   rA   r]   r   rg   field)rn   field_valueasfs      r?   advertise_alternative_servicez&H2Stream.advertise_alternative_service  s[    
 	  8+t	
 	(()N)NO$..)	ur>   c                    | j                   j                  j                  d| |       | j                  j	                  t
        j                         | j                  j                  |       t        | j                        }||_        |gS )zS
        Increase the size of the flow control window for the remote side.
        z)Increase flow control window for %r by %d)r   r   r   r   r|   rA   rR   r   window_openedr	   rg   window_increment)rn   	incrementwufs      r?   increase_flow_control_windowz%H2Stream.increase_flow_control_window  sq     	  7)	
 	(()H)HI$$229=/(ur>   c                    | j                   j                  j                  d| |       | j                  j	                  t
        j                        }||d   _        | j                  |      }| j                  |||      |d   _
        g |fS )z
        Receives a push promise frame sent on this stream, pushing a remote
        stream. This is called on the stream that has the PUSH_PROMISE sent
        on it.
        z/Receive Push Promise on %r for remote stream %dr   )r   r   r   r   r|   rA   rU   pushed_stream_idr   _process_received_headersr   )rn   r   r   header_encodingr   r   s         r?   receive_push_promise_in_bandz%H2Stream.receive_push_promise_in_band  s     	  =$	
 ##11**
 &8q	"#??G ::)?
q	 6zr>   c                     | j                   j                  j                  d|        | j                  j	                  t
        j                        }t        |      | _        g |fS )z
        Mark this stream as one that was pushed by the remote peer. Must be
        called immediately after initialization. Sends no frames, simply
        updates the state machine.
        z%r pushed by remote peer)	r   r   r   r   r|   rA   rU   r$   r   )rn   pushed_headersr   s      r?   remotely_pushedzH2Stream.remotely_pushed  sV     	  !;TB##11**
 1@6zr>   c                    t        |      r|rt        d      t        j                  }nt        j                  }| j
                  j                  |      }|r;| j
                  j                  t        j                        }|d   |d   _        ||z  }| j                  |       t        |d   t              r|st        d      | j                  |      }| j                  |||      |d   _        g |fS )z9
        Receive a set of headers (or trailers).
        z0Cannot set END_STREAM on informational responsesr   z!Trailers must have END_STREAM set)r#   r   rA   r\   rT   r   r|   rY   r   _initialize_content_lengthrq   r   r   r  r   )rn   r   r   r	  rv   r   	es_eventsr   s           r?   receive_headerszH2Stream.receive_headers  s     %W-#F  "<<F!..F##11&9**88,,I &/q\F1I"iF''0fQi!12#$GHH#??G ::)?
q	 6zr>   c                    | j                   j                  j                  d| ||       | j                  j	                  t
        j                        }| j                  j                  |       | j                  t        |      |       |rG| j                  j	                  t
        j                        }|d   |d   _        |j                  |       ||d   _        ||d   _        g |fS )z$
        Receive some data.
        zGReceive data on %r with end stream %s and flow control length set to %dr   )r   r   r   r   r|   rA   rW   r   window_consumed_track_content_lengthlenrY   r   extendr   r   )rn   r   r   flow_control_lenr   r  s         r?   receive_datazH2Stream.receive_data&  s     	  z+;	
 ##11,2H2HI$$445EF""3t9j9**88,,I &/q\F1I"MM)$q	+;q	(6zr>   c                    | j                   j                  j                  d| |       | j                  j	                  t
        j                        }g }|r*||d   _        	 t        | j                  |      | _	        ||fS ||fS # t        $ r[ t               }| j                  |_        t        j                  |_        d|_        |g}| j#                  |j                        }Y ||fS w xY w)z3
        Handle a WINDOW_UPDATE increment.
        z/Receive Window Update on %r for increment of %dr   F)r   r   r   r   r|   rA   rX   deltar"   r   r!   r   rg   r   FLOW_CONTROL_ERRORr   r   reset_stream)rn   r  r   r   r   s        r?   receive_window_updatezH2Stream.receive_window_update=  s     	  =)	
 ##11++
 
 'F1IO=4J5551 v~vv~ $ 	= $"&..#-#@#@ %*"**5+;+;<v~	=s    B AC'&C'c                     | j                   j                  j                  d|        | j                  j	                  t
        j                         J d       )z
        A naked CONTINUATION frame has been received. This is always an error,
        but the type of error it is depends on the state of the stream and must
        transition the state of the stream, so we need to handle it.
        z Receive Continuation frame on %rzShould not be reachable)r   r   r   r   r|   rA   rZ   r   s    r?   receive_continuationzH2Stream.receive_continuationa  sI     	  !CTJ((**	
 	0//ur>   c                 F   | j                   j                  j                  d|        |j                  rg g fS | j                  j                  t        j                        }|r=t        |d   t              sJ | j                  |d   _        |j                  |d   _        g |fS )z
        An Alternative Service frame was received on the stream. This frame
        inherits the origin associated with this stream.
        z.Receive Alternative Service frame on stream %rr   )r   r   r   originr   r|   rA   r^   rq   r   r   r   r   rn   framer   s      r?   receive_alt_svczH2Stream.receive_alt_svcm  s    
 	  <d	

 <<r6M##1111
 fQi)DEEE#F1I$)KKF1I!6zr>   c                     | j                   j                  j                  d| |       | j                  j	                  t
        j                         t        | j                        }||_	        |gS )zP
        Close the stream locally. Reset the stream with an error code.
        z"Local reset %r with error code: %d)
r   r   r   r   r|   rA   rP   r
   rg   r   )rn   r   rsfs      r?   r  zH2Stream.reset_stream  s[     	  0$
	
 	(()E)EFT^^,#ur>   c                     | j                   j                  j                  d| |j                         | j                  j                  t        j                        }|rt        |j                        |d   _        g |fS )z7
        Handle a stream being reset remotely.
        z#Remote reset %r with error code: %dr   )	r   r   r   r   r   r|   rA   rV   r   r"  s      r?   r   zH2Stream.stream_reset  sk     	  149I9I	
 ##11,2N2NO#78H8H#IF1I 6zr>   c                     | j                   j                  j                  d||        | j                  j	                  |      }|rt        | j                        }||_        |gS g S )z
        The user has informed us that they've processed some amount of data
        that was received on this stream. Pass that to the window manager and
        potentially return some WindowUpdate frames.
        z,Acknowledge received data with size %d on %r)r   r   r   r   process_bytesr	   rg   r  )rn   acknowledged_sizer  fs       r?   acknowledge_received_dataz"H2Stream.acknowledge_received_data  sf     	  :t	
 00>>
	 !$..1A!*A3J	r>   c                     t        |d   t        t        f      }t        |d   t        t        t
        f      }t        |d   t        t        f      }t        | j                  j                  |||      S )z|
        Constructs a set of header validation flags for use when normalizing
        and validating header blocks.
        r   )	is_client
is_traileris_response_headeris_push_promise)rq   r   r   r   r   r   r   r   r(   r   rh   )rn   r   r/  r0  r1  s        r?   r   z$H2Stream._build_hdr_validation_flags  s    
  1I'78

 (1I -
 %1I,.@A
 %((//!1+	
 	
r>   c                    | j                   j                  rt        ||      }| j                   j                  rt        ||      }|j                  |      }t	        dt        |      | j                        D cg c]  }|||| j                  z     }}g }|d   |_        |j                  |       |dd D ]/  }	t        | j                        }
|	|
_        |j                  |
       1 |d   j                  j                  d       |S c c}w )zH
        Helper method to build headers or push promise frames.
        r   r   NEND_HEADERS)r   r'   r&   encoderanger  r   r   appendr   rg   r   r   )rn   r   r   first_framer   encoded_headersiheader_blocksr   blockcfs              r?   r   zH2Stream._build_headers_frames  s    ;;110-G ;;00/-G "..1 3')E)E
 Aa < <<= 	 
 (+k""12&E"4>>2BBGMM" '
 	r
]+#
s   8Dc                     | j                   j                  rt        ||      }| j                   j                  rt        ||      }|rt	        ||      }t        |      S )z
        When headers have been received from the remote peer, run a processing
        pipeline on them to transform them into the appropriate form for
        attaching to an event.
        )r   r*   validate_inbound_headersr%   _decode_headerslist)rn   r   header_validation_flagsr	  s       r?   r  z"H2Stream._process_received_headers  sX     ;;00/0G ;;//&w0GHG%g?G G}r>   c                     | j                   dk(  rd| _        y|D ]  \  }}|dk(  s	 t        |d      | _         y y# t        $ r t	        d|z        w xY w)z
        Checks the headers for a content-length header and initializes the
        _expected_content_length field from it. It's not an error for no
        Content-Length header to be present.
        s   HEADr   Ns   content-lengthrE   z!Invalid content-length header: %s)r   r   intrr   r   )rn   r   nvs       r?   r  z#H2Stream._initialize_content_length  sr     '),-D)DAq%%472JD1   " ';a? s	   <Ac                     | xj                   |z  c_         | j                   }| j                  }|&||k  rt        ||      |r||k7  rt        ||      yyy)a  
        Update the expected content length in response to data being received.
        Validates that the appropriate amount of data is sent. Always updates
        the received data, but only validates the length against the
        content-length header if one was sent.

        :param length: The length of the body chunk received.
        :param end_stream: If this is the last body chunk received.
        N)r   r   r    )rn   lengthr   actualexpecteds        r?   r  zH2Stream._track_content_length+  si     	##v-#,,00& ,Xv>>h&0,Xv>> 1z	  r>   c                     | j                   j                  |z   }| j                   j                  |       || j                   _        y)aY  
        We changed SETTINGS_INITIAL_WINDOW_SIZE, which means we need to
        update the target window size for flow control. For our flow control
        strategy, this means we need to do two things: we need to adjust the
        current window size, but we also need to set the target maximum window
        size to the new value.
        N)r   max_window_sizer  )rn   r  new_max_sizes      r?   *_inbound_flow_control_change_from_settingsz3H2Stream._inbound_flow_control_change_from_settings@  s<     33CCeK$$22597C$$4r>   )F)FN)r   )#r3   r4   r5   r   ro   r   propertyr   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r  r$  r  r   r,  r   r   r  r  r  rN  r=   r>   r?   r   r     s    8
 @ @ 
5 
5 > > 3 3$/b06	0 D."H
06(
6)V0*?*
Dr>   r   c              #      K   | D ]O  }t        |t              sJ |\  }}|j                  |      }|j                  |      }|j                  ||       Q yw)z
    Given an iterable of header two-tuples and an encoding, decodes those
    headers using that encoding while preserving the type of the header tuple.
    This ensures that the use of ``HeaderTuple`` is preserved.
    N)rq   r   decode	__class__)r   encodingheadernamevalues        r?   r@  r@  M  s\       &+...e{{8$X&tU++ s   AA)lr   enumr   r   hpackr   hyperframe.framer   r   r   r	   r
   r   r   errorsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
exceptionsr   r   r    r!   	utilitiesr"   r#   r$   r%   r&   r'   r(   r)   r*   windowsr+   r-   rA   rb   r6  r  r   r9   r;   r:   rd   r6   rN   r   rT   r   rW   r   r<   rO   r   r8   rU   r   r7   r^   r_   r`   r   rR   rX   r   rP   r   rV   r   r]   r   r   r   rQ   r   rS   rY   r   r   r   r[   r   r\   r   r   r   r   r   r   r   rs   r   r@  )_s   0r?   <module>r_     sm      
 5      
 #' 4 ,T  $As;'7898u89 $K -1K)) *.2K** + NF|001		*	*K,<,<=F |001		.	.0@0@AF |--.		3	3[5G5GHF |556		4	4&&	(F |556		4	4''	)F |<<=	{ F  |223		*	*K,I,IJ#F$ |223		.	.**	,'F. !:!:;		+	+[-K-KL1F2 !7!78		3	3[5G5GH5F6 !@!@A	{))*9F: !@!@A		,	,k.H.HI=F> !=!=>		/	/1C1CDAFB !=!=>		*	*K,>,>?EFF !F!FG		*	*K,F,FGIFJ !F!FG	{))*MFR   ,";";<		/	/))	+UFX   ,"8"89		3	3[5G5GH[F\   ,"A"AB	{**+_F`   ,"A"AB		,	,k.I.IJcFd   ,">">?		/	/1C1CDgFh   ,">">?		*	*K,>,>?kFl   ,"G"GH		*	*K,G,GHoFt |001		+	+[-=-=>wFx |001		/	/1A1AB{F| |--.	{ F@ |--.		+	+[-=-=>CFD |334	{,,-GFH |334		0	0		'	'	)KFN |667	{ QFR |667		,	,k.>.>?UFV |334		/	/1C1CDYFZ |334		*	*K,>,>?]F^ |556		/	/1A1ABaFb |556		/	/1A1ABeFf |>>?		9	9;;K;KLiFj |>>?		9	9;;K;KLmFn |<<=		*	*K,<,<=qFr |<<=		*	*K,<,<=uFz ##\%>%>?		+	+[-K-KL}F~ ##\%>%>?		3	3[5G5GHAFB ##\%;%;<	{--.EFF ##\%;%;<		3	3[5G5GHIFJ ##\%A%AB		-	-{/A/ABMFN ##\%D%DE	{--.QFR ##\%D%DE		,	,k.L.LMUFV ##\%A%AB		/	/1C1CDYFZ ##\%A%AB		*	*K,>,>?]F^ ##\%C%CD		/	/**	,aFd ##\%C%CD		3	3[5G5GHgFh ##\%L%LM		9	9**	,kFn ##\%J%JK		*	*K,J,JKqFr ##\%J%JK		*	*K,J,JKuFz ""L$=$=>		/	/))	+}F@ ""L$:$:;		+	+[-J-JKCFD ""L$@$@A		*	*K,>,>?GFH ""L$C$CD	{,,-KFL ""L$C$CD		,	,k.K.KLOFP ""L$@$@A		/	/1C1CDSFT ""L$@$@A		*	*K,>,>?WFX ""L$B$BC		/	/))	+[F^ ""L$K$KL		9	9))	+aFd ""L$I$IJ		*	*K,I,IJgFh ""L$I$IJ		*	*K,I,IJkFp 556	{!!"sFt >>?	{!!"wFJ 223		3	3[5G5GHMFN //0		3	3[5G5GHQF\ 889	{!!"556	{!!"
 778		8	8+:L:LM 223		3	3[5G5GH778		8	8+:L:LM556		3	3[5G5GH//0		3	3[5G5GH889		3	3[5G5GH556		3	3[5G5GHKFRg	D g	DT,A( :s   %	z