o
    ۷i                  	   @   s.  d dl Z d dlZd dlmZ d dlZd dl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 d dlZdd Ze Zdedefd	d
Zdedee defddZddefddZe dedee fddZdedefddZG dd dejZ		ddedee dB dejed B dB dej fddZ!dS )     N)Path)	lru_cachewraps)Any	AwaitableCallable	CoroutineLiteralTTuplec                  C   sH   t tjd } t| d}|  W  d    S 1 sw   Y  d S )Nzversion.txtr)r   __file__parentopenreadstrip)version_filef r   H/home/ubuntu/vllm_env/lib/python3.10/site-packages/safehttpx/__init__.pyget_version   s   
$r   ipreturnc                 C   sB   zt | }|jp|jp|jp|jp|j W S  ty    Y dS w )NF)	ipaddress
ip_address
is_privateis_loopbackis_link_localis_multicastis_reserved
ValueError)r   ip_objr   r   r   is_public_ip   s   
r"   hostnamedomain_whitelistc                 C   sr   | r|sdS |   } |D ]*}|sq|  }|dr/|dd  }| |ks+| d| r. dS q| |kr6 dS qdS )NFz*.   .T)lower
startswithendswith)r#   r$   domainsuffixr   r   r   matches_domain_whitelist    s    
r,      maxsizec                    s8   dt dttttf f dt dtt f f fdd}|S )N
async_func.r   c              	      s4   t dt dtdtdtt f fdd}|S )Nr.   argskwargsr   c                     s   t  | i |S N)asynciocreate_task)r1   r2   r/   r   r   wrapper5   s   z3lru_cache_async.<locals>.decorator.<locals>.wrapper)r   r   r   r   r
   )r/   r7   r0   r6   r   	decorator2   s   "z"lru_cache_async.<locals>.decorator)r   r   r   r
   r   )r.   r8   r   r0   r   lru_cache_async1   s   
r9   c              	      s   t  4 I d H W}z@|d|  dI d H }|d|  dI d H }g }| | fD ]}|dd |dg D  q-|W W  d   I d H  S  ty_   g  Y W  d   I d H  S w 1 I d H sfw   Y  d S )Nz https://dns.google/resolve?name=z&type=Az
&type=AAAAc                 S   s   g | ]}|d  qS )datar   ).0answerr   r   r   
<listcomp>L   s    z1async_resolve_hostname_google.<locals>.<listcomp>Answer)httpxAsyncClientgetjsonextend	Exception)r#   clientresponse_v4response_v6ipsresponser   r   r   async_resolve_hostname_google?   s&   



rJ   c              
      s   zt  }|| d I d H }W n tjy( } ztd|  d| |d }~ww |D ]\}}}}}|d }|tjtjfv rFt|rF|  S q+t	| I d H D ]
}t|rX|  S qNtd|  d)NzUnable to resolve hostname z: r   z	Hostname z failed validation)
r4   get_event_loopgetaddrinfosocketgaierrorr    AF_INETAF_INET6r"   rJ   )r#   loopaddrinfoefamily_sockaddrr   r   r   r   async_validate_urlR   s$   rW   c                       sH   e Zd Zdef fddZdejdeee	e	ej
f f fddZ  ZS )AsyncSecureTransportverified_ipc                    s   || _ t   d S r3   )rY   super__init__)selfrY   	__class__r   r   r[   f   s   zAsyncSecureTransport.__init__requestr   c                    sH   |j }|j}|j| jd}||_ ||jd< d|i|_t |I d H S )N)hostHostsni_hostname)urlr`   	copy_withrY   headers
extensionsrZ   handle_async_request)r\   r_   original_urloriginal_hostnew_urlr]   r   r   rg   j   s   

z)AsyncSecureTransport.handle_async_request)__name__
__module____qualname__strr[   r?   Requestr   intbytesHeadersrg   __classcell__r   r   r]   r   rX   e   s    rX   rc   
_transportFc           	   	      s   t | }|j}|std|  d|du rg }|r|}n|du s't||r*d}nt|I dH }t|}t j|d4 I dH }|j| fddi|I dH W  d  I dH  S 1 I dH s^w   Y  dS )a<  
    This is the main function that should be used to make async HTTP GET requests.
    It will automatically use a secure transport for non-whitelisted domains, unless
    a proxy is set in the environment variables (HTTP_PROXY, HTTPS_PROXY, http_proxy, https_proxy).

    Parameters:
    - url (str): The URL to make a GET request to.
    - domain_whitelist (list[str] | None): A list of domains to whitelist, which will not use a secure transport. Supports wildcard subdomains with "*.domain.com" format (asterisk must be at the beginning).
    - _transport (httpx.AsyncBaseTransport | Literal[False] | None): A custom transport to use for the request. Takes precedence over domain_whitelist. Set to False to use no transport.
    - **kwargs: Additional keyword arguments to pass to the httpx.AsyncClient.get() function.
    zURL z does not have a valid hostnameNF)	transportfollow_redirects)	r?   URLr`   r    r,   rW   rX   r@   rA   )	rc   r$   rt   r2   
parsed_urlr#   ru   rY   rE   r   r   r   rA   v   s    
0rA   )r-   )NN)"r4   r   pathlibr   rM   os	functoolsr   r   typingr   r   r   r   r	   r
   r   r?   r   __version__rn   boolr"   listr,   rp   r9   rJ   rW   AsyncHTTPTransportrX   AsyncBaseTransportResponserA   r   r   r   r   <module>   s8    $
