U
    k;d                     @   sP   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 dZ
G dd deZd	S )
a  
WSGI middleware that finds the registered mapped DAV-Provider, creates a new
RequestServer instance, and dispatches the request.

.. warning::
   The following documentation was taken over from PyFileServer and is outdated.

WsgiDAV file sharing
--------------------

WsgiDAV allows the user to specify in wsgidav.conf a number of
realms, and a number of users for each realm.

Realms
   Each realm corresponds to a filestructure on disk to be stored,
   for example::

      addShare('pubshare','/home/public/share')

   would allow the users to access using WebDAV the directory/file
   structure at /home/public/share from the url
   http://<servername:port>/<approot>/pubshare

   The realm name is set as '/pubshare'

   e.g. /home/public/share/WsgiDAV/LICENSE becomes accessible as
   http://<servername:port>/<approot>/pubshare/WsgiDAV/LICENSE

Users
   A number of user_name/password pairs can be set for each realm::

      adduser('pubshare', 'user_name', 'password', 'description/unused')

   would add a user_name/password pair to realm /pubshare.

Note: if developers wish to maintain a separate users database, you can
write your own domain controller for the HTTPAuthenticator. See
http_authenticator.py and domain_controller.py for more details.


Request Resolver
----------------

WSGI middleware for resolving Realm and Paths for the WsgiDAV
application.

Usage::
   It *must* be configured as the last item on `middleware_stack` list.

   from wsgidav.request_resolver import RequestResolver
   config = {
        ...,
        'middleware_stack': [
            ...,
            RequestResolver,
        ],
    }

The RequestResolver resolves the requested URL to the following values
placed in the environ dictionary. First it resolves the corresponding
realm::

   url: http://<servername:port>/<approot>/pubshare/WsgiDAV/LICENSE
   environ['wsgidav.mappedrealm'] = /pubshare

Based on the configuration given, the resource abstraction layer for the
realm is determined. if no configured abstraction layer is found, the
default abstraction layer fileabstractionlayer.FilesystemAbstractionLayer()
is used::

   environ['wsgidav.resourceAL'] = fileabstractionlayer.MyOwnFilesystemAbstractionLayer()

The path identifiers for the requested url are then resolved using the
resource abstraction layer::

   environ['wsgidav.mappedpath'] = /home/public/share/WsgiDAV/LICENSE
   environ['wsgidav.mappedURI'] = /pubshare/WsgiDAV/LICENSE

in this case, FilesystemAbstractionLayer resolves any relative paths
to its canonical absolute path

The RequestResolver also resolves any value in the Destination request
header, if present, to::

   Destination: http://<servername:port>/<approot>/pubshare/WsgiDAV/LICENSE-dest
   environ['wsgidav.destrealm'] = /pubshare
   environ['wsgidav.destpath'] = /home/public/share/WsgiDAV/LICENSE-dest
   environ['wsgidav.destURI'] = /pubshare/WsgiDAV/LICENSE
   environ['wsgidav.destresourceAL'] = fileabstractionlayer.MyOwnFilesystemAbstractionLayer()

    )util)DAVErrorHTTP_NOT_FOUND)BaseMiddleware)RequestServerreStructuredTextc                       s$   e Zd Z fddZdd Z  ZS )RequestResolverc                    s   t t| ||| d S )N)superr   __init__)selfwsgidav_appnext_appconfig	__class__ (/opt/wsgidav/wsgidav/request_resolver.pyr
      s    zRequestResolver.__init__c           
      c   s   |d }|d }|d dkr|dkrd}|d ksB|  sB|jd krFd}dd	d
|fdt fg}|d ddrx|d |d| dV  d S |d krttd|t	|}|||}|D ]
}	|	V  qt
|dr|  d S )N	PATH_INFOzwsgidav.providerREQUEST_METHODOPTIONS)/*z1,21)zContent-Typez	text/html)zContent-Length0DAVDatezwsgidav.configadd_header_MS_Author_ViaF)zMS-Author-Viar   z200 OK    z)Could not find resource provider for '{}'close)is_readonlylock_managerr   get_rfc1123_timegetappendr   r   formatr   hasattrr   )
r   environstart_responsepathproviderZdav_compliance_levelheadersappapp_itervr   r   r   __call__   sB    


 

zRequestResolver.__call__)__name__
__module____qualname__r
   r.   __classcell__r   r   r   r   r      s   r   N)__doc__wsgidavr   wsgidav.dav_errorr   r   wsgidav.middlewarer   Zwsgidav.request_serverr   __docformat__r   r   r   r   r   <module>   s   [6