Format: application/x-python-code
⬇ Download File (16.3 KB)
   1  o
   2  
   3  7'h�]�@sddlmZddlZddlZddlZddlZddlZddlZddl	Zddl
   4  ZddlZddlZddl
   5  mZddlmZmZmZddlmZmZmZddlmZmZmZddlmZd	d
   6  lmZe�d�Z ddd�Z!Gdd�dej"�Z#Gdd�de�Z$Gdd�d�Z%dS)�)�annotationsN�)�AsyncNetworkStream)�ConnectionNotAvailable�LocalProtocolError�RemoteProtocolError)�Origin�Request�Response)�	AsyncLock�AsyncSemaphore�AsyncShieldCancellation)�Trace�)�AsyncConnectionInterfacezhttpcore.http2�requestr	�return�boolcCstdd�|jD��S)Ncss,�|]\}}|��dkp|��dkVqdS)scontent-length�transfer-encodingN��lower��.0�k�v�r�KC:\Users\shubh\Videos\agent\nova\Lib\site-packages\httpcore/_async/http2.py�	<genexpr>s
   7  ��
   8  �z#has_body_headers.<locals>.<genexpr>)�any�headers)rrrr�has_body_headerss�r c@seZdZdZdZdZdS)�HTTPConnectionStaterr�N)�__name__�
   9  __module__�__qualname__�ACTIVE�IDLE�CLOSEDrrrrr!$sr!c@s2eZdZdZejjdd�Z	dWdXdd�ZdYdd�Z	dZdd�Z
  10  d[dd�Zd[dd�Zd\dd�Z
  11  d[d d!�Zd]d#d$�Zd^d&d'�Zd_d)d*�Z	dWd`d,d-�Zdad0d1�Zdbd2d3�Zdcd4d5�Zddd7d8�ZdZd9d:�Zded;d<�Zdfd>d?�Zdgd@dA�ZdgdBdC�ZdgdDdE�ZdgdFdG�ZdhdIdJ�ZdhdKdL�ZdidMdN�Z 			djdkdUdV�Z!dS)l�AsyncHTTP2Connection�F)Zvalidate_inbound_headersN�originr�streamr�keepalive_expiry�float | NonecCs�||_||_||_tjj|jd�|_tj	|_
  12  d|_d|_t
  13  �|_t
  14  �|_t
  15  �|_t
  16  �|_d|_d|_d|_i|_d|_d|_d|_dS)N)�configrF)�_origin�_network_stream�_keepalive_expiry�h2�
  17  connectionZH2Connection�CONFIG�	_h2_stater!r'�_state�
  18  _expire_at�_request_countr�
  19  _init_lock�_state_lock�
  20  _read_lock�_write_lock�_sent_connection_init�_used_all_stream_ids�_connection_error�_events�_connection_terminated�_read_exception�_write_exception)�selfr+r,r-rrr�__init__.s&�
  21  zAsyncHTTP2Connection.__init__rr	rr
  22  c
  23  �sD�|�|jj�std|jj�d|j����|j4IdH�&|jtjtj	fvr5|j
  24  d7_
  25  d|_tj|_nt��Wd�IdHn1IdHsHwY|j
  26  4IdH��|js�z2d|i}tdt||�4IdH�|jdi|��IdHWd�IdHn1IdHs�wYWn)ty�}zt��|��IdHWd�|�1s�wY|�d}~wwd|_d|_|jjj}t|�|_t||j�D]
  27  }|j��IdHq�Wd�IdHn1IdHs�wY|j��IdHz|j��}g|j|<Wntj j!�yd|_"|j
  28  d8_
  29  t��wz�||d�}tdt||�4IdH�|j#||d�IdHWd�IdHn1IdH�sDwYtd	t||�4IdH�|j$||d�IdHWd�IdHn1IdH�spwYtd
  30  t||�4IdH�}|j%||d�IdH\}	}
  31  |	|
  32  f|_&Wd�IdHn1IdH�s�wYt'|	|
  33  t(|||d�d|j)|d
  34  �d�WSt�y!}zZt��7d|i}tdt||�4IdH�|j*|d�IdHWd�IdHn1IdH�s�wYWd�n	1�swYt+|tj j,��r|j-�rt.|j-��t/|��|�d}~ww)NzAttempted to send request to z on connection to rrZsend_connection_initT�r�	stream_idZsend_request_headersZsend_request_bodyZreceive_response_headers�rHsHTTP/2)�http_versionZnetwork_streamrH)�statusr�content�
  35  extensionsrH�response_closedr)0�can_handle_request�urlr+�RuntimeErrorr0r;r7r!r&r'r9r8rr:r>r�logger�_send_connection_init�
  36  BaseExceptionr
  37  �aclose�_max_streamsr6�local_settings�max_concurrent_streamsr�_max_streams_semaphore�range�acquireZget_next_available_stream_idrAr3�
  38  exceptionsZNoAvailableStreamIDErrorr?�_send_request_headers�_send_request_body�_receive_response�return_valuer
  39  �HTTP2ConnectionByteStreamr1�_response_closed�
  40  isinstance�
  41  ProtocolErrorrBrr)rErZ
  42  sci_kwargs�excZlocal_settings_max_streams�_rH�kwargs�tracerKrrrr�handle_async_requestUs����
  43  (��(��
  44  �����
  45  �(�
  46  �
  47  *�*���*���
  48  *���
  49  
  50  ��z)AsyncHTTP2Connection.handle_async_request�Nonec�sn�tjjdtjjjdtjjjdtjjjdid�|j_|jjtjjj	=|j�
  51  �|j�d�|�|�IdHdS)z�
  52          The HTTP/2 connection requires some initial setup before we can start
  53          using individual request/response streams on it.
  54          Tr�dr*)�clientZinitial_values�N)
  55  r3�settingsZSettings�SettingCodesZENABLE_PUSH�MAX_CONCURRENT_STREAMSZMAX_HEADER_LIST_SIZEr6rWZENABLE_CONNECT_PROTOCOLZinitiate_connection�increment_flow_control_window�_write_outgoing_data)rErrrrrS�s�
  56  
  57  
  58  �
  59  ��
  60  z*AsyncHTTP2Connection._send_connection_initrH�intc�s��t|�}dd�|jD�d}d|jfd|fd|jjfd|jjfgdd�|jD�}|jj|||d	�|jjd
  61  |d�|�	|�IdHdS)
  62  z@
  63          Send the request headers to a given stream ID.
  64          cSs g|]\}}|��dkr|�qS)�hostrrrrr�
  65  <listcomp>�s z>AsyncHTTP2Connection._send_request_headers.<locals>.<listcomp>rs:methods
  66  :authoritys:schemes:pathcSs(g|]\}}|��dvr|��|f�qS))rtrrrrrrru�s�
  67  �)�
  68  end_streamrmrIN)
  69  r r�methodrP�scheme�targetr6�send_headersrqrr)rErrHrv�	authorityrrrrr]�s�
  70  
  71  
  72  ���z*AsyncHTTP2Connection._send_request_headersc�s`�t|�sdSt|jtj�sJ�|j2z3dHW}|�|||�IdHq6|�||�IdHdS)zP
  73          Iterate over the request body sending it to a given stream ID.
  74          N)r rcr,�typing�
  75  AsyncIterable�_send_stream_data�_send_end_stream)rErrH�datarrrr^�s��z'AsyncHTTP2Connection._send_request_bodyr��bytesc�sj�|r3|�||�IdH}tt|�|�}|d|�||d�}}|j�||�|�|�IdH|sdSdS)zI
  76          Send a single chunk of data in one or more data frames.
  77          N)�_wait_for_outgoing_flow�min�lenr6Z	send_datarr)rErrHr�Zmax_flow�
  78  chunk_size�chunkrrrr~	s��z&AsyncHTTP2Connection._send_stream_datac�s"�|j�|�|�|�IdHdS)z`
  79          Send an empty data frame on on a given stream ID with the END_STREAM flag set.
  80          N)r6rvrr)rErrHrrrrs�z%AsyncHTTP2Connection._send_end_stream�%tuple[int, list[tuple[bytes, bytes]]]c�s��	|�||�IdH}t|tjj�rnqd}g}|jdusJ�|jD]\}}|dkr4t|jddd��}q"|�d�s@|�	||f�q"||fS)	zT
  81          Return the response status code and headers for a given stream ID.
  82          TN��s:status�ascii�ignore)�errors�:)
  83  �_receive_stream_eventrcr3�events�ResponseReceivedrrs�decode�
  84  startswith�append)rErrH�event�status_coderrrrrrr_s ��
  85  �z&AsyncHTTP2Connection._receive_response�typing.AsyncIterator[bytes]cCs��	|�||�IdH}t|tjj�r7|jdusJ�|jdus J�|j}|j�||�|�	|�IdH|jVn	t|tjj
  86  �r@dSq)z]
  87          Iterator that returns the bytes of the response body for a given stream ID.
  88          TN)r�rcr3r��DataReceivedZflow_controlled_lengthr�r6Zacknowledge_received_datarr�StreamEnded)rErrHr��amountrrr�_receive_response_body5s�
  89  �z+AsyncHTTP2Connection._receive_response_body�Kh2.events.ResponseReceived | h2.events.DataReceived | h2.events.StreamEndedc�sV�|j�|�s|�||�IdH|j�|�r|j|�d�}t|tjj�r)t|��|S)z�
  90          Return the next available event for a given stream ID.
  91  
  92          Will read more data from the network if required.
  93          Nr)	rA�get�_receive_events�poprcr3r��StreamResetr)rErrHr�rrrr�Gs��z*AsyncHTTP2Connection._receive_stream_event�
  94  int | Nonec�sz�|j4IdH��|jdur)|jj}|r$|r$||kr$|jd8_t��t|j��|dus3|j�|�s�|�|�IdH}|D]`}t	|t
  95  jj�rrt
  96  dt|�4IdH�}|�|�IdH||_Wd�IdHn1IdHslwYq=t	|t
  97  jjt
  98  jjt
  99  jjt
 100  jjf�r�|j|jvr�|j|j�|�q=t	|t
 101  jj�r�||_q=Wd�IdHn1IdHs�wY|�|�IdHdS)zp
 102          Read some data from the network until we see one or more events
 103          for a given stream ID.
 104          NrZreceive_remote_settings)r<rB�last_stream_idr9rrrAr��_read_incoming_datarcr3r�ZRemoteSettingsChangedrrR�_receive_remote_settings_changer`r�r�r�r�rHr�ZConnectionTerminatedrr)rErrHr�r�r�rhrrrr�VsH�
 105  
 106  �(����	��(�'z$AsyncHTTP2Connection._receive_eventsr��h2.events.RemoteSettingsChangedc�s��|j�tjjj�}|rPt|j|jj	j
 107  �}|rR||jkrT||jkr5|j�
 108  �IdH|jd7_||jks!||jkrV|j��IdH|jd8_||jks:dSdSdSdSdS)Nr)Zchanged_settingsr�r3rnrorpr��	new_valuer6rWrXrVrY�releaser[)rEr�rXZnew_max_streamsrrrr��s*���
 109  
 110  �
 111  ��z4AsyncHTTP2Connection._receive_remote_settings_changec	�s�|j��IdH|j|=|j4IdH�l|jr#|js#|��IdHn<|jtjkrT|jsjtj	|_|j
 112  dur?t��}||j
 113  |_
 114  |jru|��IdHWd�IdHdSWd�IdHdSWd�IdHdSWd�IdHdSWd�IdHdS1IdHs�wYdS�N)rYr�rAr;rBrUr7r!r&r'r2�time�	monotonicr8r?)rErH�nowrrrrb�s,�
 115  ����	.�z%AsyncHTTP2Connection._response_closedc�s(�|j��tj|_|j��IdHdSr�)r6Zclose_connectionr!r(r7r1rU�rErrrrU�s�
 116  zAsyncHTTP2Connection.aclose�list[h2.events.Event]c
 117  �s��|j�di�}|�dd�}|jdur|j�z|j�|j|�IdH}|dkr*td��Wnty?}z||_d|_|�d}~ww|j	�
 118  |�}|S)N�timeout�read�zServer disconnectedT)rMr�rCr1r��READ_NUM_BYTESr�	Exceptionr@r6Zreceive_data)rEr�timeoutsr�r�rer�rrrr��s$�
 119  �	��
 120  z(AsyncHTTP2Connection._read_incoming_datac�s��|j�di�}|�dd�}|j4IdH�:|j��}|jdur#|j�z|j�||�IdHWntyC}z||_d|_	|�d}~wwWd�IdHdS1IdHsUwYdS)Nr��writeT)
 121  rMr�r=r6�data_to_sendrDr1r�r�r@)rErr�r�r�rerrrrr�s"�
 122  
 123  	���.�z)AsyncHTTP2Connection._write_outgoing_datac�sb�|j�|�}|jj}t||�}|dkr/|�|�IdH|j�|�}|jj}t||�}|dks|S)a
 124          Returns the maximum allowable outgoing flow for a given stream.
 125  
 126          If the allowable flow is zero, then waits on the network until
 127          WindowUpdated frames have increased the flow rate.
 128          https://tools.ietf.org/html/rfc7540#section-6.9
 129          rN)r6Zlocal_flow_control_windowZmax_outbound_frame_sizer�r�)rErrHZ
 130  local_flowZmax_frame_sizeZflowrrrr��s�
 131  
 132  �z,AsyncHTTP2Connection._wait_for_outgoing_flowrcCs
 133  ||jkSr�)r0�rEr+rrrrO�s
 134  z'AsyncHTTP2Connection.can_handle_requestcCs2|jtjko|jo|jo|jjjtj	j
 135  jkSr�)r7r!r(r@r?r6Z
 136  state_machine�stater3r4ZConnectionStater�rrr�is_available�s�����z!AsyncHTTP2Connection.is_availablecCst��}|jduo
 137  ||jkSr�)r�r�r8)rEr�rrr�has_expired
 138  sz AsyncHTTP2Connection.has_expiredcC�|jtjkSr�)r7r!r'r�rrr�is_idle�zAsyncHTTP2Connection.is_idlecCr�r�)r7r!r(r�rrr�	is_closedr�zAsyncHTTP2Connection.is_closed�strcCs$t|j�}|�d|jj�d|j��S)Nz
 139  , HTTP/2, �, Request Count: )r�r0r7�namer9r�rrr�infos
 140  
 141  ��zAsyncHTTP2Connection.infoc	Cs6|jj}t|j�}d|�d|�d|jj�d|j�d�	S)N�<z [z, r�z]>)�	__class__r#r�r0r7r�r9)rE�
 142  class_namer+rrr�__repr__s
 143  ��zAsyncHTTP2Connection.__repr__c�s�|Sr�rr�rrr�
 144  __aenter__&s�zAsyncHTTP2Connection.__aenter__�exc_type�type[BaseException] | None�	exc_value�BaseException | None�	traceback�types.TracebackType | Nonec�s�|��IdHdSr�)rU)rEr�r�r�rrr�	__aexit__)s�zAsyncHTTP2Connection.__aexit__r�)r+rr,rr-r.)rr	rr
 145  )rr	rrj)rr	rHrsrrj)rr	rHrsr�r�rrj)rr	rHrsrr�)rr	rHrsrr�)rr	rHrsrr�)rr	rHr�rrj)r�r�rrj)rHrsrrj�rrj)rr	rr�)rr	rHrsrrs)r+rrr)rr)rr�)rr))NNN)r�r�r�r�r�r�rrj)"r#r$r%r�r3r/ZH2Configurationr5rFrirSr]r^r~rr_r�r�r�r�rbrUr�rrr�rOr�r�r�r�r�r�r�r�rrrrr)*sD�
 146  '
 147  h
 148  !
 149  
 150  
 151  
 152  	
 153  
 154  �
 155  0
 156  
 157  
 158  	
 159  
 160  
 161  
 162  
 163  
 164  
 165  
 166  
 167  
 168  �r)c@s*eZdZdd	d
 169  �Zddd
 170  �Zddd�ZdS)rar4r)rr	rHrsrrjcCs||_||_||_d|_dS)NF)�_connection�_request�
 171  _stream_id�_closed)rEr4rrHrrrrF3s
 172  z"HTTP2ConnectionByteStream.__init__r�cCs��|j|jd�}z<tdt|j|�4IdH�"|jj|j|jd�2z	3dHW}|Vq 6Wd�IdHWdS1IdHs=wYWdStym}zt��|��IdHWd�|�1sbwY|�d}~ww)NrGZreceive_response_body)	r�r�rrRr�r�rTr
 173  rU)rErgr�rerrr�	__aiter__;s&���2�
 174  ����z#HTTP2ConnectionByteStream.__aiter__c	�s|�|js<d|_d|ji}tdt|j|�4IdH�|jj|jd�IdHWd�IdHdS1IdHs5wYdSdS)NTrHrNrI)r�r�rrRr�r�rb)rErgrrrrUKs�
 175  .��z HTTP2ConnectionByteStream.acloseN)r4r)rr	rHrsrrj)rr�r�)r#r$r%rFr�rUrrrrra2s
 176  
 177  ra)rr	rr)&�
 178  __future__r�enum�loggingr��typesr|Z	h2.configr3Z
 179  h2.connectionZ	h2.eventsZ
 180  h2.exceptionsZh2.settingsZ_backends.baser�_exceptionsrrrZ_modelsrr	r
 181  Z_synchronizationrrr
 182  �_tracerZ
 183  interfacesr�	getLoggerrRr �IntEnumr!r)rarrrr�<module>s4
 184  
 185