o
    Zha$                     @  s   d dl mZ d dlZd dlmZmZmZmZ d dlZ	d dl
mZmZ d dlmZmZmZ er6d dlmZmZ dd	d
ZdddZG dd de	jjZG dd de	jjZG dd de	jjZG dd de	jjZG dd deZg dZG dd deZdS )    )annotationsN)TYPE_CHECKINGAnyCallablecast)config	file_util)allowlisted_origins emit_endpoint_deprecation_noticeis_xsrf_enabled)	AwaitableSequencereturnboolc                   C  s   t d p
t dS )a0  True if cross-origin requests from any origin are allowed.

    We only allow ALL cross-origin requests when CORS protection has been
    disabled with server.enableCORS=False or if using the Node server in dev
    mode. When in dev mode, we have a dev and prod port, which count as two
    origins.
    zserver.enableCORSglobal.developmentMode)r   
get_option r   r   \/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/web/server/routes.pyallow_all_cross_origin_requests!   s   	r   originr   c                 C  s   t | tsdS | t v S )NF)
isinstancestrr	   )r   r   r   r   is_allowed_origin/   s   

r   c                      sJ   e Zd Z		dd fddZdddZd fddZd fddZ  ZS )StaticFileHandlerNr   pathr   default_filename
str | Nonereserved_pathsSequence[str]r   Nonec                   s   || _ t || d S N)_reserved_pathssuper
initialize)selfr   r   r   	__class__r   r   r#   6   s   zStaticFileHandler.initializec                 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.htmlCache-Controlno-cachepublicN)lenendswith
set_header)r$   r   is_index_urlr   r   r   set_extra_headers@   s   z#StaticFileHandler.set_extra_headersrootabsolute_pathc              
     s   zt  ||W S  tjjyX } zB|jdkrS| j tjjdkr( 	tjjd t
 fdd| jD r5 | | jp;d| _| | j| j}t  ||W  Y d }~S  d }~ww )N  /c                 3  s    | ]}  |V  qd S r    )r+   ).0xurl_pathr   r   	<genexpr>X   s    z;StaticFileHandler.validate_absolute_path.<locals>.<genexpr>
index.html)r"   validate_absolute_pathtornadoweb	HTTPErrorstatus_coder   ossepreplaceanyr!   parse_url_pathr   get_absolute_pathr/   )r$   r/   r0   er%   r5   r   r9   L   s   
z(StaticFileHandler.validate_absolute_pathr=   intkwargsr   c                   s@   |dkrt jt d}| | d S t j|fi | d S )Nr1   r8   )r>   r   joinr   get_static_dirrenderr"   write_error)r$   r=   rF   
index_filer%   r   r   rJ   a   s   zStaticFileHandler.write_error)Nr   )r   r   r   r   r   r   r   r   )r   r   r   r   )r/   r   r0   r   r   r   )r=   rE   rF   r   r   r   )__name__
__module____qualname__r#   r.   r9   rJ   __classcell__r   r   r%   r   r   5   s    

r   c                   @     e Zd ZejjdddZdS )AddSlashHandlerr   r   c                 C     d S r    r   r$   r   r   r   getj      zAddSlashHandler.getNr   r   )rL   rM   rN   r:   r;   addslashrT   r   r   r   r   rQ   i       rQ   c                   @  rP   )RemoveSlashHandlerr   r   c                 C  rR   r    r   rS   r   r   r   rT   p   rU   zRemoveSlashHandler.getNrV   )rL   rM   rN   r:   r;   removeslashrT   r   r   r   r   rY   o   rX   rY   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
_SpecialRequestHandlerz2Superclass for "special" endpoints, like /healthz.r   r   c                 C  sR   |  dd t r|  dd d S t| jjd }r'|  dtd| d S d S )Nr'   r(   zAccess-Control-Allow-Origin*Originr   )r,   r   r   requestheadersrT   r   )r$   r   r   r   r   set_default_headersx   s   z*_SpecialRequestHandler.set_default_headersc                 C  s   |  d |   dS )a  /OPTIONS handler for preflight CORS checks.

        When a browser is making a CORS request, it may sometimes first
        send an OPTIONS request, to check whether the server understands the
        CORS protocol. This is optional, and doesn't happen for every request
        or in every browser. If an OPTIONS request does get sent, and is not
        then handled by the server, the browser will fail the underlying
        request.

        The proper way to handle this is to send a 204 response ("no content")
        with the CORS headers attached. (These headers are automatically added
        to every outgoing response, including OPTIONS responses,
        via set_default_headers().)

        See https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
           N)
set_statusfinishrS   r   r   r   options   s   
z_SpecialRequestHandler.optionsNrV   )rL   rM   rN   __doc__r`   rd   r   r   r   r   r[   u   s    
r[   c                   @  s4   e Zd ZdddZdddZdd	d
ZdddZdS )HealthHandlercallback)Callable[[], Awaitable[tuple[bool, str]]]r   r   c                 C  s
   || _ dS )zInitialize the handler.

        Parameters
        ----------
        callback : callable
            A function that returns True if the server is healthy

        N)	_callback)r$   rg   r   r   r   r#      s   
	zHealthHandler.initializec                      |   I d H  d S r    handle_requestrS   r   r   r   rT         zHealthHandler.getc                   rj   r    rk   rS   r   r   r   head   rm   zHealthHandler.headc                   s   | j jrd| j jvrd| j jv rdnd}t| |d |  I d H \}}|rN| | | d t rL| jdi }| j	| jdd	| j
fi | d S d S | d
 | | d S )Nz_stcore/zscript-health-checkz/_stcore/script-health-checkz/_stcore/health)new_path   xsrf_cookie_kwargsxsrf_cookie_name_streamlit_xsrfi  )r^   urir
   ri   writerb   r   settingsrT   
set_cookie
xsrf_token)r$   ro   okmsgcookie_kwargsr   r   r   rl      s,   




zHealthHandler.handle_requestN)rg   rh   r   r   rV   )rL   rM   rN   r#   rT   rn   rl   r   r   r   r   rf      s
    


rf   )zhttps://devel.streamlit.testzhttps://*.streamlit.apptestzhttps://*.streamlitapp.testzhttps://*.streamlitapp.comzhttps://share.streamlit.iozhttps://share-demo.streamlit.iozhttps://share-head.streamlit.ioz"https://share-staging.streamlit.iozhttps://*.demo.streamlit.runzhttps://*.head.streamlit.runzhttps://*.staging.streamlit.runzhttps://*.streamlit.runzhttps://*.demo.streamlit.appzhttps://*.head.streamlit.appzhttps://*.staging.streamlit.appzhttps://*.streamlit.appc                   @  s    e Zd ZdddZdddZdS )	HostConfigHandlerr   r   c                 C  s6   t  | _tdrd| jvr| jd d S d S d S )Nr   zhttp://localhost) _DEFAULT_ALLOWED_MESSAGE_ORIGINScopy_allowed_originsr   r   appendrS   r   r   r   r#      s   

zHostConfigHandler.initializec              
     s,   |  | jdddddd d | d d S )NF )allowedOriginsuseExternalAuthTokenenableCustomParentMessagesenforceDownloadInNewTab
metricsUrlblockErrorDialogsresourceCrossOriginModerp   )ru   r   rb   rS   r   r   r   rT      s   zHostConfigHandler.getNrV   )rL   rM   rN   r#   rT   r   r   r   r   r|      s    
r|   )r   r   )r   r   r   r   )
__future__r   r>   typingr   r   r   r   tornado.webr:   	streamlitr   r    streamlit.web.server.server_utilr	   r
   r   collections.abcr   r   r   r   r;   r   RequestHandlerrQ   rY   r[   rf   r}   r|   r   r   r   r   <module>   s"   

45