
Á«‘_%  ã               @   s0  d  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 d d l m Z d d l m Z d d l m Z Gd d „  d e ƒ Z d S)z¤
Serve Shared Static Files
=========================

.. autoclass:: SharedDataMiddleware
    :members: is_allowed

:copyright: 2007 Pallets
:license: BSD-3-Clause
é    N)Údatetime)ÚBytesIO)Úmktime)Útime)Úadler32é   )ÚPY2)Ústring_types)Úget_filesystem_encoding)Ú	http_date)Úis_resource_modified)Ú	safe_join)Úget_content_type)Úget_path_info)Ú	wrap_filec               @   s‚   e  Z d  Z d Z d d d d d d „ Z d	 d
 „  Z d d „  Z d d „  Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d S)ÚSharedDataMiddlewareaã	  A WSGI middleware that provides static content for development
    environments or simple server setups. Usage is quite simple::

        import os
        from werkzeug.middleware.shared_data import SharedDataMiddleware

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

    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.  One can also mount
    files 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 be 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é<   é   zapplication/octet-streamc       
         s#  | |  _  g  |  _ | |  _ | |  _ t | d ƒ r? | j ƒ  } x  | D]˜ \ } } t | t ƒ rs |  j | Œ  }	 nU t | t	 ƒ r¸ t
 j j | ƒ r¦ |  j | ƒ }	 qÈ |  j | ƒ }	 n t d | ƒ ‚ |  j j | |	 f ƒ qF Wˆ  d  k	 rd d l m ‰ ‡  ‡ f d d †  |  _ | |  _ d  S)NÚitemszunknown def %rr   )Úfnmatchc                s   ˆ |  ˆ  ƒ S)N© )Úx)Údisallowr   r   úC/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/middleware/shared_data.pyÚ<lambda>   s    z/SharedDataMiddleware.__init__.<locals>.<lambda>)ÚappÚexportsÚcacheÚcache_timeoutÚhasattrr   Ú
isinstanceÚtupleÚget_package_loaderr	   ÚosÚpathÚisfileÚget_file_loaderÚget_directory_loaderÚ	TypeErrorÚappendr   Ú
is_allowedÚfallback_mimetype)
Úselfr   r   r   r   r   r+   ÚkeyÚvalueÚloaderr   )r   r   r   Ú__init__^   s&    					zSharedDataMiddleware.__init__c             C   s   d S)zµSubclasses can override this method to disallow the access to
        certain files.  However by providing `disallow` in the constructor
        this method is overwritten.
        Tr   )r,   Úfilenamer   r   r   r*   ƒ   s    zSharedDataMiddleware.is_allowedc                s   ‡  f d d †  S)Nc                  s=   t  ˆ  d ƒ t j t j j ˆ  ƒ ƒ t t j j ˆ  ƒ ƒ f S)NÚrb)Úopenr   Úutcfromtimestampr#   r$   ÚgetmtimeÚintÚgetsizer   )r1   r   r   r   ‹   s    z.SharedDataMiddleware._opener.<locals>.<lambda>r   )r,   r1   r   )r1   r   Ú_openerŠ   s    zSharedDataMiddleware._openerc                s   ‡  ‡ f d d †  S)Nc                s   t  j j ˆ  ƒ ˆ j ˆ  ƒ f S)N)r#   r$   Úbasenamer8   )r   )r1   r,   r   r   r   ’   s    z6SharedDataMiddleware.get_file_loader.<locals>.<lambda>r   )r,   r1   r   )r1   r,   r   r&   ‘   s    z$SharedDataMiddleware.get_file_loaderc                s¸   t  j ƒ  ‰ t j | ƒ ‰ t ˆ d ƒ rT ˆ j | ƒ ‰ ‡ ‡ ‡ f d d †  } n` ˆ j | ƒ } t j j	 | ƒ ‰  t j j
 t j j | ƒ ˆ ƒ ‰ ‡  ‡ ‡ ‡ ‡ f d d †  } | S)NÚget_resource_readerc                s˜   |  d  k r d St  ˆ |  ƒ }  t j |  ƒ } y ˆ j |  ƒ ‰  Wn t k
 rY d SYn Xt ˆ  t ƒ r‚ | ‡ ‡  f d d †  f S| ‡  f d d †  f S)Nc                  s   ˆ ˆ  t  ˆ j ƒ  ƒ f S)N)ÚlenÚgetvaluer   )ÚloadtimeÚresourcer   r   r   «   s    zISharedDataMiddleware.get_package_loader.<locals>.loader.<locals>.<lambda>c                  s4   ˆ  t  j t j j ˆ  j ƒ ƒ t j j ˆ  j ƒ f S)N)r   r4   r#   r$   r5   Únamer7   r   )r>   r   r   r   °   s    )NN)NN)r   Ú	posixpathr9   Zopen_resourceÚIOErrorr    r   )r$   r9   )r=   Úpackage_pathÚreader)r>   r   r/   œ   s    	z7SharedDataMiddleware.get_package_loader.<locals>.loaderc                s¢   |  d  k r d St  ˆ |  ƒ }  t j |  ƒ } ˆ r] t j j |  ƒ sJ d S| ˆ j |  ƒ f Sy ˆ j |  ƒ ‰  Wn t k
 rˆ d SYn X| ‡  ‡ f d d †  f S)Nc                  s   t  ˆ  ƒ ˆ t ˆ  ƒ f S)N)r   r;   r   )Údatar=   r   r   r   Ï   s    zISharedDataMiddleware.get_package_loader.<locals>.loader.<locals>.<lambda>)NN)NN)NN)	r   r@   r9   r#   r$   r%   r8   Úget_datarA   )r$   r9   )Úis_filesystemr=   ÚproviderÚrootr,   )rD   r   r/   ½   s    	)r   ÚutcnowÚpkgutilÚ
get_loaderr   r:   Úget_filenamer#   r$   ÚexistsÚjoinÚdirname)r,   ÚpackagerB   r/   Zpackage_filenamer   )rF   r=   rB   rG   rC   rH   r,   r   r"   ”   s    !z'SharedDataMiddleware.get_package_loaderc                s   ‡  ‡ f d d †  } | S)Nc                sY   |  d  k	 r t  ˆ  |  ƒ }  n ˆ  }  t j j |  ƒ rU t j j |  ƒ ˆ j |  ƒ f Sd S)N)NN)r   r#   r$   r%   r9   r8   )r$   )Ú	directoryr,   r   r   r/   Ô   s    z9SharedDataMiddleware.get_directory_loader.<locals>.loaderr   )r,   rQ   r/   r   )rQ   r,   r   r'   Ó   s    z)SharedDataMiddleware.get_directory_loaderc             C   sH   t  | t ƒ s! | j t ƒ  ƒ } d t | j ƒ  ƒ | t | ƒ d @f S)Nzwzsdm-%d-%s-%sl   ÿÿ )r    ÚbytesÚencoder
   r   Ú	timetupler   )r,   ÚmtimeÚ	file_sizeÚreal_filenamer   r   r   Úgenerate_etagá   s    z"SharedDataMiddleware.generate_etagc             C   s:  t  | ƒ } t r$ | j t ƒ  ƒ } d  } x™ |  j D]Ž \ } } | | k rk | d  ƒ \ } } | d  k	 rk P| j d ƒ s„ | d 7} | j | ƒ r4 | | t | ƒ d  … ƒ \ } } | d  k	 r4 Pq4 W| d  k sâ |  j | ƒ rò |  j	 | | ƒ St
 j | ƒ } t | d p|  j d ƒ }	 | ƒ  \ }
 } } d t ƒ  f g } |  j rß|  j } |  j | | | ƒ } | d d | f d d | f g 7} t | | d	 | ƒs¼|
 j ƒ  | d
 | ƒ g  S| j d t t ƒ  | ƒ f ƒ n | j d ƒ | j d |	 f d t | ƒ f d t | ƒ f f ƒ | d | ƒ t | |
 ƒ S)Nú/r   zutf-8ÚDateZEtagz"%s"úCache-Controlzmax-age=%d, publicZlast_modifiedz304 Not ModifiedZExpiresÚpubliczContent-TypezContent-LengthzLast-Modifiedz200 OK)r[   r\   )r   r   rS   r
   r   ÚendswithÚ
startswithr;   r*   r   Ú	mimetypesÚ
guess_typer   r+   r   r   r   rX   r   Úcloser)   r   ÚextendÚstrr   )r,   ÚenvironZstart_responser$   Zfile_loaderÚsearch_pathr/   rW   Zguessed_typeZ	mime_typeÚfrU   rV   ÚheadersÚtimeoutÚetagr   r   r   Ú__call__ë   sN    
"		
#	zSharedDataMiddleware.__call__i  iÀ¨  )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r*   r8   r&   r"   r'   rX   rj   r   r   r   r   r       s   <?
r   )rn   r_   r#   rJ   r@   r   Úior   r   r   Úzlibr   Ú_compatr   r	   Ú
filesystemr
   Úhttpr   r   Úsecurityr   Úutilsr   Zwsgir   r   Úobjectr   r   r   r   r   Ú<module>
   s&   