/// <summary>
        /// Copies the item.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="context">The context.</param>
        /// <param name="store">The store.</param>
        /// <param name="source">The source.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavForbiddenException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException"></exception>
        private static void CopyItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
            IWebDavStoreItem source)
        {
            Uri destinationUri = GetDestinationHeader(context.Request);
            IWebDavStoreCollection destinationParentCollection = GetParentCollection(server, store, destinationUri);

            bool copyContent = (GetDepthHeader(context.Request) != 0);
            bool isNew = true;

            string destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
            IWebDavStoreItem destination = destinationParentCollection.GetItemByName(destinationName);
            
            if (destination != null)
            {
                if (source.ItemPath == destination.ItemPath)
                    throw new WebDavForbiddenException();
                if (!GetOverwriteHeader(context.Request))
                    throw new WebDavPreconditionFailedException();
                if (destination is IWebDavStoreCollection)
                    destinationParentCollection.Delete(destination);
                isNew = false;
            }

            destinationParentCollection.CopyItemHere(source, destinationName, copyContent);

            context.SendSimpleResponse(isNew ? HttpStatusCode.Created : HttpStatusCode.NoContent);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            if (context.Request.ContentLength64 > 0)
            {
                throw new WebDavUnsupportedMediaTypeException();
            }

            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            string           collectionName = context.Request.Url.GetLastSegment();
            IWebDavStoreItem item;

            if ((item = collection.GetItemByName(collectionName)) != null)
            {
                WebDavServer.Log.Warning("MKCOL Failed: item {0} already exists as child of {1}. ",
                                         collectionName, collection.ItemPath);
                throw new WebDavMethodNotAllowedException();
            }


            collection.CreateCollection(collectionName);

            context.SendSimpleResponse((int)HttpStatusCode.Created);
        }
Пример #3
0
        /// <summary>
        /// Retrieves a store item through the specified
        /// <see cref="Uri" /> from the
        /// specified
        /// <see cref="WebDavServer" /> and
        /// <see cref="IWebDavStore" />.
        /// </summary>
        /// <param name="uri">The <see cref="Uri" /> to retrieve the store item for.</param>
        /// <param name="server">The <see cref="WebDavServer" /> that hosts the <paramref name="store" />.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> from which to retrieve the store item.</param>
        /// <returns>
        /// The retrieved store item.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="uri" /> is <c>null</c>.</para>
        /// <para>
        ///   <paramref name="server" /> is <c>null</c>.</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If the item was not found.</exception>
        /// <exception cref="WebDavConflictException"><paramref name="uri" /> refers to a document in a collection, where the collection does not exist.</exception>
        /// <exception cref="WebDavNotFoundException"><paramref name="uri" /> refers to a document that does not exist.</exception>
        public static IWebDavStoreItem GetItem(
            this Uri uri,
            WebDavServer server,
            IWebDavStore store)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            Uri prefixUri = uri.GetPrefixUri(server);
            IWebDavStoreCollection collection = store.Root;

            IWebDavStoreItem item = null;

            if (prefixUri.Segments.Length == uri.Segments.Length)
            {
                return(collection);
            }

            string[] segments = SplitUri(uri, prefixUri);

            for (int index = 0; index < segments.Length; index++)
            {
                string segmentName = Uri.UnescapeDataString(segments[index]);

                IWebDavStoreItem nextItem = collection.GetItemByName(segmentName);
                if (nextItem == null)
                {
                    throw new WebDavNotFoundException(String.Format("Cannot find item {0} from collection {1}", segmentName, collection.ItemPath)); //throw new WebDavConflictException();
                }
                if (index == segments.Length - 1)
                {
                    item = nextItem;
                }
                else
                {
                    collection = nextItem as IWebDavStoreCollection;
                    if (collection == null)
                    {
                        throw new WebDavNotFoundException(String.Format("NextItem [{0}] is not a collection", nextItem.ItemPath));
                    }
                }
            }

            if (item == null)
            {
                throw new WebDavNotFoundException(String.Format("Unable to find {0}", uri));
            }

            return(item);
        }
Пример #4
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="store">The
        /// <see cref="IWebDavStore" /> store object that will provide
        /// collections and documents</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <param name="methodHandlers">A collection of HTTP method handlers to use or
        /// <c>null</c> to use the built-in method handlers.</param>
        /// <exception cref="System.ArgumentException"><para>
        ///   <paramref name="methodHandlers" /> is empty.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
        public WebDavRequestProcessor(IWebDavStore store, IEnumerable <IWebDavMethodHandler> methodHandlers = null)
        {
            _log = LogManager.GetCurrentClassLogger();

            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            _store = store;

            methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;

            IWebDavMethodHandler[] webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();

            if (!webDavMethodHandlers.Any())
            {
                throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers");
            }
            if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
            {
                throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers");
            }

            var handlersWithNames =
                from methodHandler in webDavMethodHandlers
                from name in methodHandler.Names
                select new
            {
                name,
                methodHandler
            };

            _methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);
        }
        /// <summary>
        /// Moves the
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="sourceWebDavStoreItem">The <see cref="IWebDavStoreItem" /> that will be moved</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavForbiddenException">If the source path is the same as the destination path</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException">If one of the preconditions failed</exception>
        private static void MoveItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
            IWebDavStoreItem sourceWebDavStoreItem)
        {
            Uri destinationUri = GetDestinationHeader(context.Request);
            IWebDavStoreCollection destinationParentCollection = GetParentCollection(server, store, destinationUri);

            bool isNew = true;

            string destinationName = destinationUri.GetLastSegment();
            IWebDavStoreItem destination = destinationParentCollection.GetItemByName(destinationName);
            if (destination != null)
            {
                if (sourceWebDavStoreItem.ItemPath == destination.ItemPath)
                    throw new WebDavForbiddenException();
                // if the overwrite header is F, statuscode = precondition failed 
                if (!GetOverwriteHeader(context.Request))
                    throw new WebDavPreconditionFailedException();
                // else delete destination and set isNew to false
                destinationParentCollection.Delete(destination);
                isNew = false;
            }

            destinationParentCollection.MoveItemHere(sourceWebDavStoreItem, destinationName);

            // send correct response
            context.SendSimpleResponse(isNew ? (int)HttpStatusCode.Created : (int)HttpStatusCode.NoContent);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="WebDavDiskStoreItem" /> class.
 /// </summary>
 /// <param name="parentCollection">
 ///     The parent
 ///     <see cref="WebDavDiskStoreCollection" /> that contains this
 ///     <see cref="WebDavDiskStoreItem" />;
 ///     or
 ///     <c>null</c> if this is the root
 ///     <see cref="WebDavDiskStoreCollection" />.
 /// </param>
 /// <param name="path">The path that this <see cref="WebDavDiskStoreItem" /> maps to.</param>
 /// <param name="store"></param>
 /// <exception cref="System.ArgumentNullException">path</exception>
 /// <exception cref="ArgumentNullException"><paramref name="path" /> is <c>null</c> or empty.</exception>
 protected WebDavDiskStoreItem(WebDavDiskStoreCollection parentCollection, string path, IWebDavStore store)
     : base(parentCollection, path, store)
 {
     if (IsNullOrWhiteSpace(path))
         throw new ArgumentNullException(nameof(path));
     _path = path;
 }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException"></exception>
        /// <exception cref="WebDavNotFoundException"><para>
        ///   <paramref name="context" /> specifies a request for a store item that does not exist.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="context" /> specifies a request for a store item that is not a document.</para></exception>
        /// <exception cref="WebDavConflictException"><paramref name="context" /> specifies a request for a store item using a collection path that does not exist.</exception>
        public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);
            IWebDavStoreDocument doc = item as IWebDavStoreDocument;
            if (doc == null)
                throw new WebDavNotFoundException();

            long docSize = doc.Size;
            if (docSize == 0)
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                context.Response.ContentLength64 = 0;
            }

            using (Stream stream = doc.OpenReadStream())
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;

                if (docSize > 0)
                    context.Response.ContentLength64 = docSize;

                byte[] buffer = new byte[4096];
                int inBuffer;
                while ((inBuffer = stream.Read(buffer, 0, buffer.Length)) > 0)
                    context.Response.OutputStream.Write(buffer, 0, inBuffer);
            }
            context.Response.Close();
        }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WebDavServer" /> class.
        /// </summary>
        /// <param name="store">The 
        /// <see cref="IWebDavStore" /> store object that will provide
        /// collections and documents for this 
        /// <see cref="WebDavServer" />.</param>
        /// <param name="listener">The 
        /// <see cref="IHttpListener" /> object that will handle the web server portion of
        /// the WebDAV server; or 
        /// <c>null</c> to use a fresh one.</param>
        /// <param name="methodHandlers">A collection of HTTP method handlers to use by this 
        /// <see cref="WebDavServer" />;
        /// or 
        /// <c>null</c> to use the built-in method handlers.</param>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="listener" /> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="System.ArgumentException"><para>
        ///   <paramref name="methodHandlers" /> is empty.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
        public WebDavServer(IWebDavStore store, IHttpListener listener = null, IEnumerable<IWebDavMethodHandler> methodHandlers = null)
        {
            if (store == null)
                throw new ArgumentNullException("store");
            if (listener == null)
            {
                listener = new HttpListenerAdapter();
                _ownsListener = true;
            }
            methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;

            IWebDavMethodHandler[] webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();

            if (!webDavMethodHandlers.Any())
                throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers");
            if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
                throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers");

            _listener = listener;
            _store = store;
            var handlersWithNames =
                from methodHandler in webDavMethodHandlers
                from name in methodHandler.Names
                select new
                {
                    name,
                    methodHandler
                };
            _methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);
            _log = LogManager.GetCurrentClassLogger();
            }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        /// <param name="response"></param>
        /// <param name="request"></param>
         protected override void OnProcessRequest(
           WebDavServer server,
           IHttpListenerContext context,
           IWebDavStore store,
           XmlDocument request,
           XmlDocument response)
        {
            if (context.Request.ContentLength64 > 0)
                throw new WebDavUnsupportedMediaTypeException();

            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);
                
            string collectionName = context.Request.Url.GetLastSegment();
            IWebDavStoreItem item;
            if ((item = collection.GetItemByName(collectionName)) != null)
            {
                WebDavServer.Log.WarnFormat("MKCOL Failed: item {0} already exists as child of {1}. ",
                    collectionName, collection.ItemPath);
                throw new WebDavMethodNotAllowedException();
            }
                
              
            collection.CreateCollection(collectionName);

            context.SendSimpleResponse((int)HttpStatusCode.Created);
        }
        /// <summary>
        /// Get the parent collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The parrent collection as an <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavConflictException">
        /// </exception>
        /// <exception cref="WebDavUnauthorizedException">When the user is unauthorized and doesn't have access</exception>
        /// <exception cref="WebDavConflictException">When the parent collection doesn't exist</exception>
        public static IWebDavStoreCollection GetParentCollection(
            WebDavServer server,
            IWebDavStore store,
            Uri childUri)
        {
            Uri parentCollectionUri = childUri.GetParentUri();
            IWebDavStoreCollection collection;

            try
            {
                collection = parentCollectionUri.GetItem(server, store) as IWebDavStoreCollection;
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException wex)
            {
                throw new WebDavConflictException(innerException: wex);
            }
            if (collection == null)
            {
                throw new WebDavConflictException(String.Format("Get parent collection return null. Uri: {0}", childUri));
            }

            //if (WebDavServer.Log.IsDebugEnabled)
            //{
            //    WebDavServer.Log.DebugFormat("GETPARENTCOLLECTION: uri {0} parenturi {1} return collection {2}",
            //        childUri, parentCollectionUri, GetFullItemPath(collection));
            //}

            return(collection);
        }
        /// <summary>
        /// Get the parent collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The parrent collection as an <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavConflictException">
        /// </exception>
        /// <exception cref="WebDavUnauthorizedException">When the user is unauthorized and doesn't have access</exception>
        /// <exception cref="WebDavConflictException">When the parent collection doesn't exist</exception>
        public static IWebDavStoreCollection GetParentCollection(
            WebDavServer server, 
            IWebDavStore store, 
            Uri childUri)
        {
            Uri parentCollectionUri = childUri.GetParentUri();
            IWebDavStoreCollection collection;
            try
            {
                collection = parentCollectionUri.GetItem(server, store) as IWebDavStoreCollection;
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException wex)
            {
                throw new WebDavConflictException(innerException : wex);
            }
            if (collection == null)
                throw new WebDavConflictException(String.Format("Get parent collection return null. Uri: {0}", childUri));

            //if (WebDavServer.Log.IsDebugEnabled)
            //{
            //    WebDavServer.Log.DebugFormat("GETPARENTCOLLECTION: uri {0} parenturi {1} return collection {2}",
            //        childUri, parentCollectionUri, GetFullItemPath(collection));
            //}
            
            return collection;
        }
        /// <summary>
        /// Moves the
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="sourceWebDavStoreItem">The <see cref="IWebDavStoreItem" /> that will be moved</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavForbiddenException">If the source path is the same as the destination path</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException">If one of the preconditions failed</exception>
        private void MoveItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
                              IWebDavStoreItem sourceWebDavStoreItem)
        {
            Uri destinationUri = GetDestinationHeader(context.Request);
            IWebDavStoreCollection destinationParentCollection = GetParentCollection(server, store, destinationUri);

            bool isNew = true;

            string           destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
            IWebDavStoreItem destination     = destinationParentCollection.GetItemByName(destinationName);

            if (destination != null)
            {
                if (sourceWebDavStoreItem.ItemPath == destination.ItemPath)
                {
                    throw new WebDavForbiddenException();
                }
                // if the overwrite header is F, statuscode = precondition failed
                if (!GetOverwriteHeader(context.Request))
                {
                    throw new WebDavPreconditionFailedException();
                }
                // else delete destination and set isNew to false
                destinationParentCollection.Delete(destination);
                isNew = false;
            }

            destinationParentCollection.MoveItemHere(sourceWebDavStoreItem, destinationName);

            // send correct response
            context.SendSimpleResponse(isNew ? HttpStatusCode.Created : HttpStatusCode.NoContent);
        }
Пример #13
0
        /// <summary>
        /// Copies the item.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="store">The store.</param>
        /// <param name="source">The source.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavForbiddenException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException"></exception>
        private static void CopyItem(IWebDavContext context, IWebDavStore store, IWebDavStoreItem source)
        {
            Uri destinationUri = GetDestinationHeader(context.Request);
            IWebDavStoreCollection destinationParentCollection = GetParentCollection(store, context, destinationUri);

            bool copyContent = (GetDepthHeader(context.Request) != 0);
            bool isNew       = true;

            string           destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
            IWebDavStoreItem destination     = destinationParentCollection.GetItemByName(destinationName);

            if (destination != null)
            {
                if (source.ItemPath == destination.ItemPath)
                {
                    throw new WebDavForbiddenException();
                }
                if (!GetOverwriteHeader(context.Request))
                {
                    throw new WebDavPreconditionFailedException();
                }
                if (destination is IWebDavStoreCollection)
                {
                    destinationParentCollection.Delete(destination);
                }
                isNew = false;
            }

            destinationParentCollection.CopyItemHere(source, destinationName, copyContent);

            var statusCode = isNew ? HttpStatusCode.Created : HttpStatusCode.NoContent;

            context.SetStatusCode(statusCode);
        }
Пример #14
0
 /// <summary>
 /// </summary>
 /// <param name="name"></param>
 /// <param name="listener"></param>
 /// <param name="handlers"></param>
 /// <param name="store"></param>
 /// <param name="server"></param>
 public HttpReciever(string name, ref IHttpListener listener, ref Dictionary<string, IWebDavMethodHandler> handlers, ref IWebDavStore store, WebDavServer server)
 {
     _listener = listener;
     _methodHandlers = handlers;
     _store = store;
     _server = server;
     _name = name;
 }
 /// <summary>
 /// Processes the request.
 /// </summary>
 /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
 /// <param name="context">The 
 /// <see cref="IHttpListenerContext" /> object containing both the request and response
 /// objects to use.</param>
 /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
 /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
 public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
 {            
     IWebDavStoreItem source = context.Request.Url.GetItem(server, store);
     if (source is IWebDavStoreDocument || source is IWebDavStoreCollection)
         CopyItem(server, context, store, source);
     else
         throw new WebDavMethodNotAllowedException();
 }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavLengthRequiredException">If the ContentLength header was not found</exception>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            // Get the parent collection
            IWebDavStoreCollection parentCollection = GetParentCollection(server, store, context.Request.Url);

            // Gets the item name from the url
            string itemName = context.Request.Url.GetLastSegment();

            IWebDavStoreItem     item = parentCollection.GetItemByName(itemName);
            IWebDavStoreDocument doc;

            if (item != null)
            {
                doc = item as IWebDavStoreDocument;
                if (doc == null)
                {
                    throw new WebDavMethodNotAllowedException();
                }
            }
            else
            {
                doc = parentCollection.CreateDocument(itemName);
            }

            Int64 contentLength = context.Request.ContentLength64;

            if (context.Request.ContentLength64 < 0)
            {
                var XLength = context.Request.Headers["x-expected-entity-length"];
                if (!Int64.TryParse(XLength, out contentLength))
                {
                    throw new WebDavLengthRequiredException();
                }
            }

            using (Stream stream = doc.OpenWriteStream(false))
            {
                long   left   = contentLength;
                byte[] buffer = new byte[4096];
                while (left > 0)
                {
                    int toRead   = Convert.ToInt32(Math.Min(left, buffer.Length));
                    int inBuffer = context.Request.InputStream.Read(buffer, 0, toRead);
                    stream.Write(buffer, 0, inBuffer);

                    left -= inBuffer;
                }
            }
            doc.FinishWriteOperation();

            context.SendSimpleResponse((int)HttpStatusCode.Created);
        }
 /// <summary>
 /// Processes the request.
 /// </summary>
 /// <param name="context">The 
 /// <see cref="IWebDavContext" /> object containing both the request and response
 /// objects to use.</param>
 /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
 public void ProcessRequest(IWebDavContext context, IWebDavStore store)
 {
     /***************************************************************************************************
     * Send the response
     ***************************************************************************************************/
     WindowsIdentity Identity = (WindowsIdentity)Thread.GetData(Thread.GetNamedDataSlot(Constants.HttpUser));
     var statusCode = WebDavStoreItemLock.UnLock(context.Request.Url, GetLockTokenHeader(context.Request), Identity.Name);
     context.SetStatusCode(statusCode);
 }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            /***************************************************************************************************
            * Send the response
            ***************************************************************************************************/
            WindowsIdentity Identity   = (WindowsIdentity)Thread.GetData(Thread.GetNamedDataSlot(Constants.HttpUser));
            var             statusCode = WebDavStoreItemLock.UnLock(context.Request.Url, GetLockTokenHeader(context.Request), Identity.Name);

            context.SetStatusCode(statusCode);
        }
 /// <summary>
 /// </summary>
 /// <param name="parentCollection"></param>
 /// <param name="path"></param>
 /// <param name="rootPath"></param>
 /// <param name="rootGuid"></param>
 /// <param name="store"></param>
 public WebDavSqlStoreCollection(IWebDavStoreCollection parentCollection, string path, String rootPath, Guid rootGuid, IWebDavStore store)
     : base(parentCollection, path, rootPath, rootGuid, store)
 {
     using (var context = new OnlineFilesEntities())
     {
         Folder f = context.Folders.AsNoTracking().FirstOrDefault(d => d.pk_FolderId == ObjectGuid);
         if (f != null)
             _creationDt = f.CreateDt;
     }
 }
        /// <summary>
        ///     Initializes a new instance of the <see cref="WebDavStoreItemBase" /> class.
        /// </summary>
        /// <param name="parentCollection">
        ///     The parent <see cref="IWebDavStoreCollection" /> that contains this
        ///     <see cref="IWebDavStoreItem" /> implementation.
        /// </param>
        /// <param name="name">The name of this <see cref="IWebDavStoreItem" /></param>
        /// <param name="store"></param>
        /// <exception cref="System.ArgumentNullException">name</exception>
        /// <exception cref="ArgumentNullException"><paramref name="name" /> is <c>null</c>.</exception>
        protected WebDavStoreItemBase(IWebDavStoreCollection parentCollection, string name, IWebDavStore store)
        {
            Store = store;
            if (IsNullOrWhiteSpace(name))
                throw new ArgumentNullException(nameof(name));

            ParentCollection = parentCollection;
            _name = name;
            UserIdentity = (WindowsIdentity)Thread.GetData(Thread.GetNamedDataSlot(WebDavServer.HttpUser));
        }
        ///// <summary>
        ///// Gets the prefix <see cref="Uri" /> that matches the specified <see cref="Uri" />.
        ///// </summary>
        ///// <param name="uri">The <see cref="Uri" /> to find the most specific prefix <see cref="Uri" /> for.</param>
        ///// <param name="context">Context</param>
        ///// <returns>
        ///// The most specific <see cref="Uri" /> for the given <paramref name="uri" />.
        ///// </returns>
        ///// <exception cref="WebDAVSharp.Server.Exceptions.WebDavInternalServerException">Unable to find correct server root</exception>
        ///// <exception cref="WebDavInternalServerException"><paramref name="uri" /> specifies a <see cref="Uri" /> that is not known to the <paramref name="server" />.</exception>
        //public static Uri GetPrefixUri(this Uri uri, IWebDavContext context)
        //{
        //    string.Format("{0}://{1}", context.Request.Url.Scheme, context.Request.Url.Authority);

        //    string url = uri.ToString();
        //    foreach (
        //        string prefix in
        //            server.Listener.Prefixes.Where(
        //                prefix => url.StartsWith(uri.ToString(), StringComparison.OrdinalIgnoreCase)))
        //        return new Uri(prefix);
        //    throw new WebDavInternalServerException("Unable to find correct server root");
        //}

        /// <summary>
        /// Retrieves a store item through the specified
        /// <see cref="Uri" /> from the
        /// specified
        /// <see cref="WebDavServer" /> and
        /// <see cref="IWebDavStore" />.
        /// </summary>
        /// <param name="uri">The <see cref="Uri" /> to retrieve the store item for.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> from which to retrieve the store item.</param>
        /// <param name="context">Context</param>
        /// <returns>
        /// The retrieved store item.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="uri" /> is <c>null</c>.</para>
        /// <para>
        ///   <paramref name="context" /> is <c>null</c>.</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If the item was not found.</exception>
        /// <exception cref="WebDavConflictException"><paramref name="uri" /> refers to a document in a collection, where the collection does not exist.</exception>
        /// <exception cref="WebDavNotFoundException"><paramref name="uri" /> refers to a document that does not exist.</exception>
        public static IWebDavStoreItem GetItem(this Uri uri, IWebDavStore store, IWebDavContext context)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            IWebDavStoreCollection collection = store.Root;

            IWebDavStoreItem item = null;

            if (context.Request.Url.Segments.Length - 1 == uri.Segments.Length)
            {
                return(collection);
            }

            //todo пути с глубиной больше рута глючат. надо дописать логику далее.

            for (int index = uri.Segments.Length; index < context.Request.Url.Segments.Length; index++)
            {
                string           segmentName = Uri.UnescapeDataString(context.Request.Url.Segments[index]);
                IWebDavStoreItem nextItem    = collection.GetItemByName(segmentName.TrimEnd('/', '\\'));
                if (nextItem == null)
                {
                    throw new WebDavNotFoundException(); //throw new WebDavConflictException();
                }
                if (index == context.Request.Url.Segments.Length - 1)
                {
                    item = nextItem;
                }
                else
                {
                    collection = nextItem as IWebDavStoreCollection;
                    if (collection == null)
                    {
                        throw new WebDavNotFoundException();
                    }
                }
            }

            if (item == null)
            {
                throw new WebDavNotFoundException();
            }

            return(item);
        }
 protected WebDavSqlStoreItem(IWebDavStoreCollection parentCollection, string path, String rootPath, Guid rootGuid, IWebDavStore store)
     : base(parentCollection, path, store)
 {
     if (IsNullOrWhiteSpace(path))
         throw new ArgumentNullException(nameof(path));
     RootGuid = rootGuid;
     RootPath = rootPath;
     _path = path;
     ObjectGuid = GetObjectGuid(path);
     GetFileInfo();
 }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            IWebDavStoreItem source = context.Request.Url.GetItem(server, store);

            MoveItem(server, context, store, source);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
           WebDavServer server,
           IHttpListenerContext context,
           IWebDavStore store,
           XmlDocument request,
           XmlDocument response)
        {
            IWebDavStoreItem source = context.Request.Url.GetItem(server, store);

            MoveItem(server, context, store, source);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            // Deletes the item
            collection.Delete(item);
        }
 public void ProcessRequest(
     WebDavServer server,
     IHttpListenerContext context,
     IWebDavStore store,
     out XmlDocument request,
     out XmlDocument response)
 {
     request  = new XmlDocument();
     response = new XmlDocument();
     OnProcessRequest(server, context, store, request, response);
 }
        /// <summary>
        /// Retrieves a store item through the specified
        /// <see cref="Uri" /> from the
        /// specified
        /// <see cref="WebDavServer" /> and
        /// <see cref="IWebDavStore" />.
        /// </summary>
        /// <param name="uri">The <see cref="Uri" /> to retrieve the store item for.</param>
        /// <param name="server">The <see cref="WebDavServer" /> that hosts the <paramref name="store" />.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> from which to retrieve the store item.</param>
        /// <returns>
        /// The retrieved store item.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="uri" /> is <c>null</c>.</para>
        /// <para>
        ///   <paramref name="server" /> is <c>null</c>.</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If the item was not found.</exception>
        /// <exception cref="WebDavConflictException"><paramref name="uri" /> refers to a document in a collection, where the collection does not exist.</exception>
        /// <exception cref="WebDavNotFoundException"><paramref name="uri" /> refers to a document that does not exist.</exception>
        public static IWebDavStoreItem GetItem(this Uri uri, WebDavServer server, IWebDavStore store)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            Uri prefixUri  = uri.GetPrefixUri(server);
            var collection = store.Root;

            IWebDavStoreItem item = null;

            if (prefixUri.Segments.Length == uri.Segments.Length)
            {
                return(collection);
            }

            for (var index = prefixUri.Segments.Length; index < uri.Segments.Length; index++)
            {
                var segmentName = Uri.UnescapeDataString(uri.Segments[index]);
                var nextItem    = collection.GetItemByName(segmentName.TrimEnd('/', '\\'));
                if (nextItem == null)
                {
                    throw new WebDavNotFoundException(); //throw new WebDavConflictException();
                }
                if (index == uri.Segments.Length - 1)
                {
                    item = nextItem;
                }
                else
                {
                    collection = nextItem as IWebDavStoreCollection;
                    if (collection == null)
                    {
                        throw new WebDavNotFoundException();
                    }
                }
            }

            if (item == null)
            {
                throw new WebDavNotFoundException();
            }

            return(item);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            // Deletes the item
            collection.Delete(item);
        }
        /// <summary>
        ///     Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">
        ///     The
        ///     <see cref="IHttpListenerContext" /> object containing both the request and response
        ///     objects to use.
        /// </param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            foreach (string verb in VerbsAllowed)
                context.Response.AppendHeader("Allow", verb);

            foreach (string verb in VerbsPublic)
                context.Response.AppendHeader("Public", verb);

            // Sends 200 OK
            context.SendSimpleResponse();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            // Deletes the item
            collection.Delete(item);
            context.SendSimpleResponse();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The 
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {

            foreach (string verb in verbsAllowed)
                context.Response.AppendHeader("Allow", verb);

            foreach (string verb in verbsPublic)
                context.Response.AppendHeader("Public", verb);

            // Sends 200 OK
            context.SetStatusCode();
        }
 /// <summary>
 ///     Processes the request.
 /// </summary>
 /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
 /// <param name="context">
 ///     The
 ///     <see cref="IHttpListenerContext" /> object containing both the request and response
 ///     objects to use.
 /// </param>
 /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
 public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
 {
     /***************************************************************************************************
     * Send the response
     ***************************************************************************************************/
     IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);
     // Get the item from the collection
     IWebDavStoreItem storeItem = GetItemFromCollection(collection, context.Request.Url);
     if (storeItem == null)
         throw new WebDavNotFoundException(context.Request.Url.ToString());
     var userIdentity = (WindowsIdentity) Thread.GetData(Thread.GetNamedDataSlot(WebDavServer.HttpUser));
     context.SendSimpleResponse(store.LockSystem.UnLock(storeItem, context.Request.GetLockTokenHeader(), userIdentity.Name));
 }
Пример #33
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            IWebDavStoreItem source = context.Request.Url.GetItem(store, context);

            if (source is IWebDavStoreDocument || source is IWebDavStoreCollection)
            {
                CopyItem(context, store, source);
            }
            else
            {
                throw new WebDavMethodNotAllowedException();
            }
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavLengthRequiredException">If the ContentLength header was not found</exception>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
           WebDavServer server,
           IHttpListenerContext context,
           IWebDavStore store,
           XmlDocument request,
           XmlDocument response)
        {
            // Get the parent collection
            IWebDavStoreCollection parentCollection = GetParentCollection(server, store, context.Request.Url);

            // Gets the item name from the url
            string itemName = context.Request.Url.GetLastSegment();

            IWebDavStoreItem item = parentCollection.GetItemByName(itemName);
            IWebDavStoreDocument doc;
            if (item != null)
            {
                doc = item as IWebDavStoreDocument;
                if (doc == null)
                    throw new WebDavMethodNotAllowedException();
            }
            else
            {
                doc = parentCollection.CreateDocument(itemName);
            }

            Int64 contentLength = context.Request.ContentLength64;
            if (context.Request.ContentLength64 < 0)
            {
                var XLength = context.Request.Headers["x-expected-entity-length"];
                if (!Int64.TryParse(XLength, out contentLength))
                    throw new WebDavLengthRequiredException();
            }

            using (Stream stream = doc.OpenWriteStream(false))
            {
                long left = contentLength;
                byte[] buffer = new byte[4096];
                while (left > 0)
                {
                    int toRead = Convert.ToInt32(Math.Min(left, buffer.Length));
                    int inBuffer = context.Request.InputStream.Read(buffer, 0, toRead);
                    stream.Write(buffer, 0, inBuffer);

                    left -= inBuffer;
                }
            }
            doc.FinishWriteOperation();

            context.SendSimpleResponse((int)HttpStatusCode.Created);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            /***************************************************************************************************
            * Send the response
            ***************************************************************************************************/

            context.SendSimpleResponse(HttpStatusCode.NoContent);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="store"></param>
        /// <param name="authtype"></param>
        /// <param name="methodHandlers"></param>
        public WebDavServer(IWebDavStore store, AuthType authtype, IEnumerable <IWebDavMethodHandler> methodHandlers = null)
        {
            _ownsListener = true;
            switch (authtype)
            {
            case AuthType.Basic:
                _listener = new HttpListenerBasicAdapter();
                break;

            case AuthType.Negotiate:
                _listener = new HttpListenerNegotiateAdapter();
                break;

            case AuthType.Anonymous:
                _listener = new HttpListenerAnyonymousAdapter();
                break;

            case AuthType.Smart:
                _listener = new HttpListenerSmartAdapter();
                break;
            }
            methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;

            IWebDavMethodHandler[] webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();

            if (!webDavMethodHandlers.Any())
            {
                throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers");
            }
            if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
            {
                throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers");
            }

            //_negotiateListener = listener;
            _store = store;
            var handlersWithNames =
                from methodHandler in webDavMethodHandlers
                from name in methodHandler.Names
                select new
            {
                name,
                methodHandler
            };

            _methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);

            UpdateDavHeader();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            List<string> verbsAllowed = new List<string> { "OPTIONS", "TRACE", "GET", "HEAD", "POST", "COPY", "PROPFIND", "LOCK", "UNLOCK" };

            List<string> verbsPublic = new List<string> { "OPTIONS", "GET", "HEAD", "PROPFIND", "PROPPATCH", "MKCOL", "PUT", "DELETE", "COPY", "MOVE", "LOCK", "UNLOCK" };

            foreach (string verb in verbsAllowed)
                context.Response.AppendHeader("Allow", verb);

            foreach (string verb in verbsPublic)
                context.Response.AppendHeader("Public", verb);

            // Sends 200 OK
            context.SendSimpleResponse();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            foreach (string verb in verbsAllowed)
            {
                context.Response.AppendHeader("Allow", verb);
            }

            foreach (string verb in verbsPublic)
            {
                context.Response.AppendHeader("Public", verb);
            }

            // Sends 200 OK
            context.SetStatusCode();
        }
Пример #39
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException"></exception>
        /// <exception cref="WebDavNotFoundException"><para>
        ///   <paramref name="context" /> specifies a request for a store item that does not exist.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="context" /> specifies a request for a store item that is not a document.</para></exception>
        /// <exception cref="WebDavConflictException"><paramref name="context" /> specifies a request for a store item using a collection path that does not exist.</exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);
            IWebDavStoreItem       item       = GetItemFromCollection(collection, context.Request.Url);
            IWebDavStoreDocument   doc        = item as IWebDavStoreDocument;

            if (doc == null)
            {
                throw new WebDavNotFoundException();
            }

            long docSize = doc.Size;

            if (docSize == 0)
            {
                context.Response.StatusCode      = (int)HttpStatusCode.OK;
                context.Response.ContentLength64 = 0;
            }

            using (Stream stream = doc.OpenReadStream())
            {
                if (stream == null)
                {
                    context.Response.StatusCode      = (int)HttpStatusCode.OK;
                    context.Response.ContentLength64 = 0;
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.OK;

                    //todo Это статика. Надо определять MIME тип по расширению
                    context.Response.AppendHeader("Content-type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

                    if (docSize > 0)
                    {
                        context.Response.ContentLength64 = docSize;
                    }

                    byte[] buffer = new byte[4096];
                    int    inBuffer;
                    while ((inBuffer = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        context.Response.OutputStream.Write(buffer, 0, inBuffer);
                    }
                    context.Response.OutputStream.Flush();
                }
            }
        }
        /// <summary>
        ///     Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">
        ///     The
        ///     <see cref="IHttpListenerContext" /> object containing both the request and response
        ///     objects to use.
        /// </param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            if (context.Request.ContentLength64 > 0)
                throw new WebDavUnsupportedMediaTypeException();

            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            string collectionName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));

            if (collection.GetItemByName(collectionName) != null)
                throw new WebDavMethodNotAllowedException();

            collection.CreateCollection(collectionName);

            context.SendSimpleResponse((int) HttpStatusCode.Created);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The 
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            if (context.Request.ContentLength64 > 0)
                throw new WebDavUnsupportedMediaTypeException();

            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);
                
            string collectionName = Uri.UnescapeDataString(
                context.Request.Url.Segments.Last().TrimEnd('/', '\\')
                );
            if (collection.GetItemByName(collectionName) != null)
                throw new WebDavMethodNotAllowedException();

            collection.CreateCollection(collectionName);

            context.SetStatusCode(HttpStatusCode.Created);
        }
Пример #42
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavLengthRequiredException">If the ContentLength header was not found</exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            // Get the parent collection
            IWebDavStoreCollection parentCollection = GetParentCollection(store, context, context.Request.Url);

            // Gets the item name from the url
            string itemName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));

            IWebDavStoreItem     item = parentCollection.GetItemByName(itemName);
            IWebDavStoreDocument doc;

            if (item != null)
            {
                doc = item as IWebDavStoreDocument;
                if (doc == null)
                {
                    throw new WebDavMethodNotAllowedException();
                }
            }
            else
            {
                doc = parentCollection.CreateDocument(itemName);
            }

            if (context.Request.ContentLength64 < 0)
            {
                throw new WebDavLengthRequiredException();
            }

            using (Stream stream = doc.OpenWriteStream(false))
            {
                long   left   = context.Request.ContentLength64;
                byte[] buffer = new byte[4096];
                while (left > 0)
                {
                    int toRead   = Convert.ToInt32(Math.Min(left, buffer.Length));
                    int inBuffer = context.Request.InputStream.Read(buffer, 0, toRead);
                    stream.Write(buffer, 0, inBuffer);

                    left -= inBuffer;
                }
            }

            context.SetStatusCode(HttpStatusCode.Created);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            IWebDavStoreItem source = context.Request.Url.GetItem(server, store);

            if (source is IWebDavStoreDocument || source is IWebDavStoreCollection)
            {
                CopyItem(server, context, store, source);
            }
            else
            {
                throw new WebDavMethodNotAllowedException();
            }
        }
Пример #44
0
        /// <summary>
        /// This constructor uses a Negotiate Listener if one isn't passed.
        ///
        /// Initializes a new instance of the <see cref="WebDavServer" /> class.
        /// </summary>
        /// <param name="store">The
        /// <see cref="IWebDavStore" /> store object that will provide
        /// collections and documents for this
        /// <see cref="WebDavServer" />.</param>
        /// <param name="listener">The
        /// <see cref="IHttpListener" /> object that will handle the web server portion of
        /// the WebDAV server; or
        /// <c>null</c> to use a fresh one.</param>
        /// <param name="methodHandlers">A collection of HTTP method handlers to use by this
        /// <see cref="WebDavServer" />;
        /// or
        /// <c>null</c> to use the built-in method handlers.</param>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="listener" /> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="System.ArgumentException"><para>
        ///   <paramref name="methodHandlers" /> is empty.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
        public WebDavServer(IWebDavStore store, IHttpListener listener = null, IEnumerable <IWebDavMethodHandler> methodHandlers = null)
        {
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            if (listener == null)
            {
                listener      = new HttpListenerNegotiateAdapter();
                _ownsListener = true;
            }

            _listener = listener;
            _store    = store;

            _webDavRequestProcessor = new WebDavRequestProcessor(store, methodHandlers);
        }
        /// <summary>
        /// </summary>
        /// <param name="parentCollection"></param>
        /// <param name="name"></param>
        /// <param name="rootPath"></param>
        /// <param name="rootGuid"></param>
        /// <param name="store"></param>
        public WebDavSqlStoreDocument(IWebDavStoreCollection parentCollection, string name, String rootPath, Guid rootGuid, IWebDavStore store)
            : base(parentCollection, name, rootPath, rootGuid, store)
        {
            using (var context = new OnlineFilesEntities())
            {
                File file = context.Files.AsNoTracking()
                    .Include(x => x.FileDatas)
                    .FirstOrDefault(d => d.pk_FileId == ObjectGuid && !d.IsDeleted);

                if (file == null)
                    throw new Exception("Non existant file.");
                _createDate = file.CreateDt;

                FileData lastmod = file.FileDatas.OrderByDescending(d => d.Revision).FirstOrDefault();

                _modificationDate = lastmod?.CreateDt ?? file.CreateDt;
                _filesize = lastmod?.Size ?? 1;
            }
        }
        /// <summary>
        ///     Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">
        ///     The
        ///     <see cref="IHttpListenerContext" /> object containing both the request and response
        ///     objects to use.
        /// </param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavLengthRequiredException">If the ContentLength header was not found</exception>
        public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            // Get the parent collection
            IWebDavStoreCollection parentCollection = GetParentCollection(server, store, context.Request.Url);

            // Gets the item name from the url
            string itemName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));

            IWebDavStoreItem item = parentCollection.GetItemByName(itemName);

            IWebDavStoreDocument doc;
            if (item != null)
            {
                doc = item as IWebDavStoreDocument;
                if (doc == null)
                    throw new WebDavMethodNotAllowedException();
            }
            else
            {
                doc = parentCollection.CreateDocument(itemName);
            }

            if (context.Request.ContentLength64 < 0)
                throw new WebDavLengthRequiredException();

            using (Stream stream = doc.OpenWriteStream(false))
            {
                long left = context.Request.ContentLength64;

                byte[] buffer = new byte[6000];

                while (left > 0)
                {
                    int toRead = Convert.ToInt32(Math.Min(left, buffer.Length));
                    int inBuffer = context.Request.InputStream.Read(buffer, 0, toRead);
                    stream.Write(buffer, 0, inBuffer);
                    left -= inBuffer;
                }
            }

            context.SendSimpleResponse((int) HttpStatusCode.Created);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        protected override void OnProcessRequest(
           WebDavServer server,
           IHttpListenerContext context,
           IWebDavStore store,
           XmlDocument request,
           XmlDocument response)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            // Deletes the item
            collection.Delete(item);
            //do not forget to clear all locks, if the object gets deleted there is no need to keep locks around.
            WebDavStoreItemLock.ClearLocks(context.Request.Url);

            context.SendSimpleResponse();
        }
Пример #48
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDavNotFoundException"><para>
        ///   <paramref name="context" /> specifies a request for a store item that does not exist.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="context" /> specifies a request for a store item that is not a document.</para></exception>
        /// <exception cref="WebDavConflictException"><paramref name="context" /> specifies a request for a store item using a collection path that does not exist.</exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            /***************************************************************************************************
            * Send the response
            ***************************************************************************************************/

            // HttpStatusCode doesn't contain WebDav status codes, but HttpWorkerRequest can handle these WebDav status codes
            context.SetStatusCode();

            // set the headers of the response
            context.Response.ContentLength64 = 0;
            context.Response.AppendHeader("Content-Type", "text/html");
            context.Response.AppendHeader("Last-Modified", item.ModificationDate.ToUniversalTime().ToString("R"));
        }
Пример #49
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            // Deletes the item
            collection.Delete(item);
            //do not forget to clear all locks, if the object gets deleted there is no need to keep locks around.
            WebDavStoreItemLock.ClearLocks(context.Request.Url);

            context.SendSimpleResponse();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The 
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDavNotFoundException"><para>
        ///   <paramref name="context" /> specifies a request for a store item that does not exist.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="context" /> specifies a request for a store item that is not a document.</para></exception>
        /// <exception cref="WebDavConflictException"><paramref name="context" /> specifies a request for a store item using a collection path that does not exist.</exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);

            // Get the item from the collection
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);

            /***************************************************************************************************
            * Send the response
            ***************************************************************************************************/
            
            // HttpStatusCode doesn't contain WebDav status codes, but HttpWorkerRequest can handle these WebDav status codes
            context.SetStatusCode();

            // set the headers of the response
            context.Response.ContentLength64 = 0;
            context.Response.AppendHeader("Content-Type", "text/html");
            context.Response.AppendHeader("Last-Modified", item.ModificationDate.ToUniversalTime().ToString("R"));
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The 
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
           WebDavServer server,
           IHttpListenerContext context,
           IWebDavStore store,
           XmlDocument request,
           XmlDocument response)
        {

            var baseVerbs = verbsAllowed.Aggregate((s1, s2) => s1 + ", " + s2);
            

            if (WebDavStoreItemLock.LockEnabled)
            {
                baseVerbs += ", LOCK, UNLOCK";
            }
            context.Response.AppendHeader("Content-Type", "text /html; charset=UTF-8");
            context.Response.AppendHeader("Allow", baseVerbs);
            context.Response.AppendHeader("Public", baseVerbs);

            context.Response.AppendHeader("Cache-Control", "no-cache");
            context.Response.AppendHeader("Pragma", "no-cache");
            context.Response.AppendHeader("Expires", "-1");
            context.Response.AppendHeader("Accept-Ranges", "bytes");
            context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
            context.Response.AppendHeader("Access-Control-Allow-Methods", "ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL");
            context.Response.AppendHeader("Access-Control-Allow-Headers", "Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If");
            context.Response.AppendHeader("X-Engine", ".NETWebDav");
            context.Response.AppendHeader("MS-Author-Via", "DAV");
            context.Response.AppendHeader("Access-Control-Max-Age", "2147483647");
            context.Response.AppendHeader("Public", "");

            //foreach (string verb in verbsAllowed)
            //    context.Response.AppendHeader("Allow", verb);

            //foreach (string verb in verbsPublic)
            //    context.Response.AppendHeader("Public", verb);

            // Sends 200 OK
            context.SendSimpleResponse();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            if (!WebDavStoreItemLock.LockEnabled)
            {
                throw new WebDavNotImplementedException("Lock support disabled");
            }

            /***************************************************************************************************
            * Send the response
            ***************************************************************************************************/
            WindowsIdentity Identity     = (WindowsIdentity)Thread.GetData(Thread.GetNamedDataSlot(WebDavServer.HttpUser));
            var             unlockResult = WebDavStoreItemLock.UnLock(context.Request.Url, GetLockTokenHeader(context.Request), Identity.Name);

            IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);

            try
            {
                var item = GetItemFromCollection(collection, context.Request.Url);
                if (item != null)
                {
                    //we already have an item
                    var resourceCanBeUnLocked = item.UnLock(Identity.Name);
                    if (!resourceCanBeUnLocked)
                    {
                        //TODO: decide what to do if the resource cannot be locked.
                    }
                }
            }
            catch (Exception ex)
            {
                WebDavServer.Log.Warn(
                    String.Format("Request unlock on a resource that does not exists: {0}", context.Request.Url), ex);
            }

            context.SendSimpleResponse(unlockResult);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException"></exception>
        /// <exception cref="WebDavNotFoundException"><para>
        ///   <paramref name="context" /> specifies a request for a store item that does not exist.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="context" /> specifies a request for a store item that is not a document.</para></exception>
        /// <exception cref="WebDavConflictException"><paramref name="context" /> specifies a request for a store item using a collection path that does not exist.</exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);
            IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);
            IWebDavStoreDocument doc = item as IWebDavStoreDocument;
            if (doc == null)
                throw new WebDavNotFoundException();

            long docSize = doc.Size;
            if (docSize == 0)
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                context.Response.ContentLength64 = 0;
            }

            using (Stream stream = doc.OpenReadStream())
            {
                if (stream == null)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.ContentLength64 = 0;
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.OK;

                    //todo Это статика. Надо определять MIME тип по расширению
                    context.Response.AppendHeader("Content-type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

                    if (docSize > 0)
                        context.Response.ContentLength64 = docSize;

                    byte[] buffer = new byte[4096];
                    int inBuffer;
                    while ((inBuffer = stream.Read(buffer, 0, buffer.Length)) > 0)
                        context.Response.OutputStream.Write(buffer, 0, inBuffer);
                    context.Response.OutputStream.Flush();
                }
            }
            
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="response"></param>
        /// <param name="request"></param>
        protected override void OnProcessRequest(
            WebDavServer server,
            IHttpListenerContext context,
            IWebDavStore store,
            XmlDocument request,
            XmlDocument response)
        {
            var baseVerbs = verbsAllowed.Aggregate((s1, s2) => s1 + ", " + s2);


            if (WebDavStoreItemLock.LockEnabled)
            {
                baseVerbs += ", LOCK, UNLOCK";
            }
            context.Response.AppendHeader("Content-Type", "text /html; charset=UTF-8");
            context.Response.AppendHeader("Allow", baseVerbs);
            context.Response.AppendHeader("Public", baseVerbs);

            context.Response.AppendHeader("Cache-Control", "no-cache");
            context.Response.AppendHeader("Pragma", "no-cache");
            context.Response.AppendHeader("Expires", "-1");
            context.Response.AppendHeader("Accept-Ranges", "bytes");
            context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
            context.Response.AppendHeader("Access-Control-Allow-Methods", "ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL");
            context.Response.AppendHeader("Access-Control-Allow-Headers", "Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If");
            context.Response.AppendHeader("X-Engine", ".NETWebDav");
            context.Response.AppendHeader("MS-Author-Via", "DAV");
            context.Response.AppendHeader("Access-Control-Max-Age", "2147483647");
            context.Response.AppendHeader("Public", "");

            //foreach (string verb in verbsAllowed)
            //    context.Response.AppendHeader("Allow", verb);

            //foreach (string verb in verbsPublic)
            //    context.Response.AppendHeader("Public", verb);

            // Sends 200 OK
            context.SendSimpleResponse();
        }
Пример #55
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The
        /// <see cref="IWebDavContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        public void ProcessRequest(IWebDavContext context, IWebDavStore store)
        {
            if (context.Request.ContentLength64 > 0)
            {
                throw new WebDavUnsupportedMediaTypeException();
            }

            IWebDavStoreCollection collection = GetParentCollection(store, context, context.Request.Url);

            string collectionName = Uri.UnescapeDataString(
                context.Request.Url.Segments.Last().TrimEnd('/', '\\')
                );

            if (collection.GetItemByName(collectionName) != null)
            {
                throw new WebDavMethodNotAllowedException();
            }

            collection.CreateCollection(collectionName);

            context.SetStatusCode(HttpStatusCode.Created);
        }
Пример #56
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WebDavServer" /> class.
        /// </summary>
        /// <param name="store">The
        /// <see cref="IWebDavStore" /> store object that will provide
        /// collections and documents for this
        /// <see cref="WebDavServer" />.</param>
        /// <param name="listener">The
        /// <see cref="IHttpListener" /> object that will handle the web server portion of
        /// the WebDAV server; or
        /// <c>null</c> to use a fresh one.</param>
        /// <param name="methodHandlers">A collection of HTTP method handlers to use by this
        /// <see cref="WebDavServer" />;
        /// or
        /// <c>null</c> to use the built-in method handlers.</param>
        /// <exception cref="System.ArgumentNullException"><para>
        ///   <paramref name="listener" /> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="store" /> is <c>null</c>.</para></exception>
        /// <exception cref="System.ArgumentException"><para>
        ///   <paramref name="methodHandlers" /> is empty.</para>
        /// <para>- or -</para>
        /// <para>
        ///   <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
        public WebDavServer(IWebDavStore store, IHttpListener listener = null, IEnumerable <IWebDavMethodHandler> methodHandlers = null)
        {
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }
            if (listener == null)
            {
                listener      = new HttpListenerAdapter();
                _ownsListener = true;
            }
            methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;

            var webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();

            if (!webDavMethodHandlers.Any())
            {
                throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers");
            }
            if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
            {
                throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers");
            }

            _listener = listener;
            _store    = store;
            var handlersWithNames =
                from methodHandler in webDavMethodHandlers
                from name in methodHandler.Names
                select new
            {
                name,
                methodHandler
            };

            _methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);
            //_log = LogManager.GetCurrentClassLogger();
            _log = LogManager.GetLogger(GetType().Name);
        }
Пример #57
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="store"></param>
        /// <param name="authtype"></param>
        /// <param name="methodHandlers"></param>
        public WebDavServer(IWebDavStore store, AuthType authtype, IEnumerable <IWebDavMethodHandler> methodHandlers = null)
        {
            _ownsListener = true;
            switch (authtype)
            {
            case AuthType.Basic:
                _listener = new HttpListenerBasicAdapter();
                break;

            case AuthType.Negotiate:
                _listener = new HttpListenerNegotiateAdapter();
                break;

            case AuthType.Anonymous:
                _listener = new HttpListenerAnyonymousAdapter();
                break;
            }

            //_negotiateListener = listener;
            _store = store;

            _webDavRequestProcessor = new WebDavRequestProcessor(store, methodHandlers);
        }
Пример #58
0
        /// <summary>
        /// Get the parent collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        /// <param name="context">Context</param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The parrent collection as an <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavConflictException">
        /// </exception>
        /// <exception cref="WebDavUnauthorizedException">When the user is unauthorized and doesn't have access</exception>
        /// <exception cref="WebDavConflictException">When the parent collection doesn't exist</exception>
        public static IWebDavStoreCollection GetParentCollection(IWebDavStore store, IWebDavContext context, Uri childUri)
        {
            Uri parentCollectionUri = childUri.GetParentUri();
            IWebDavStoreCollection collection;

            try
            {
                collection = parentCollectionUri.GetItem(store, context) as IWebDavStoreCollection;
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException)
            {
                throw new WebDavConflictException();
            }
            if (collection == null)
            {
                throw new WebDavConflictException();
            }

            return(collection);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            // Get the parent collection of the item
            var collection = GetParentCollection(server, store, context.Request.Url);

            // Get the item from the collection
            var item = GetItemFromCollection(collection, context.Request.Url);

            // Deletes the item
            collection.Delete(item);
            context.SendSimpleResponse();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
        /// <param name="context">The
        /// <see cref="IHttpListenerContext" /> object containing both the request and response
        /// objects to use.</param>
        /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
        public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
        {
            var source = context.Request.Url.GetItem(server, store);

            MoveItem(server, context, store, source);
        }