
    5iy                    2   U d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	lmZ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m Z  ddl!m"Z"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ er0ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddlm8Z8 ddl9m:Z:  ee;      Z<de=d<   dZ>de=d<   d Z?de=d!<   e> d"Z@de=d#<   d$ZAde=d%<   e> d&ZBde=d'<   e> d(ZCde=d)<   e> d*ZDde=d+<   e> d,ZEde=d-<   e? d.ZFde=d/<   e@ d0ZGde=d1<   eA d2ZHde=d3<   e> d4ZIde=d5<   d6ZJde=d7<   dEdFd8ZKdGd9ZLdGd:ZM	 	 	 	 	 	 	 	 	 	 dHd;ZNdId<ZO	 	 	 	 	 	 dId=ZPdId>ZQdJd?ZR	 	 	 	 	 	 dKd@ZS	 	 	 	 	 	 	 	 dLdAZT	 	 	 	 	 	 dMdBZU	 	 	 	 	 	 dNdCZV	 	 	 	 	 	 dOdDZWy)Pz(Route handlers for the Starlette server.    )annotationsN)Path)TYPE_CHECKINGFinal)quote)config	file_util)
get_logger)MediaFileKindMediaFileStorageError)get_extension_for_mimetype)UploadedFileRec)MAX_APP_STATIC_FILE_SIZESAFE_APP_STATIC_FILE_EXTENSIONS)build_safe_abspathguess_content_type) _DEFAULT_ALLOWED_MESSAGE_ORIGINSallow_all_cross_origin_requestsis_allowed_origin)get_urlis_xsrf_enabled)starlette_app_utils)validate_xsrf_token)XSRF_COOKIE_NAME)StatsRequestHandler)RequestResponse)	BaseRoute)BaseComponentRegistry)BidiComponentManager)Runtime)MemoryMediaFileStorage)MemoryUploadedFileManagerr   _LOGGER_stcoreBASE_ROUTE_COREmediaBASE_ROUTE_MEDIAz/upload_fileBASE_ROUTE_UPLOAD_FILE	componentBASE_ROUTE_COMPONENTz/health_ROUTE_HEALTHz/script-health-check_ROUTE_SCRIPT_HEALTHz/metrics_ROUTE_METRICSz/host-config_ROUTE_HOST_CONFIGz/{file_id:path}_ROUTE_MEDIAz/{session_id}/{file_id}_ROUTE_UPLOAD_FILEz/{path:path}_ROUTE_COMPONENTS_V1z/bidi-components/{path:path}_ROUTE_COMPONENTS_V2zapp/static/{path:path}_ROUTE_APP_STATICc                Z    ddl m} ||nt        j                  d      xs d} |||       S )a  Prepend the base URL path to a route path.

    Parameters
    ----------
    path
        The route path to prepend the base URL to (e.g., "_stcore/health").
    base_url
        Optional explicit base URL. If None, uses the configured server.baseUrlPath.
        If an empty string, no base URL is prepended.

    Returns
    -------
    str
        The full route path with base URL prepended (e.g., "/myapp/_stcore/health").
    r   )make_url_pathzserver.baseUrlPath )streamlit.url_utilr7   r   
get_option)pathbase_urlr7   bases       y/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/streamlit/web/server/starlette/starlette_routes.py
_with_baser?   _   s;      1 (f.?.?@T.U	 	 t$$    c                   K   t               rd|j                  d<   y| j                  j                  d      }|rt        |      r||j                  d<   yyyw)a  Set CORS headers on a response based on configuration.

    Configures the Access-Control-Allow-Origin header according to the following rules:
    - If CORS is disabled or in development mode: allows all origins ("*")
    - Otherwise: only allows origins that match the configured allowlist

    Parameters
    ----------
    request
        The incoming Starlette request (used to read the Origin header).
    response
        The outgoing Starlette response to set headers on.
    *Access-Control-Allow-OriginNOrigin)r   headersgetr   )requestresponseorigins      r>   _set_cors_headersrJ   w   sY      '(:=67__  *F#F+:@67 ,vs   AAc           
        t               sy| j                  j                  t              }d}d}|rt	        j
                  |      \  }}t	        j                  ||      }t        |t        |t        t        j                  d                   y)a  Ensure that the XSRF cookie is set on the response.

    This function manages XSRF (Cross-Site Request Forgery) token generation
    and cookie setting to maintain compatibility with Tornado's implementation.
    If an existing valid XSRF cookie is present, its token bytes and timestamp
    are preserved. Otherwise, a new token is generated.

    The cookie is only set if XSRF protection is enabled in the configuration.
    The Secure flag is added when SSL is configured.

    Note: The XSRF cookie intentionally does NOT have the HttpOnly flag. This
    is required for the double-submit cookie pattern: JavaScript reads the
    cookie value and includes it in the X-Xsrftoken request header, which the
    server then compares against the cookie value to validate requests.

    Parameters
    ----------
    request
        The incoming Starlette request (used to read existing XSRF cookie).
    response
        The outgoing Starlette response to set the cookie on.
    Nzserver.sslCertFile)secure)r   cookiesrF   r   r   decode_xsrf_token_stringgenerate_xsrf_token_string_set_unquoted_cookieboolr   r:   )rG   rH   
raw_cookietoken_bytes	timestampcookie_values         r>   _ensure_xsrf_cookierV      s    .  $$%56J $K I!4!M!M"
Y
 'AAYL F%%&:;<	r@   c                  dj                  | d| ddg|rdgng       }| dj                  d      }| j                  D cg c]I  \  }}|j                         dk(  r-|j                         j	                  |j                               s||fK }}}|j                  d|j                  d      f       || _        yc c}}w )	ai  Set a cookie without URL-encoding or quoting the value.

    Starlette's standard set_cookie() method URL-encodes special characters
    (like `|`) in cookie values. This function bypasses that encoding to
    maintain compatibility with Tornado's cookie format, which is required
    for XSRF tokens that use the format "2|mask|token|timestamp".

    If a cookie with the same name already exists, it is replaced.

    Cookie flags set:
    - Path=/: Available to all paths
    - SameSite=Lax: Protects against CSRF while allowing top-level navigations
    - Secure (conditional): Added when SSL is configured

    HttpOnly is intentionally NOT set for XSRF cookies because JavaScript must
    read the cookie value to include it in request headers (double-submit pattern).
    This matches Tornado's behavior.

    Parameters
    ----------
    response
        The Starlette response to set the cookie on.
    cookie_name
        The name of the cookie.
    cookie_value
        The raw cookie value (will not be URL-encoded or quoted).
    secure
        Whether to add the Secure flag (should be True when using HTTPS).
    z; =zPath=/zSameSite=LaxSecurezlatin-1s
   set-cookieN)joinencoderaw_headerslower
startswithappend)	rH   cookie_namerU   rL   header_value
key_prefixnamevaluefiltered_headerss	            r>   rP   rP      s    J 99m1\N+	
 #xj		
L  =")))4J $//3D%JJLM)(()9)9);<	 
u3 3 ]L,?,?	,JKL+H3s   AB?c                     ddl mm ddlm} d
 fd}dfd} |t        t        |      |ddg       |t        t        |      |d	g      gS )aJ  Create health check route handlers for /_stcore/health.

    The health endpoint returns 200 OK when the runtime is ready to accept
    browser connections, or 503 Service Unavailable otherwise. This is used
    by load balancers and orchestration systems to determine service readiness.

    Parameters
    ----------
    runtime
        The Streamlit runtime instance to check health status.
    base_url
        Optional base URL path prefix for the routes.

    Returns
    -------
    list[BaseRoute]
        List of Starlette Route objects for GET, HEAD, and OPTIONS methods.
    r   PlainTextResponser   Routec                   K   j                    d {   \  }}|rdnd} ||      }d|j                  d<   t        | |       d {    t        | |       |S 7 H7 wN   i  status_codeno-cacheCache-Control)is_ready_for_browser_connectionrE   rJ   rV   rG   okmessagestatusrH   rh   runtimes        r>   _health_endpointz.create_health_routes.<locals>._health_endpoint  si     #CCCG$W&A,6)222GX. D 	3s!   A A5A 	A
A A c                l   K    d      }d|j                   d<   t        | |       d {    |S 7 wN   rn   rp   rq   rE   rJ   rG   rH   r   s     r>   _health_optionsz-create_health_routes.<locals>._health_options  ;     ,,6)222 	3   (424GETHEADmethodsOPTIONSrG   r   returnrh   rG   r   r   r   )starlette.responsesrh   r   starlette.routingrj   r?   r-   )rw   r<   rj   rx   r~   rh   r   s   `    @@r>   create_health_routesr      sZ    & @' 	}h/FO	

 	}h/K	
 r@   c                     ddl mm ddlm} d
 fd}dfd} |t        t        |      |ddg       |t        t        |      |d	g      gS )z*Create script health check route handlers.r   rg   ri   c                   K   j                          d {   \  }}|rdnd} ||      }d|j                  d<   t        | |       d {    t        | |       |S 7 H7 wrl   )does_script_run_without_errorrE   rJ   rV   rs   s        r>   _script_health_endpointz<create_script_health_routes.<locals>._script_health_endpoint4  sl     #AACCG$W&A,6)222GX. D 	3s!   A$A 5A$A"A$"A$c                l   K    d      }d|j                   d<   t        | |       d {    |S 7 wrz   r|   r}   s     r>   _script_health_optionsz;create_script_health_routes.<locals>._script_health_options=  r   r   r   r   r   r   r   r   )r   rh   r   r   rj   r?   r.   )rw   r<   rj   r   r   rh   r   s   `    @@r>   create_script_health_routesr   -  s\     @' 	+X6#FO	

 	+X6"K	
 r@   c                     ddl mm ddlm} d	 fd}d	fd} |t        t        |      |dg       |t        t        |      |dg      gS )
zCreate metrics route handlers.r   rg   ri   c                  K   | j                   j                  d      }	j                  j                  |r|nd       }| j                  j                  dd      }d|v r.t        j                  |      j                         } |d      }nt        j                  |      } |d      }t        | |       d {    |S 7 w)Nfamilies)family_namesAcceptr8   zapplication/x-protobuf
media_typezapplication/openmetrics-text)query_paramsgetlist	stats_mgr	get_statsrE   rF   r   _stats_to_protoSerializeToString_stats_to_textrJ   )
rG   requested_familiesstatsacceptpayloadrH   textrh   r   rw   s
          r>   _metrics_endpointz0create_metrics_routes.<locals>._metrics_endpointV  s     $1199*E!!++/A+t , 
 $$Xr2#v-)99%@RRTG4LMH&55e<D(!?H  222 	3s   B8C;C<Cc                   K    d      }d|j                   d<   d|j                   d<   t        | |       d {    |S 7 w)Nr{   rn   GET, OPTIONSAccess-Control-Allow-Methodsr   Access-Control-Allow-Headersr|   r}   s     r>   _metrics_optionsz/create_metrics_routes.<locals>._metrics_optionsg  sM     ,;I78;C78222 	3s   7AAAr   r   r   r   )r   rh   r   r   rj   r?   r/   )rw   r<   rj   r   r   rh   r   s   `    @@r>   create_metrics_routesr   Q  sU    ?'" 	~x0G	

 	~x0K	
 r@   c                ^    ddl m ddlm} dfd} |t	        t
        |       |dg      gS )z"Create host config route handlers.r   )JSONResponseri   c           
        K   t        t              }t        j                  d      rd|vr|j	                  d        |dddddd d      }t        | |       d {    d|j                  d<   |S 7 w)Nzglobal.developmentModezhttp://localhostFr8   )allowedOriginsuseExternalAuthTokenenableCustomParentMessagesenforceDownloadInNewTab
metricsUrlblockErrorDialogsresourceCrossOriginModerp   rq   )listr   r   r:   r_   rJ   rE   )rG   allowedrH   r   s      r>   _host_config_endpointz8create_host_config_routes.<locals>._host_config_endpoint  s     7867"'1NN-.")(-.3+0 %*+/

  222,6) 	3s   AA4A2A4r   r   )rG   r   r   r   )r   r   r   rj   r?   r0   )r<   rj   r   r   s      @r>   create_host_config_routesr   |  s5    0'0 	)84!G	
 r@   c                     ddl m ddlm ddlm} d fd}dfd} |t        t        |      |ddg	       |t        t        |      |d
g	      gS )a  Create media file route handlers for /media/{file_id}.

    Serves media files (images, audio, video) stored by st.image, st.audio,
    st.video, and st.download_button. Supports HTTP range requests for
    streaming media playback.

    Parameters
    ----------
    media_storage
        The media file storage backend.
    base_url
        Optional base URL path prefix for the routes.

    Returns
    -------
    list[BaseRoute]
        List of Starlette Route objects for GET, HEAD, and OPTIONS methods.
    r   HTTPExceptionr   ri   c                P  K   | j                   d   }	 j                  |      }i }|j                  t        j
                  k(  rF|j                  }|sdt        |j                         }	 |j                  d       d| d}d
| |d<   d|d<   |j                  }t        |      }d}	| j                  j                  d      }
|
rJ	 t!        j"                  |
|      \  }}d}	|||dz    }d| d| d| |d<   t'        t        |            |d<   nt'        |      |d<    ||	|j                  xs d|      }t)        | |       d {    |S # t        $ r} dd      |d }~ww xY w# t        $ r d	t        |       }Y w xY w# t$        $ r  dddd| i      w xY w7 ]w)Nfile_id  File not foundro   detailstreamlit_downloadlatin1z
filename=""zfilename*=utf-8''zattachment; zContent-DispositionbyteszAccept-Rangesrm   rangei  zInvalid rangezContent-Rangezbytes */)ro   r   rE         zbytes -/zContent-Length
text/plain)ro   r   rE   )path_paramsget_filer   kindr   DOWNLOADABLEfilenamer   mimetyper[   UnicodeEncodeErrorr   contentlenrE   rF   r   parse_range_header
ValueErrorstrrJ   )rG   r   
media_fileexcrE   r   dispositionr   content_lengthro   range_headerrange_start	range_endrH   r   r   media_storages                 r>   _media_endpointz,create_media_routes.<locals>._media_endpoint  s    %%i0	S&//8J #%??m888!**H(1*2E2EFGI D) *8*A6 0<K=-IG)* $+ $$W**73	)<)O)O .*&Y KkIM:GQyk>2BC O$ ),CL(9G$%(+N(;G$%#!**:l	
  222k % 	SC8HIsR	S & D 1%/1BCD   # #*,8H.IJ * 	3sr   F&E AF&+E) AF&F A'F&F$F&	E&E!!E&&F&)F F&FF&F!!F&c                N   K    d      }t        | |       d {    |S 7 wNr{   rn   rJ   r}   s     r>   _media_optionsz+create_media_routes.<locals>._media_options  +     ,222 	3   %#%r   r   r   r   r   )starlette.exceptionsr   r   r   r   rj   r?   r1   )r   r<   rj   r   r   r   r   s   `    @@r>   create_media_routesr     s]    * 3,':x 	|X.FO		
 	|X.K	
 r@   c                &   	
 ddl m	 ddlm ddlm ddlm} dfd
dddfd}d	
 fd	}d
fd
} |t        t        |      |dg       |t        t        |      |dg       |t        t        |      |dg      gS )aw  Create file upload route handlers for /_stcore/upload_file/{session_id}/{file_id}.

    Handles file uploads from st.file_uploader widgets. Supports PUT for uploading
    files and DELETE for removing them. XSRF protection is enforced when enabled.

    Parameters
    ----------
    runtime
        The Streamlit runtime instance (used to validate session IDs).
    upload_mgr
        The uploaded file manager to store/retrieve files.
    base_url
        Optional base URL path prefix for the routes.

    Returns
    -------
    list[BaseRoute]
        List of Starlette Route objects for PUT, DELETE, and OPTIONS methods.
    r   )
UploadFiler   r   ri   c                    t               sy| j                  j                  d      }| j                  j                  t              }t        ||      s
 dd      y)zValidate XSRF token for non-safe HTTP methods.

        Raises HTTPException with 403 if XSRF is enabled and validation fails.
        This mirrors Tornado's automatic XSRF protection for non-GET requests.
        NzX-Xsrftoken  zXSRF token missing or invalidr   )r   rE   rF   rM   r   r   )rG   xsrf_headerxsrf_cookier   s      r>   _check_xsrfz)create_upload_routes.<locals>._check_xsrf'  sT      oo))-8oo))*:;";<C8WXX =r@   c                :  K   d|j                   d<   d|j                   d<   t               rYt        t        j                  d            |j                   d<   d|j                   d<   d|j                   d	<   d
|j                   d<   y t        | |       d {    y 7 w)NzPUT, OPTIONS, DELETEr   Content-Typer   zbrowser.serverAddressrC   zX-Xsrftoken, Content-TyperD   Varytruez Access-Control-Allow-Credentials)rE   r   r   r   r:   rJ   )rG   rH   s     r>   _set_upload_headersz1create_upload_routes.<locals>._set_upload_headers6  s     ;Q78;I78>E!!"9:?H:; , ;< (0HV$CIH?@#GX666s   BBBBc                H   K    d      } | |       d {    |S 7 wr    )rG   rH   r   r   s     r>   _upload_optionsz-create_upload_routes.<locals>._upload_optionsE  s+     ,!'8444 	5s   " "c           	       K    |        | j                   d   }| j                   d   }j                  |      s
 dd      t        j                  d      dz  dz  }| j                  j                  d      }|r	 t        |      |kD  r
 d	d
      	 | j                          d{   }|j                         D cg c]  }t        |      s| }}t        |      dk7  r ddt        |             |d   }	 |j                          d{   }	|j                  j                          t        |	      |kD  r
 d	d
      j                  |t!        ||j"                  xs d|j$                  xs d|	              d      }
 | |
       d{    |
S # t        $ r  dd      w xY w7 c c}w 7 # |j                  j                          w xY w7 Fw)zUpload a file to the server.
session_idr   i  zInvalid session_idr   zserver.maxUploadSizei   zcontent-lengthi  zFile too largezInvalid Content-Length headerNr   zExpected 1 file, but got r   r8   zapplication/octet-stream)r   rc   typedata)r   filer{   rn   )r   is_active_sessionr   r:   rE   rF   intr   formvalues
isinstancer   readr   closeadd_filer   r   content_type)rG   r   r   max_size_bytesr   r  rd   uploadsuploadr   rH   r   r   r   r   r   rw   
upload_mgrs              r>   _upload_putz)create_upload_routes.<locals>._upload_putJ  s     	G((6
%%i0((4C8LMM 45<tC 	
 !,,-=>~&7'C@PQQ 8 \\^#&*kkmUUz%7T5UUw<1*CCL>(R  
	 &DKKt9~%C8HII! __*((F,F	 	 	
 ,!'8444K  # #,K 
 $U 'KK 	5sy   A:G'>F% G'*F<+G'F?F?+G'G GG BG'G%G'%F99G'?G'G G""G'c                   K    |        | j                   d   }| j                   d   }j                  ||        d      } | |       d{    |S 7 w)zDelete a file from the server.r   r   )r   r   r{   rn   N)r   remove_file)rG   r   r   rH   r   r   r   r  s       r>   _upload_deletez,create_upload_routes.<locals>._upload_delete  sh      	G((6
%%i0*gF,!'8444 	5s   AAAAPUTr   DELETEr   )rG   r   r   NonerG   r   rH   r   r   r  r   )
starlette.datastructuresr   r   r   r   r   r   rj   r?   r2   )rw   r  r<   rj   r   r  r  r   r   r   r   r   s   ``     @@@@@r>   create_upload_routesr  
  s    0 42,'Y7
: :x  	)84G	

 	)84J	

 	)84K	
 r@   c                     ddl ddlm ddlm ddlm} d fd}dfd} |t        t        |      |dg	       |t        t        |      |d
g	      gS )z'Create custom component route handlers.r   Nr   r   ri   c                  K   | j                   d   }|j                  dd      }t        |      dk(  s|d   s
 dd      |d   }t        |      d	k(  r|d   nd
}j                  |      }|
 dd      t	        ||      }|
 dd      	 j                  |d       d {   4 d {   }|j                          d {   }d d d       d {     t        |            }
t        | |
       d {    |r|j                  d      rd|
j                  d<   |
S d|
j                  d<   |
S 7 7 7 t7 f# 1 d {  7  sw Y   vxY w# t        $ r}	 dd      |	d }	~	ww xY w7 rw)Nr;   r   r   )maxsplitr   r   zComponent not foundr      r8   r   	Forbiddenrb
read errorr   r   .htmlrp   rq   public)r   splitr   get_component_pathr   	open_filer  OSErrorr   rJ   endswithrE   )rG   r;   partscomponent_namer   component_rootabspathr   r   r   rH   r   r   anyiocomponent_registrys              r>   _component_endpointz4create_component_routes.<locals>._component_endpoint  s    ""6*

3
+u:?%(C8MNNq"5zQ58B+>>~N!C8MNN %^X>?CDD	O"__Wd;; ) )t!YY[() )
 D5G5PQ2228,,W50:H_-  19H_- < )() ) ) ) 	OCE3N	O 	3s   BE5E (D:)E 1D<2E 5E	D>
EE E E "E5 E39E5:E <E >E E EE	EE 	E0 E++E00E5c                N   K    d      }t        | |       d {    |S 7 wr   r   r}   s     r>   _component_optionsz3create_component_routes.<locals>._component_options  r   r   r   r   r   r   )	r)  r   r   r   r   r   rj   r?   r3   )r*  r<   rj   r+  r-  r   r   r)  s   `    @@@r>   create_component_routesr.    se     2,'! !F 	+X6G	

 	+X6K	
 r@   c                     ddl ddlmm ddlm} d
 fd}d
fd} |t        t        |      |dg       |t        t        |      |d	g      gS )z.Create bidirectional component route handlers.r   Nrg   ri   c                   K   d fd} j                   d   }|j                  d      }|r|d   nd}|s |dd       d {   S j                  |       |dd       d {   S j                  |      }| |dd       d {   S dj	                  |dd        }|r|j                  d      r |dd       d {   S t        ||      }| |d	d
       d {   S t        j                  j                  |      r |dd       d {   S 	 j                  |d       d {   4 d {   }|j                          d {   }	d d d       d {     	t!        |            }t#         |       d {    |j                  d      rd|j$                  d<   |S d|j$                  d<   |S 7 x7 Y7 87 7 7 7 7 7 7 t# 1 d {  7  sw Y   xY w# t        $ rM |j                  dd      j                  dd      }
t        j                  d|
        |dd       d {  7  cY S w xY w7 w)Nc                P   K    | |      }t        |       d {    |S 7 w)Nrn   r   )bodyro   rH   rh   rG   s      r>   _text_responsezVcreate_bidi_component_routes.<locals>._bidi_component_endpoint.<locals>._text_response  s-     (;GH#GX666O 7s   &$&r;   r   r   r8   z	not foundr   r   	forbiddenr   r  
z&Error reading bidi component asset: %sr  r  r  rp   rq   r  )r2  r   ro   r   r   rh   )r   r   rF   r!  rZ   r$  r   osr;   isdirr"  r  r#  replacer%   	exceptionr   rJ   rE   )rG   r3  r;   r%  r&  r'  r   r(  r   r   sanitized_abspathrH   rh   r   r)  bidi_component_managers   `           r>   _bidi_component_endpointz>create_bidi_component_routes.<locals>._bidi_component_endpoint  sF    	
 ""6*

3%*q'S999!%%n5='S999/BB>R!'S99988E!"I&8,,S1'S999$^X>?'S99977==!'S999	;"__Wd;; ) )t!YY[() ) D5G5PQ222W%0:H_-  19H_-M : : : : : : < )() ) ) ) 	; 'b 9 A A$ K8:K (c::::	; 	3s  ?IF;!I%F>&#I	G
7IGI G!/IG	IG( +G,G( 4G5G( 8GGGG( GG( !"II8I>IIII	IG( G( GG( G%GG%!G( (AH>5H86H>;I=H>>Ic                N   K    d      }t        | |       d {    |S 7 wr   r   r}   s     r>   _bidi_component_optionsz=create_bidi_component_routes.<locals>._bidi_component_options  r   r   r   r   r   r   )r)  r   rh   r   r   rj   r?   r4   )r<  r<   rj   r=  r?  rh   r   r)  s   `    @@@r>   create_bidi_component_routesr@    sb     ?'0 0d 	+X6$G	

 	+X6#K	
 r@   c                "   ddl m ddlmm ddlm} | r2t        j                  j                  t        j                  |             nddfd}dfd} |t        t        |      |dg	       |t        t        |      |d
g	      gS )z.Create app static serving file route handlers.r   r   )FileResponser   ri   Nc                .  K   s
 dd      | j                   j                  dd      }t        |      }|
 dd      t        j                  j                  |      rt        j                  j                  |      r
 dd      t        j                  j                  |      t        kD  r
 dd      t        |      j                  j                         }d }|t        vrd} ||      }d	|j                  d
<   d|j                  d<   |S w)Nr   r   r   r;   r8   zFile is too larger   r   rB   rC   nosniffzX-Content-Type-Options)r   rF   r   r7  r;   existsr8  getsizer   r   suffixr]   r   rE   )	rG   relative_path	safe_pathextr   rH   rB  r   app_static_roots	         r>   _app_static_endpointz>create_app_static_serving_routes.<locals>._app_static_endpoint@  s    C8HII++//;&F	C8HIIww~~i(BGGMM),DC8HII77??9%(@@* 
 9o$$**,
55%J	jA:=675>12s   DDc                |   K    d      }d|j                   d<   d|j                   d<   d|j                   d<   |S w)	Nr{   rn   rB   rC   r   r   r   r   )rE   )_requestrH   r   s     r>   _app_static_optionsz=create_app_static_serving_routes.<locals>._app_static_options\  sI     ,:=67;I78;I78s   9<r   r   r   r   )rN  r   r   r   )r   r   r   rB  r   r   rj   r7  r;   realpathr	   get_app_static_dirr?   r5   )	main_script_pathr<   rj   rL  rO  rB  r   r   rK  s	        @@@@r>    create_app_static_serving_routesrS  2  s     3:'  	556FGH 8 	((3 G	

 	((3K	
 r@   )N)r;   r   r<   
str | Noner   r   r  )
rH   r   r`   r   rU   r   rL   rQ   r   r  )rw   r"   r<   rT  r   list[BaseRoute])r<   rT  r   rU  )r   r#   r<   rT  r   rU  )rw   r"   r  r$   r<   rT  r   rU  )r*  r    r<   rT  r   rU  )r<  r!   r<   rT  r   rU  )rR  rT  r<   rT  r   rU  )X__doc__
__future__r   r7  pathlibr   typingr   r   urllib.parser   	streamlitr   r	   streamlit.loggerr
   $streamlit.runtime.media_file_storager   r   +streamlit.runtime.memory_media_file_storager   'streamlit.runtime.uploaded_file_managerr   ,streamlit.web.server.app_static_file_handlerr   r   )streamlit.web.server.component_file_utilsr   r   streamlit.web.server.routesr   r   r    streamlit.web.server.server_utilr   r   streamlit.web.server.starletter   2streamlit.web.server.starlette.starlette_app_utilsr   6streamlit.web.server.starlette.starlette_server_configr   *streamlit.web.server.stats_request_handlerr   starlette.requestsr   r   r   r   r   2streamlit.components.types.base_component_registryr    )streamlit.components.v2.component_managerr!   streamlit.runtimer"   r#   .streamlit.runtime.memory_uploaded_file_managerr$   __name__r%   __annotations__r'   r)   r*   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r?   rJ   rV   rP   r   r   r   r   r   r  r.  r@  rS  r   r@   r>   <module>ro     s   / " 	  '  ' ' U R C 
 F > R S J*,+XN)RXH% % # "! % !#2"3< @  @) e ) **'2u 2!0 11EF e F ++84 4  //|< E < ***;<e <566QR E R "6 6nE e E!0 11OP e P 4 5 3%0A.-`9,9,9, 9,
 9, 
9,x0f!! *!!H(V"Jf)f5?ffRYY)Y Y 	Yx<-<9C<<~J0J<FJJZ< <,6<<r@   