o
    'hF%                     @  sL  d Z ddlmZ ddlm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 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 d	dlmZ ejg eej e! ee"f f Z#ejej$e% geej$e% ej$e# f f Z&ej'rddl(m)Z) ddl(m*Z* ddl(m+Z+ G dd dZ,dS )z
Serve Shared Static Files
=========================

.. autoclass:: SharedDataMiddleware
    :members: is_allowed

:copyright: 2007 Pallets
:license: BSD-3-Clause
    )annotationsN)datetime)timezone)BytesIO)time)adler32   )	http_date)is_resource_modified)	safe_join)get_content_type)get_path_info)	wrap_file)StartResponse)WSGIApplication)WSGIEnvironmentc                   @  sj   e Zd ZdZ				d2d3ddZd4ddZd5ddZd6ddZd7d d!Zd8d#d$Z	d9d)d*Z
d:d0d1ZdS );SharedDataMiddlewarea	  A WSGI middleware which provides static content for development
    environments or simple server setups. Its usage is quite simple::

        import os
        from werkzeug.middleware.shared_data import SharedDataMiddleware

        app = SharedDataMiddleware(app, {
            '/shared': os.path.join(os.path.dirname(__file__), 'shared')
        })

    The contents of the folder ``./shared`` will now be available on
    ``http://example.com/shared/``.  This is pretty useful during development
    because a standalone media server is not required. Files can also be
    mounted on the root folder and still continue to use the application because
    the shared data middleware forwards all unhandled requests to the
    application, even if the requests are below one of the shared folders.

    If `pkg_resources` is available you can also tell the middleware to serve
    files from package data::

        app = SharedDataMiddleware(app, {
            '/static': ('myapplication', 'static')
        })

    This will then serve the ``static`` folder in the `myapplication`
    Python package.

    The optional `disallow` parameter can be a list of :func:`~fnmatch.fnmatch`
    rules for files that are not accessible from the web.  If `cache` is set to
    `False` no caching headers are sent.

    Currently the middleware does not support non-ASCII filenames. If the
    encoding on the file system happens to match the encoding of the URI it may
    work but this could also be by accident. We strongly suggest using ASCII
    only file names for static files.

    The middleware will guess the mimetype using the Python `mimetype`
    module.  If it's unable to figure out the charset it will fall back
    to `fallback_mimetype`.

    :param app: the application to wrap.  If you don't want to wrap an
                application you can pass it :exc:`NotFound`.
    :param exports: a list or dict of exported files and folders.
    :param disallow: a list of :func:`~fnmatch.fnmatch` rules.
    :param cache: enable or disable caching headers.
    :param cache_timeout: the cache timeout in seconds for the headers.
    :param fallback_mimetype: The fallback mimetype for unknown files.

    .. versionchanged:: 1.0
        The default ``fallback_mimetype`` is
        ``application/octet-stream``. If a filename looks like a text
        mimetype, the ``utf-8`` charset is added to it.

    .. versionadded:: 0.6
        Added ``fallback_mimetype``.

    .. versionchanged:: 0.5
        Added ``cache_timeout``.
    NT  application/octet-streamappr   exportsXcabc.Mapping[str, str | tuple[str, str]] | t.Iterable[tuple[str, str | tuple[str, str]]]disallowNonecacheboolcache_timeoutintfallback_mimetypestrreturnc           
        s   || _ g | _|| _|| _t|tjr| }|D ]5\}}t|tr'| j	| }	nt|t
r>tj|r8| |}	n| |}	ntd|| j||	f q d ur`ddlm  fdd| _|| _d S )Nzunknown def r   )fnmatchc                   s   |   S N xr   r!   r#   [/var/www/html/olx_land/venv/lib/python3.10/site-packages/werkzeug/middleware/shared_data.py<lambda>   s    z/SharedDataMiddleware.__init__.<locals>.<lambda>)r   r   r   r   
isinstancecabcMappingitemstupleget_package_loaderr   ospathisfileget_file_loaderget_directory_loader	TypeErrorappendr!   
is_allowedr   )
selfr   r   r   r   r   r   keyvalueloaderr#   r&   r'   __init__g   s&   


zSharedDataMiddleware.__init__filenamec                 C  s   dS )zSubclasses can override this method to disallow the access to
        certain files.  However by providing `disallow` in the constructor
        this method is overwritten.
        Tr#   r7   r<   r#   r#   r'   r6      s   zSharedDataMiddleware.is_allowed_TOpenerc                   s    fddS )Nc                     s0   t  dtjtj tjdttj	 fS )Nrbtz)
openr   fromtimestampr/   r0   getmtimer   utcr   getsizer#   r<   r#   r'   r(      s   z.SharedDataMiddleware._opener.<locals>.<lambda>r#   r=   r#   rG   r'   _opener   s   zSharedDataMiddleware._opener_TLoaderc                   s    fddS )Nc                   s   t j  fS r"   )r/   r0   basenamerH   r$   r<   r7   r#   r'   r(      s    z6SharedDataMiddleware.get_file_loader.<locals>.<lambda>r#   r=   r#   rK   r'   r2      s   z$SharedDataMiddleware.get_file_loaderpackagepackage_pathc                   s:   t tj tj|}|j|d fdd}|S )Nr0   
str | Noner    "tuple[str | None, _TOpener | None]c                   s|   | d u rdS t | } | d u rdS t| }z|  W n
 ty'   Y dS w t tr6| fddfS | fddfS )NNNc                     s    t  fS r"   )lengetvaluer#   )	load_timeresourcer#   r'   r(      s    zISharedDataMiddleware.get_package_loader.<locals>.loader.<locals>.<lambda>c                     s*    t jtj jtjdtj jfS )Nr@   )	r   rC   r/   r0   rD   namer   rE   rF   r#   rT   r#   r'   r(      s   )r   	posixpathrJ   open_resourceOSErrorr)   r   )r0   rJ   rS   rM   readerrV   r'   r:      s$   



z7SharedDataMiddleware.get_package_loader.<locals>.loaderr0   rN   r    rO   )	r   nowr   rE   	importlibutil	find_specr:   get_resource_reader)r7   rL   rM   specr:   r#   rZ   r'   r.      s
   #z'SharedDataMiddleware.get_package_loader	directoryc                   s   d fdd}|S )Nr0   rN   r    rO   c                   sJ   | d urt  | } | d u rdS n } tj| r#tj| | fS dS )NrP   )r   r/   r0   r1   rJ   rH   )r0   rc   r7   r#   r'   r:      s   
z9SharedDataMiddleware.get_directory_loader.<locals>.loaderr\   r#   )r7   rc   r:   r#   rd   r'   r3      s   z)SharedDataMiddleware.get_directory_loadermtimer   	file_sizereal_filenamec                 C  s4   t |}| }t|d@ }d| d| d| S )Nl    zwzsdm--)r/   fsencode	timestampr   )r7   re   rf   rg   fn_strrj   checksumr#   r#   r'   generate_etag   s   
z"SharedDataMiddleware.generate_etagenvironr   start_responser   t.Iterable[bytes]c                 C  s  t |}d }| jD ]4\}}||kr|d \}}|d ur n!|ds&|d7 }||r=||t|d  \}}|d ur= nq	|d u sG| |sM| ||S t|}t	|d pY| j
d}	| \}
}}dt fg}| jr| j}| |||}|dd| dfdd| d	fg7 }t|||d
s|
  |d| g S |dtt | f n|d |d|	fdt|fdt|ff |d| t||
S )N/r   zutf-8DateEtag"Cache-Controlzmax-age=z, public)last_modifiedz304 Not ModifiedExpires)ru   publiczContent-TypezContent-LengthzLast-Modifiedz200 OK)r   r   endswith
startswithrQ   r6   r   	mimetypes
guess_typer   r   r	   r   r   rm   r
   closer5   r   extendr   r   )r7   rn   ro   r0   file_loadersearch_pathr:   rg   guessed_type	mime_typefre   rf   headerstimeoutetagr#   r#   r'   __call__   sR   








zSharedDataMiddleware.__call__)NTr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r    r   )r<   r   r    r   )r<   r   r    r>   )r<   r   r    rI   )rL   r   rM   r   r    rI   )rc   r   r    rI   )re   r   rf   r   rg   r   r    r   )rn   r   ro   r   r    rp   )__name__
__module____qualname____doc__r;   r6   rH   r2   r.   r3   rm   r   r#   r#   r#   r'   r   *   s    C
(



*
r   )-r   
__future__r   collections.abcabcr*   importlib.utilr^   r{   r/   rW   typingtr   r   ior   r   zlibr   httpr	   r
   securityr   utilsr   wsgir   r   Callabler-   IObytesr   r>   Optionalr   rI   TYPE_CHECKING_typeshed.wsgir   r   r   r   r#   r#   r#   r'   <module>   s4    *