o
    h                     @   s   d dl Z d dlZddlmZmZmZ ddlmZmZ ddl	m
Z
mZ ddlmZ e jejejejejejejf Ze jg e jd f ZeeZG dd dZdS )	    N   )BaseTCPStreamConcurrencyBackendTimeoutFlag)TimeoutConfigTimeoutTypes)AsyncRequestAsyncResponse)
get_loggerc                   @   s2  e Zd ZdZ	d#dededeje fddZ		d#de
d	ed
efddZd$ddZ	d#de
d	ed
dfddZ	d#deje d	ed
dfddZd#ded	ed
dfddZ	d#d	ed
ejeeejejeef  f fddZ	d#d	ed
eje fddZd#d	ed
efddZd$ddZed
efdd Z d
efd!d"Z!dS )%HTTP11Connectioni   Nstreambackend
on_releasec                 C   s.   || _ || _|| _tjtjd| _t | _d S )N)Zour_role)	r   r   r   h11
ConnectionZCLIENT	h11_stater   timeout_flag)selfr   r   r    r   ]/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/httpx/dispatch/http11.py__init__    s
   zHTTP11Connection.__init__requesttimeoutreturnc           	   	      s   |d u rd nt |}| ||I d H  | j| |g}}| jj|g|R  4 I d H  | |I d H \}}}W d   I d H  n1 I d H sHw   Y  | |}t||||| j	|dS )N)status_codehttp_versionheaderscontentZon_closer   )
r   _send_request_send_request_datar   r   Zbackground_manager_receive_response_receive_response_datar	   response_closed)	r   r   r   taskargsr   r   r   r   r   r   r   send,   s    (
zHTTP11Connection.sendc                    sT   t  }ztd| | j| W n
 t jy   Y nw | j I d H  d S )Nzsend_event event=)	r   ConnectionClosedloggerdebugr   r%   ZLocalProtocolErrorr   close)r   eventr   r   r   r)   A   s   zHTTP11Connection.closec                    sn   t d|jd|jjd|j |jd}|jjd}|jj}tj	|||d}| 
||I dH  dS )zK
        Send the request method, URL, and headers to the network.
        zsend_headers method=z target=z	 headers=ascii)methodtargetr   N)r'   r(   r,   url	full_pathr   encoderawr   Request_send_event)r   r   r   r,   r-   r   r*   r   r   r   r   K   s   
zHTTP11Connection._send_requestdatac                    s   zRz3|2 z 3 dH W }t dt| d tj|d}| ||I dH  q6 t }| ||I dH  W n tyC   | j	  Y n	w W | j
  dS W | j
  dS | j
  w )z7
        Send the request body to the network.
        Nzsend_data data=Data(< bytes>))r4   )r'   r(   lenr   Datar3   EndOfMessageOSErrorr   Zsend_failedr   set_read_timeouts)r   r4   r   chunkr*   r   r   r   r   ]   s    z#HTTP11Connection._send_request_datar*   c                    s&   | j |}| j||I dH  dS )zs
        Send a single `h11` event to the network, waiting for the data to
        drain before returning.
        N)r   r%   r   write)r   r*   r   Zbytes_to_sendr   r   r   r3   v   s   zHTTP11Connection._send_eventc                    s`   	 |  |I dH }| j  t|tjrqt|tjsJ 	 d|jjddd }||j	|j
fS )zH
        Read the response status and headers from the network.
        TNzHTTP/%szlatin-1ignore)errors)_receive_eventr   r:   
isinstancer   InformationalResponseResponser   decoder   r   )r   r   r*   r   r   r   r   r    ~   s   
z"HTTP11Connection._receive_responsec                 C  sN   	 |  |I dH }t|tjrt|jV  nt|tjs$|tju s$J dS q)z:
        Read the response data from the network.
        TN)r?   r@   r   r7   bytesr4   r8   ZPAUSED)r   r   r*   r   r   r   r!      s   z'HTTP11Connection._receive_response_datac                    s   	 | j  }t|tjrtdt|j d ntd| |tj	u rJz| j
j| j|| jdI dH }W n tyB   d}Y nw | j | n
|tj	usQJ 	 |S q)zZ
        Read a single `h11` event, reading more data from the network if needed.
        Tzreceive_event event=Data(<r5   zreceive_event event=)flagN    )r   Z
next_eventr@   r   r7   r'   r(   r6   r4   Z	NEED_DATAr   readREAD_NUM_BYTESr   r9   Zreceive_data)r   r   r*   r4   r   r   r   r?      s&   


zHTTP11Connection._receive_eventc                    s   t d| jjd| jj  | jjtju r)| jjtju r)| j  | j	  n| 
 I d H  | jd ur>|  I d H  d S d S )Nzresponse_closed our_state=z their_state=)r'   r(   r   	our_stateZtheir_stater   ZDONEZstart_next_cycler   Zset_write_timeoutsr)   r   r   r   r   r   r"      s    

z HTTP11Connection.response_closedc                 C   s   | j jtjtjfv S N)r   rI   r   ZCLOSEDERRORrJ   r   r   r   	is_closed   s   zHTTP11Connection.is_closedc                 C   s
   | j  S rK   )r   is_connection_droppedrJ   r   r   r   rN      s   
z&HTTP11Connection.is_connection_droppedrK   )r   N)"__name__
__module____qualname__rH   r   r   typingOptionalOnReleaseCallbackr   r   r   r	   r%   r)   r   r   AsyncIteratorrD   r   H11Eventr3   TuplestrintListr    r!   r?   r"   propertyboolrM   rN   r   r   r   r   r      sj    




	


r   )rR   r   Zconcurrency.baser   r   r   configr   r   modelsr   r	   utilsr
   Unionr2   rB   rA   r7   r8   r&   rV   Callable	AwaitablerT   rO   r'   r   r   r   r   r   <module>   s$    