o
    @ZcJ                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Zddl
mZmZmZ ddlmZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ d
edefddZG dd de jZG dd deZG dd dZ dS )    N   )ConnectionNotAvailableLocalProtocolErrorRemoteProtocolError)OriginRequestResponse)Lock	Semaphore)Trace)NetworkStream   )ConnectionInterfacerequestreturnc                 C   s   t dd | jD S )Nc                 S   s(   g | ]\}}|  d kp|  dkqS )s   content-length   transfer-encodinglower.0kv r   J/var/www/chikooza/env/lib/python3.10/site-packages/httpcore/_sync/http2.py
<listcomp>   s    z$has_body_headers.<locals>.<listcomp>)anyheaders)r   r   r   r   has_body_headers   s
   r   c                   @   s   e Zd ZdZdZdZdS )HTTPConnectionStater   r      N)__name__
__module____qualname__ACTIVEIDLECLOSEDr   r   r   r   r   !   s    r   c                   @   s   e Zd ZdZejjddZ	d<dede	de
je fdd	Zd
edefddZd
eddfddZd
ededdfddZd
ededdfddZd
edede
jee
je
jeef  f fddZd
edede
je fddZd
ededejjfddZ	d<d
ede
je ddfddZdeddfddZd=dd Z d
ede
jejj fd!d"Z!d
eddfd#d$Z"d
ededefd%d&Z#dede$fd'd(Z%de$fd)d*Z&de$fd+d,Z'de$fd-d.Z(de$fd/d0Z)de*fd1d2Z+de*fd3d4Z,d>d5d6Z-			d?d7e
je
j.e/  d8e
je/ d9e
je0j1 ddfd:d;Z2dS )@HTTP2Connection   F)validate_inbound_headersNoriginstreamkeepalive_expiryc                 C   s   || _ || _|| _tjj| jd| _tj	| _
d | _d| _t | _t | _t | _t | _d| _d| _d| _i | _d | _d | _d | _d S )N)configr   F)_origin_network_stream_keepalive_expiryh2
connectionH2ConnectionCONFIG	_h2_stater   r$   _state
_expire_at_request_countr	   
_init_lock_state_lock
_read_lock_write_lock_sent_connection_init_used_all_stream_ids_connection_error_events_read_exception_write_exception_connection_error_event)selfr)   r*   r+   r   r   r   __init__+   s$   
zHTTP2Connection.__init__r   r   c           	      C   s  |  |jjstd|jj d| j | j# | jtjtj	fv r0|  j
d7  _
d | _tj| _nt W d    n1 s=w   Y  | j: | jsxd|i}td|| | jdi | W d    n1 sfw   Y  d| _| jjj}t|| _W d    n1 sw   Y  | j  z| j }g | j|< W n tjjy   d| _t w zu||d}td|| | j||d W d    n1 sw   Y  td	|| | j||d W d    n1 sw   Y  td
||}| j||d\}}||f|_ W d    n	1 sw   Y  t!||t"| ||d|dddW S  t#yb } z8d|i}td|| | j$|d W d    n	1 sBw   Y  t%|tjj&r\| j'rXt(| j't)||d }~ww )NzAttempted to send request to z on connection to r   r   zhttp2.send_connection_initTr   	stream_idzhttp2.send_request_headerszhttp2.send_request_bodyzhttp2.receive_response_headersrF   s   HTTP/2)rF   http_version)statusr   content
extensionsrF   http2.response_closedr   )*can_handle_requesturlr)   RuntimeErrorr-   r9   r5   r   r#   r$   r7   r6   r   r8   r<   r   _send_connection_initr4   local_settingsmax_concurrent_streamsr
   _max_streams_semaphoreacquireget_next_available_stream_idr?   r0   
exceptionsNoAvailableStreamIDErrorr=   _send_request_headers_send_request_body_receive_responsereturn_valuer   HTTP2ConnectionByteStream	Exception_response_closed
isinstanceProtocolErrorrB   r   r   )	rC   r   kwargsmax_streamsrF   tracerI   r   excr   r   r   handle_requestD   s   



	




zHTTP2Connection.handle_requestc                 C   sf   t jjdt jjjdt jjjdt jjjdid| j_| jjt jjj	= | j
  | jd | | dS )z
        The HTTP/2 connection requires some initial setup before we can start
        using individual request/response streams on it.
        Tr   d   r'   )clientinitial_values   N)r0   settingsSettingsSettingCodesENABLE_PUSHMAX_CONCURRENT_STREAMSMAX_HEADER_LIST_SIZEr4   rQ   ENABLE_CONNECT_PROTOCOLinitiate_connectionincrement_flow_control_window_write_outgoing_data)rC   r   r   r   r   rP      s   




z%HTTP2Connection._send_connection_initrF   c                 C   s   t | }dd |jD d }d|jfd|fd|jjfd|jjfgdd |jD  }| jj|||d	 | jjd
|d | 	| d S )Nc                 S   s    g | ]\}}|  d kr|qS )   hostr   r   r   r   r   r      s     z9HTTP2Connection._send_request_headers.<locals>.<listcomp>r   s   :methods
   :authoritys   :schemes   :pathc                 S   s(   g | ]\}}|  d vr|  |fqS ))rt   r   r   r   r   r   r   r      s    
)
end_streamri   rG   )
r   r   methodrN   schemetargetr4   send_headersrr   rs   )rC   r   rF   ru   	authorityr   r   r   r   rX      s   


z%HTTP2Connection._send_request_headersc                 C   s   t |sd S t|jtjsJ |jD ],}|r>| ||}tt||}|d | ||d  }}| j	|| | 
| |sq| j| | 
| d S N)r   r_   r*   typingIterable_wait_for_outgoing_flowminlenr4   	send_datars   ru   )rC   r   rF   datamax_flow
chunk_sizechunkr   r   r   rY      s   

z"HTTP2Connection._send_request_bodyc                 C   st   	 |  ||}t|tjjrnqd}g }|jD ]\}}|dkr)t|jddd}q|ds5|	||f q||fS )NT   s   :statusasciiignore)errors   :)
_receive_stream_eventr_   r0   eventsResponseReceivedr   intdecode
startswithappend)rC   r   rF   eventstatus_coder   r   r   r   r   r   rZ      s   
z!HTTP2Connection._receive_responsec                 c   sb    	 |  ||}t|tjjr#|j}| j|| | | |j	V  nt|tjj
tjjfr0d S qr{   )r   r_   r0   r   DataReceivedflow_controlled_lengthr4   acknowledge_received_datars   r   StreamEndedStreamReset)rC   r   rF   r   amountr   r   r   _receive_response_body   s   

z&HTTP2Connection._receive_response_bodyc                 C   sJ   | j |s| || | j |r| j | d}t|dr#t||S )Nr   
error_code)r?   get_receive_eventspophasattrr   )rC   r   rF   r   r   r   r   r     s   
z%HTTP2Connection._receive_stream_eventc                 C   s   | j I | jd urt| j|d u s| j|sE| |}|D ]%}t|dd}t|dr7|dkr7|| _t||| jv rD| j| | qW d    n1 sOw   Y  | 	| d S )NrF   r   r   )
r:   rB   r   r?   r   _read_incoming_datagetattrr   r   rs   )rC   r   rF   r   r   event_stream_idr   r   r   r     s   



zHTTP2Connection._receive_eventsc                 C   s   | j   | j|= | jD | jtjkr8| js@tj| _| jd ur)t	
 }|| j | _| jrH|   W d    d S W d    d S W d    d S W d    d S 1 sSw   Y  d S r{   )rS   releaser?   r9   r5   r   r#   r$   r/   time	monotonicr6   r=   close)rC   rF   nowr   r   r   r^   *  s"   


"z HTTP2Connection._response_closedc                 C   s    | j   tj| _| j  d S r{   )r4   close_connectionr   r%   r5   r.   r   rC   r   r   r   r   6  s   
zHTTP2Connection.closec              
   C   s   |j di }|dd }| jd ur| jz| j| j|}|dkr&tdW n ty; } z|| _d| _|d }~ww | j	
|}|S )Ntimeoutread    zServer disconnectedT)rK   r   r@   r.   r   READ_NUM_BYTESr   r]   r>   r4   receive_data)rC   r   timeoutsr   r   rd   r   r   r   r   r   ?  s"   
	z#HTTP2Connection._read_incoming_datac                 C   s   |j di }|dd }| j4 | j }| jd ur| jz	| j|| W n ty; } z|| _d| _	|d }~ww W d    d S 1 sGw   Y  d S )Nr   writeT)
rK   r   r;   r4   data_to_sendrA   r.   r   r]   r>   )rC   r   r   r   r   rd   r   r   r   rs   ]  s    

	"z$HTTP2Connection._write_outgoing_datac                 C   sZ   | j |}| j j}t||}|dkr+| | | j |}| j j}t||}|dks|S )a  
        Returns the maximum allowable outgoing flow for a given stream.

        If the allowable flow is zero, then waits on the network until
        WindowUpdated frames have increased the flow rate.
        https://tools.ietf.org/html/rfc7540#section-6.9
        r   )r4   local_flow_control_windowmax_outbound_frame_sizer   r   )rC   r   rF   
local_flowmax_frame_sizeflowr   r   r   r~   x  s   


z'HTTP2Connection._wait_for_outgoing_flowc                 C   s
   || j kS r{   )r-   rC   r)   r   r   r   rM     s   
z"HTTP2Connection.can_handle_requestc                 C   s   | j tjko| j o| j S r{   )r5   r   r%   r>   r=   r   r   r   r   is_available  s
   zHTTP2Connection.is_availablec                 C   s   t  }| jd uo|| jkS r{   )r   r   r6   )rC   r   r   r   r   has_expired  s   zHTTP2Connection.has_expiredc                 C      | j tjkS r{   )r5   r   r$   r   r   r   r   is_idle     zHTTP2Connection.is_idlec                 C   r   r{   )r5   r   r%   r   r   r   r   	is_closed  r   zHTTP2Connection.is_closedc                 C   s$   t | j}|d| jj d| j S )Nz
, HTTP/2, , Request Count: )strr-   r5   namer7   r   r   r   r   info  s
   
zHTTP2Connection.infoc              	   C   s6   | j j}t| j}d| d|d| jj d| j d	S )N<z [z, r   z]>)	__class__r    r   r-   r5   r   r7   )rC   
class_namer)   r   r   r   __repr__  s   
zHTTP2Connection.__repr__c                 C   s   | S r{   r   r   r   r   r   	__enter__  s   zHTTP2Connection.__enter__exc_type	exc_value	tracebackc                 C   s   |    d S r{   )r   )rC   r   r   r   r   r   r   __exit__  s   zHTTP2Connection.__exit__r{   r   N)r   r&   )NNN)3r    r!   r"   r   r0   r,   H2Configurationr3   r   r   r|   OptionalfloatrD   r   r   re   rP   r   rX   rY   TupleListbytesrZ   Iteratorr   r   Eventr   r   r^   r   r   rs   r~   boolrM   r   r   r   r   r   r   r   r   TypeBaseExceptiontypesTracebackTyper   r   r   r   r   r&   '   s    
P!




	


r&   c                   @   sD   e Zd ZdedededdfddZdeje	 fdd	Z
dd
dZdS )r\   r1   r   rF   r   Nc                 C   s   || _ || _|| _d| _d S )NF)_connection_request
_stream_id_closed)rC   r1   r   rF   r   r   r   rD     s   
z"HTTP2ConnectionByteStream.__init__c              
   c   s    | j | jd}z,td| j | | jj| j | jdD ]}|V  qW d    W d S 1 s-w   Y  W d S  tyF } z|   |d }~ww )NrE   zhttp2.receive_response_body)r   r   r   r   r   r   r   )rC   ra   r   rd   r   r   r   __iter__  s   
&z"HTTP2ConnectionByteStream.__iter__c                 C   s^   | j s-d| _ d| ji}td| j| | jj| jd W d    d S 1 s&w   Y  d S d S )NTrF   rL   rG   )r   r   r   r   r   r^   )rC   ra   r   r   r   r     s   
"zHTTP2ConnectionByteStream.closer   )r    r!   r"   r&   r   r   rD   r|   r   r   r   r   r   r   r   r   r\     s    
r\   )!enumr   r   r|   	h2.configr0   h2.connection	h2.eventsh2.exceptionsh2.settings_exceptionsr   r   r   _modelsr   r   r   _synchronizationr	   r
   _tracer   backends.baser   
interfacesr   r   r   IntEnumr   r&   r\   r   r   r   r   <module>   s,    	   