o
    `۷i$                  	   @   s  d 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	 ddl
mZ ddlmZmZmZmZmZ ddlmZmZ ddlZddlm  mZ ddlm  mZ ddlmZ ddlm Z  dd	l!m"Z"m#Z# dd
l$m%Z%m&Z& ddlm'Z'm(Z( zej)Z)W n e*y   ej+Z)Y nw e,e-Z.e% Z/e% Z0e	dg dZ1e#j2e#j3hZ4ej5ej6eej7d fddZ8dede9fddZ:		ddedeee;  deee;  fddZ<defddZ=dS )zT
Optional utils module contains utility methods
that require optional dependencies.
    N)
namedtuple)
ModuleType)CallableListOptionalSetUnion)RequestResponse)env_bool) RAY_INTERNAL_DASHBOARD_NAMESPACE)aiohttphdrs)method_route_table_factoryrest_response)DashboardAgentModuleDashboardHeadModuleAiohttpCacheValue)data
expirationtaskFc                    sF   dksJ t    fdd}tr!}tj||S |S )Nr   c                    s`   r.t  dtjjf fdd}d d d}| j|7  _| j|7  _|S  S )Nreturnc                     s   | d }|j ddkr|  I d H S |jtv r|j n
|j| I d H f  }|d urK  |j rA|j	t

 krKtjjdi |jS  fdd}t|  }|| |d u rh|I d H S tjjdi |jS )Nnocache1c                    s   z|   }W n ty   ttjjt d}Y nw |jt	|j
|jd}t|t  |  <   t krB jdd |S )N)status_codemessage)statusheadersbodyF)last)result	Exceptionr   dashboard_utilsHTTPStatusCodeINTERNAL_ERROR	traceback
format_excr   dictr   r   _AiohttpCacheValuetimemove_to_endlenpopitem)r   responser   )cachekeymaxsizettl_seconds R/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/dashboard/optional_utils.py_update_cacheY   s&   

zNaiohttp_cache.<locals>._wrapper.<locals>._cache_handler.<locals>._update_cacher3   )querygetmethod_AIOHTTP_CACHE_NOBODY_METHODSpath_qsreadr+   r   doner   r*   r   webr
   r   create_taskadd_done_callback)argsreqvaluer5   r   )r/   handlerr1   r2   )r0   r4   _cache_handlerA   s$   




z7aiohttp_cache.<locals>._wrapper.<locals>._cache_handlerz[cache ttl=z, max_size=])	functoolswrapsr   r=   r
   __name____qualname__)rC   rD   suffixr/   enabler1   r2   )rC   r4   _wrapper>   s   3zaiohttp_cache.<locals>._wrapper)collectionsOrderedDictinspectiscoroutinefunctiondashboard_constsAIOHTTP_CACHE_TTL_SECONDS)r2   r1   rL   rM   target_funcr3   rK   r4   aiohttp_cache6   s   
>rU   rA   r   c                    s*    j dddpt fdddD S )a_  Best-effort detection if the request was made by a browser.

    Uses three heuristics:
        1) If the `User-Agent` header starts with 'Mozilla'. This heuristic is weak,
        but hard for a browser to bypass e.g., fetch/xhr and friends cannot alter the
        user agent, but requests made with an HTTP library can stumble into this if
        they choose to user a browser-like user agent. At the time of writing, all
        common browsers' user agents start with 'Mozilla'.
        2) If any of the `Sec-Fetch-*` headers are present.
        3) If any of the various CORS headers are present
    z
User-Agent Mozillac                 3   s    | ]}| j v V  qd S )N)r   ).0hrA   r3   r4   	<genexpr>   s
    
z%is_browser_request.<locals>.<genexpr>)RefererOriginzSec-Fetch-ModezSec-Fetch-DestzSec-Fetch-SitezSec-Fetch-UserzAccess-Control-Request-MethodzAccess-Control-Request-Headers)r   r7   
startswithanyrZ   r3   rZ   r4   is_browser_request   s    r`   aiohttp_moduleallowed_methodsallowed_pathsc                    s&   pt   jj fdd}|S )a  Create middleware that restricts browser access to specified HTTP methods.

    This middleware blocks browser requests to prevent DNS rebinding and CSRF
    attacks. Only explicitly allowed methods are permitted from browsers.

    Args:
        aiohttp_module: The aiohttp module to use
        allowed_methods: Set of HTTP methods browsers are allowed to use.
        allowed_paths: List of paths that bypass the method check entirely,
            allowing any method from browsers.

    Returns:
        An aiohttp middleware function
    c                    sz   t | s|| I d H S r| jv r|| I d H S s$ jjdddS | jvr6 jjdd| j ddS || I d H S )Ni  zBrowser requests not allowed.)r   texti  'z)' method not allowed for browser traffic.)r`   pathr=   r
   r8   )requestrC   ra   rb   rc   r3   r4   browser_request_middleware   s   
zBget_browser_request_middleware.<locals>.browser_request_middleware)setr=   
middleware)ra   rb   rc   ri   r3   rh   r4   get_browser_request_middleware   s   
rl   c                  C   s   dt dt fdd} | S )zDDecorator to be used on methods that require being connected to Ray.fr   c                    s(   t  dtttf f fdd}|S )Nselfc              
      s   zFt  s9z| j}td|  ttjtj	d< t j
|ddtdd W n ty8 } zt   |d d }~ww  | g|R i |I d H W S  tyl } ztd|  tt tjjjdW  Y d }~S d }~ww )NzConnecting to ray with address=&RAY_gcs_server_request_timeout_secondsFT)addresslog_to_driverconfigure_logging	namespace_skip_env_hookzUnexpected error in handler: )rd   r   )rayis_initializedgcs_addressloggerinfostrrR   GCS_RPC_TIMEOUT_SECONDSosenvironinitr   r"   shutdown	exceptionr
   r&   r'   r   r=   HTTPInternalServerErrorr   )rn   r@   kwargsrp   erm   r3   r4   	decorator   s<   

zKinit_ray_and_catch_exceptions.<locals>.decorator_factory.<locals>.decorator)rF   rG   r   r   r   )rm   r   r3   r   r4   decorator_factory   s
   
 z8init_ray_and_catch_exceptions.<locals>.decorator_factory)r   )r   r3   r3   r4   init_ray_and_catch_exceptions   s   $r   )NN)>__doc__asynciorN   rF   rP   loggingr|   r*   r&   r   typesr   typingr   r   r   r   r   aiohttp.webr	   r
   ru   ray.dashboard.consts	dashboardconstsrR   ray.dashboard.utilsutilsr#   ray._private.ray_constantsr   ray._rayletr   ray.dashboard.optional_depsr   r   ray.dashboard.routesr   r   r   r   r>   AttributeErrorensure_future	getLoggerrH   rx   DashboardHeadRouteTableDashboardAgentRouteTabler)   METH_GETMETH_DELETEr9   rS   AIOHTTP_CACHE_MAX_SIZE%AIOHTTP_CACHE_DISABLE_ENVIRONMENT_KEYrU   boolr`   rz   rl   r   r3   r3   r3   r4   <module>   s\    



N"


0