o
    j                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ G dd	 d	ZdS )
    N)ListTupleAnyUnionDict)	UniClient)	UniTargetUniProto)
Packetizer)HTTPResponse)
HTTPTargetc                   @   s   e Zd Zd!defddZdd Zdd Zed	d
 Zedd Z	dd Z
dd Zdd Zd"deeeef  dedefddZdd Zdg ddfdededeeeef  dedef
dd ZdS )#HTTPClientTransport
keep-alivetargetc                 C   s.   || _ d | _d | _d | _|| _|| _d| _d S NF)r   connection_closed_evt
connectionhttpconnrequest_connection_typeresponse_connection_type*_HTTPClientTransport__disconnect_requested)selfr   r    r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asysocks/unicomm/protocol/client/http/transport.py__init__   s   
zHTTPClientTransport.__init__c              
   C  s   z@| j  du r8| j }|tju r!| j I d H }| j| |tj	u r.| 
 I d H  n
|V  | j  du s	|  I d H  W d S  ty_ } z| 
 I d H  t  W Y d }~d S d }~ww r   )r   is_setr   
next_eventh11	NEED_DATAr   read_onereceive_dataConnectionClosed
disconnectdisconnect_if_needed	Exception	traceback	print_exc)r   eventdataer   r   r   __next_event   s$   


	z HTTPClientTransport.__next_eventc                    sH   | j |}t|tju rd| _d S |dkrd S | j|I d H  d S )NT    )r   sendtyper   r!   r   r   write)r   r'   r(   r   r   r   __send)   s   zHTTPClientTransport.__sendc                 C   s   | j | j  kodkS   S )Nr   )r   r   r   r   r   r   supports_keepalive5   s   z&HTTPClientTransport.supports_keepalivec                 C   s   | j du o| j du S )NTF)r1   r   r   r0   r   r   r   	can_reuse9   s   zHTTPClientTransport.can_reusec                    sB   t jt jd| _t | _t }t| j	|}|
 I d H | _d S )N)our_role)r   
ConnectionCLIENTr   asyncioEventr   r
   r   r   connectr   )r   
packetizerclientr   r   r   r8   =   s   
zHTTPClientTransport.connectc                    s"   | j du r|  I d H  d S d S r   )r2   r"   r0   r   r   r   r#   D   s   
z(HTTPClientTransport.disconnect_if_neededc                    s4   d| _ | j  | jd ur| j I d H  d S d S )NT)r   r   setr   closer0   r   r   r   r"   H   s   

zHTTPClientTransport.disconnectr+   Fheadersr(   need_lengthc                 C   s   d}d}d}|D ])}|d   dkrd}q|d   dkr'd}|d   | _q|d   dkr1d}q|du r@|d| j f |du rL|d	| jf |du re|du re|d u rZd
}|dtt|f |S )NFr   hostTr      zcontent-lengthHostr4   r+   zContent-Length)lowerr   appendr   get_hostname_or_ipstrlen)r   r=   r(   r>   has_hosthas_connection
has_lengthentryr   r   r   __correct_headersO   s*   z%HTTPClientTransport.__correct_headersc                    sn   |   2 z.3 d H W }t|tju rt|| j | |  S t|tju r(tdt|tju r3tdq6 d S )Nz.Response ended without server sending headers!z!Server terminated the connection!)	 _HTTPClientTransport__next_eventr-   r   Responser   from_h11_headerEndOfMessager$   r!   )r   fullurlr'   r   r   r   __read_headere   s   z!HTTPClientTransport.__read_header/Nreq_typec                    s   | j jtj | j jtj   krtjkrn n| j   | |||}tj|||d}| 	|I d H  |d urNt
|tu rB| }| 	tj|dI d H  | 	t I d H  | |I d H }	|	jD ]}
|
 dkru|	j|
 d  | _qc|	S )N)methodr   r=   )r(   r   r   )r   statesr   r5   SERVERDONEstart_next_cycle%_HTTPClientTransport__correct_headersRequest_HTTPClientTransport__sendr-   rE   encodeDatarO   !_HTTPClientTransport__read_headerr=   rB   r   )r   rP   rS   r   r=   r(   r>   request_headerrequest_eventresponseheaderr   r   r   requestn   s*   .

zHTTPClientTransport.request)r   )r+   F)__name__
__module____qualname__r   r   rL   r[   propertyr1   r2   r8   r#   r"   r   r   rE   bytesboolrY   r^   rc   r   r   r   r   r      s    	

$8	r   )r   r6   r%   typingr   r   r   r   r   asysocks.unicomm.clientr   asysocks.unicomm.common.targetr   r	   #asysocks.unicomm.common.packetizersr
   6asysocks.unicomm.protocol.client.http.commons.messagesr   4asysocks.unicomm.protocol.client.http.commons.targetr   r   r   r   r   r   <module>   s    