o
    ;i	                     @   sx   d dl Z d dlmZmZ erd dlmZ d dlmZ ddlm	Z	 dee
 dd	fd
dZG dd dZe jdddZdS )    N)TYPE_CHECKINGOptional)ClientSession)Application   )on_shutdowntimeoutreturnr   c                 C   sP   ddl }ddl}ddlm}m}m} |j| d}||d}|||| ddS )a|  Create a new HTTP client session with standard, bundled TLS certificates.

    This is necessary to prevent client issues on some system where Python does
    not come pre-installed with specific TLS certificates that are necessary to
    connect to AWS S3 bucket URLs.

    Specifically: the error "unable to get local issuer certificate" when making
    an aiohttp request.
    r   N)r   ClientTimeoutTCPConnector)cafile)ssl)total)	connectorr   )r   certifiaiohttpr   r
   r   create_default_contextwhere)r   r   r   r   r
   r   ssl_contextr    r   K/home/ubuntu/.local/lib/python3.10/site-packages/modal/_utils/http_utils.py_http_client_with_tls   s   

r   c                   @   s:   e Zd ZU ded< dZeed< edd Zedd Zd	S )
ClientSessionRegistryr   _client_sessionF_client_session_activec                   C   s*   t jstd dt _dt _tt   t jS )N)r   T)r   r   r   r   r   close_sessionr   r   r   r   get_session'   s
   z!ClientSessionRegistry.get_sessionc                      s&   t jrt j I d H  dt _d S d S )NF)r   r   r   closer   r   r   r   r   /   s
   
z#ClientSessionRegistry.close_sessionN)	__name__
__module____qualname____annotations__r   boolstaticmethodr   r   r   r   r   r   r   #   s   
 
r   appr   c           	      C  s   dd l }ddlm}m} |  }|d | d }d| }|| }| I d H  |||d}| I d H  z|V  W | I d H  d S | I d H  w )Nr   )	AppRunnerSockSite) r   r   zhttp://127.0.0.1:)sock)	socketaiohttp.web_runnerr%   r&   bindgetsocknamesetupstartcleanup)	r$   r)   r%   r&   r(   porthostrunnersiter   r   r   run_temporary_http_server6   s   

"r4   )r$   r   )
contextlibtypingr   r   r   r   aiohttp.webr   async_utilsr   floatr   r   asynccontextmanagerr4   r   r   r   r   <module>   s   