
    8CfW9                     F   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mZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0  G d de'e      Z1 G d de      Z2y)    )
formatdate)partialwraps)
guess_type)PathLikepath)PathPurePath)OptionalSequenceSetUnion)unquote)Route)	SanicMeta)
stat_async)DEFAULT_HTTP_CONTENT_TYPE)FileNotFoundHeaderNotFoundRangeNotSatisfiable)ContentRangeHandler)DirectoryHandler)error_logger)	BaseMixin)FutureStatic)Request)HTTPResponsefilefile_streamvalidate_filec                        e Zd ZddZdedefdZ	 	 	 	 	 	 	 	 	 	 	 	 	 ddedee	ef   ded	e
d
e
dee
ef   dedee   dee
   dee   de
dee   deeeee   f      de
dee   fdZy)StaticMixinreturnNc                 "    t               | _        y N)set_future_statics)selfargskwargss      S/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/sanic/mixins/static.py__init__zStaticMixin.__init__   s    25%    staticc                     t         r%   )NotImplementedErrorr(   r.   s     r+   _apply_staticzStaticMixin._apply_static   s    !!r-   urifile_or_directorypatternuse_modified_sinceuse_content_rangestream_large_filesnamehoststrict_slashescontent_typeapplyresource_typeindexdirectory_viewdirectory_handlerc                    | j                  |      }|	| j                  | j                  }	t        |t        t        t
        f      st        d|       	 t        |      j                         }|r|s|rt        d      |st        ||||      }t        |||||||||	|
||      }| j                  j                  |       |r| j                  |       yy# t        $ r t        d      w xY w)a$  Register a root to serve files from. The input can either be a file or a directory.

        This method provides an easy and simple way to set up the route necessary to serve static files.

        Args:
            uri (str): URL path to be used for serving static content.
            file_or_directory (Union[PathLike, str]): Path to the static file
                or directory with static files.
            pattern (str, optional): Regex pattern identifying the valid
                static files. Defaults to `r"/?.+"`.
            use_modified_since (bool, optional): If true, send file modified
                time, and return not modified if the browser's matches the
                server's. Defaults to `True`.
            use_content_range (bool, optional): If true, process header for
                range requests and sends  the file part that is requested.
                Defaults to `False`.
            stream_large_files (Union[bool, int], optional): If `True`, use
                the `StreamingHTTPResponse.file_stream` handler rather than
                the `HTTPResponse.file handler` to send the file. If this
                is an integer, it represents the threshold size to switch
                to `StreamingHTTPResponse.file_stream`. Defaults to `False`,
                which means that the response will not be streamed.
            name (str, optional): User-defined name used for url_for.
                Defaults to `"static"`.
            host (Optional[str], optional): Host IP or FQDN for the
                service to use.
            strict_slashes (Optional[bool], optional): Instruct Sanic to
                check if the request URLs need to terminate with a slash.
            content_type (Optional[str], optional): User-defined content type
                for header.
            apply (bool, optional): If true, will register the route
                immediately. Defaults to `True`.
            resource_type (Optional[str], optional): Explicitly declare a
                resource to be a `"file"` or a `"dir"`.
            index (Optional[Union[str, Sequence[str]]], optional): When
                exposing against a directory, index is  the name that will
                be served as the default file. When multiple file names are
                passed, then they will be tried in order.
            directory_view (bool, optional): Whether to fallback to showing
                the directory viewer when exposing a directory. Defaults
                to `False`.
            directory_handler (Optional[DirectoryHandler], optional): An
                instance of DirectoryHandler that can be used for explicitly
                controlling and subclassing the behavior of the default
                directory handler.

        Returns:
            List[sanic.router.Route]: Routes registered on the router.

        Examples:
            Serving a single file:
            ```python
            app.static('/foo', 'path/to/static/file.txt')
            ```

            Serving all files from a directory:
            ```python
            app.static('/static', 'path/to/static/directory')
            ```

            Serving large files with a specific threshold:
            ```python
            app.static('/static', 'path/to/large/files', stream_large_files=1000000)
            ```
        Nz'Static route must be a valid path, not z=Static file or directory must be a path-like object or stringzWhen explicitly setting directory_handler, you cannot set either directory_view or index. Instead, pass these arguments to your DirectoryHandler instance.)r3   	directoryr@   r?   )_generate_namer;   
isinstancestrbytesr
   
ValueErrorr	   resolve	TypeErrorr   r   r'   addr2   )r(   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   r.   s                    r+   r.   zStaticMixin.static   s,   h ""4(!d&9&9&E!00N+c5(-CD9:K9LM 	 $%6 7 ? ? A .EE  ! 0+-	! 
 	  (v& I  	O 	s   C C,)r#   N)z/?.+TFFr.   NNNTNNFN)__name__
__module____qualname__r,   r   r   r2   rF   r   r   boolintr   r   r   r.    r-   r+   r"   r"      s'   8"L "U " #'"'/4")-&*'+59$8<!E'E' !3/E' 	E'
 !E'  E' "$),E' E' smE' !E' smE' E'  }E' c8C=012E' E'  $$45!E'r-   r"   )	metaclassc                   t    e Zd ZdedefdZdefdZddddeded	e	d
e	de
e	ef   dedee   dee   fdZd Zy)StaticHandleMixinr.   r#   c                 $    | j                  |      S r%   )_register_staticr1   s     r+   r2   zStaticHandleMixin._apply_static   s    $$V,,r-   c                 <   t        |j                  t              r|j                  j                  d      }nat        |j                  t              rt        |j                        }n1t        |j                  t
              st        d      |j                  }|j                  }|j                  }|j                  s,t        j                  |      s|j                  d      }|dz  }n|j                  dk(  r;t        j                  |      rt        d| d      |j                  d      }|dz  }nM|j                  dk(  r$t        j                  |      st        d	| d      |j                  dk7  rt        d
       t        | j                        t!        | j                  ||j"                  |j$                  |j&                  |j(                  |j*                              } | j-                  |ddg||j.                  |j0                  d      |      \  }}|S )z
        Register a static directory handler with Sanic by adding a route to the
        router and registering a handler.
        zutf-8zInvalid file path string./z/<__file_uri__:path>dirz3Resource type improperly identified as directory. ''r   z.Resource type improperly identified as file. 'z2The resource_type should be set to 'file' or 'dir')r4   r6   r7   r8   r<   rA   GETHEADT)r3   methodsr9   r:   r;   r.   )rE   r4   rG   decoder
   rF   rH   r3   r9   r>   r   isfilerstriprJ   r   _static_request_handlerr   r6   r7   r8   r<   rA   router:   r;   )r(   r.   r4   r3   r9   _handlerrb   _s           r+   rV   z"StaticHandleMixin._register_static   s    f..6 & 8 8 ? ? H00(; #F$<$< =F44c:899 & 8 8jj{{ ##;;01jjo--!!U*{{,-)*!-  **S/C))C!!V+DKK5
 %&a)  !!V+D  75556,,"3#)#<#<"(":":#)#<#<#00"(":":


4::FO!00  
 q r-   N)r<   __file_uri__requestr4   r6   r7   r8   rA   r<   re   c                  K   t        d||      }	| j                  |||	       d {   }
	 i }d }|rQt        |
       d {   }|j                  }t	        |j
                  |       d {   }|r|S t        |d      |d<   d }|rnd }|st        |
       d {   }d|d<   t        |j                        |d<   |j                  d	k7  r+	 t        ||      }|d= |j                  |j
                         d
|vr>|xs t        |
      d   xs t        }d|vr|j                  d      s|dk(  r|dz  }||d<   |j                  d	k(  rt!        |      S |rOt#        |t$              rd}n|}|st        |
       d {   }|j                  |k\  rt'        |
||       d {   S t)        |
||       d {   S 7 7 |7 U7 )# t        $ r Y w xY w7 Z7 77 ## t*        t,        f$ r( |j/                  ||j0                         d {  7  cY S t2        $ r  t4        $ r |	t6        $ r t9        j:                  d| d|         w xY ww)NzFile not found)r   relative_urlT)usegmtzLast-ModifiedrG   zAccept-RangeszContent-Lengthr\   zcontent-typer   zcharset=ztext/zapplication/javascriptz; charset=utf-8zContent-Type)headersi   )rj   _rangez*Exception in static request handler: path=, relative_url=)r   _get_file_pathr   st_mtimer    rj   r   rF   st_sizemethodr   updater   r   r   
startswithr   rE   rO   r   r   IsADirectoryErrorPermissionErrorhandler   r   FileNotFoundError	Exceptionr   	exception)r(   rf   r4   r6   r7   r8   rA   r<   re   	not_found	file_pathrj   statsmodified_sinceresponserk   	thresholds                    r+   ra   z)StaticHandleMixin._static_request_handler   s     !"%
	 --|Y
 
	I	G E!(33!&!.w!OO#O+5"4,( F ",Y"77E+2(,/,>()>>V+7!4We!D $$45v~~6W,  1!),Q/10  \1 ++G4#'?? $55L*6'~~'#G44%!"4d;$/	$6	 &0&; ;}}	1%0%wv&    ")WVLLLA
 4O 8 * > !<  M!?3 	I*11'7<<HHHH" 	  	O 	"")* +  ,~/
 	s   %IF:IG F=)G +G ,G 3I4&G G0G G A:G I'G :G;$G G G #I$G 5G6G 9I=G  G G 	GG GG G G .I		H
I	I8I		Ic                   K   t        t        |            }|j                         x}}|rvt        |      j                  d      }t        ||      }|j                         }||k  r|j	                         rd|j
                  v rt        j                  d| d|        |	 |j                  |       |S # t        $ r1 |j	                         st        j                  d| d|        |Y |S w xY ww)NrX   z..zFile not found: path=rl   )
r	   r   rI   lstrip
is_symlinkpartsr   rx   relative_torH   )r(   r4   re   ry   file_path_raw	root_pathrz   unquoted_file_uris           r+   rm   z StaticHandleMixin._get_file_pathY  s    W%678 - 5 5 77	I !( 5 < <S A !24EFM%--/II%m.F.F.H,,,&&+,=+> ?$$0>3  	 !!),   	  ++-&&+,=+> ?$$0>3   . 	 s*   BC2"B5 3C256C/+C2.C//C2)rL   rM   rN   r   r   r2   rV   r   r   rO   r   rP   r   r   rF   ra   rm   rQ   r-   r+   rT   rT      s    -L -U -JJj '+&*`` $	`
 !`  ` "$),` ,` sm` sm`Dr-   rT   N)3email.utilsr   	functoolsr   r   	mimetypesr   osr   r   pathlibr	   r
   typingr   r   r   r   urllib.parser   sanic_routing.router   sanic.base.metar   sanic.compatr   sanic.constantsr   sanic.exceptionsr   r   r   sanic.handlersr   sanic.handlers.directoryr   	sanic.logr   sanic.mixins.baser   sanic.models.futuresr   sanic.requestr   sanic.responser   r   r   r    r"   rT   rQ   r-   r+   <module>r      sf    " $    " 1 1   % % # 5 N N . 5 " ' - ! I IL')y L'^O) Or-   