o
    h>                     @  s   U d dl mZ d dlZd dlZd dlmZmZ d dlZd dl	Z
d dlmZ er-d dlmZ eeZded< G dd	 d	ejjZdS )
    )annotationsN)TYPE_CHECKINGFinal)
get_logger)BaseComponentRegistryr   _LOGGERc                   @  sZ   e Zd ZdddZdd	d
ZdddZdddZdddZedddZ	edddZ
dS )ComponentRequestHandlerregistryr   c                 C  s
   || _ d S )N)	_registry)selfr	    r   b/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/web/server/component_request_handler.py
initialize!   s   
z"ComponentRequestHandler.initializepathstrreturnNonec           
   
   C  sJ  | d}|d }| j|}|d u r| d | d d S tj|}d|dd  }tj	tj||}tj
||g|krO| d | d d S zt|d}| }W d    n1 sdw   Y  W n% ty }	 ztjd	||	d
 | d | d W Y d }	~	d S d }	~	ww | | | d| | | | d S )N/r   z	not foundi     	forbiddeni  rbz*ComponentRequestHandler: GET %s read error)exc_infoz
read errorzContent-Type)splitr
   get_component_pathwrite
set_statusosr   realpathjoinnormpath
commonpathopenreadOSErrorr   error
set_headerget_content_typeset_extra_headers)
r   r   partscomponent_namecomponent_rootfilenameabspathfilecontentser   r   r   get$   s>   








zComponentRequestHandler.getc                 C  s:   t |dk}|s|dr| dd dS | dd dS )zDisable cache for HTML files.

        Other assets like JS and CSS are suffixed with their hash, so they can
        be cached indefinitely.
        r   z.htmlzCache-Controlzno-cachepublicN)lenendswithr%   )r   r   is_index_urlr   r   r   r'   G   s   z)ComponentRequestHandler.set_extra_headersc                 C  s"   t jjj r| dd d S d S )NzAccess-Control-Allow-Origin*)	streamlitwebserverroutesallow_cross_origin_requestsr%   r   r   r   r   set_default_headersT   s   z+ComponentRequestHandler.set_default_headersc                 C  s   |  d |   dS )z+/OPTIONS handler for preflight CORS checks.   N)r   finishr;   r   r   r   optionsX   s   
zComponentRequestHandler.optionsr,   c                 C  s6   t | \}}|dkrdS |durdS |dur|S dS )zuReturns the ``Content-Type`` header to be used for this request.
        From tornado.web.StaticFileHandler.
        gzipzapplication/gzipNzapplication/octet-stream)	mimetypes
guess_type)r,   	mime_typeencodingr   r   r   r&   ]   s   z(ComponentRequestHandler.get_content_typefile_idc                 C  s
   d|  S )z6Return the URL for a component file with the given ID.zcomponents/r   )rE   r   r   r   get_urlq   s   
zComponentRequestHandler.get_urlN)r	   r   )r   r   r   r   )r   r   )r,   r   r   r   )rE   r   r   r   )__name__
__module____qualname__r   r0   r'   r<   r?   staticmethodr&   rF   r   r   r   r   r       s    


#

r   )
__future__r   rA   r   typingr   r   tornado.webtornadostreamlit.web.server.routesr6   streamlit.loggerr   2streamlit.components.types.base_component_registryr   rG   r   __annotations__r7   RequestHandlerr   r   r   r   r   <module>   s   