/// <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 = destinationUri.GetLastSegment();
            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 ? (int)HttpStatusCode.Created : (int)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);
        }
        /// <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);
        }
Пример #4
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);
        }
Пример #5
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);
        }
        /// <summary>
        /// Get the item in the collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="collection">The parent collection as a <see cref="IWebDavStoreCollection" /></param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The <see cref="IWebDavStoreItem" /> from the <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If user is not authorized to get access to the item</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If item not found.</exception>
        public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
        {
            IWebDavStoreItem item;
            String           name = null;

            try
            {
                name = childUri.GetLastSegment();
                item = collection.GetItemByName(name);
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException wex)
            {
                throw new WebDavNotFoundException(String.Format("Cannot found name {0} from uri {1} father {2}", name, childUri, collection.ItemPath), wex);
            }
            if (item == null)
            {
                throw new WebDavNotFoundException(String.Format("Cannot found name {0} from uri {1} father {2}", name, childUri, collection.ItemPath));
            }

            return(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>
        /// <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>
        ///     Get the item in the collection from the requested
        ///     <see cref="Uri" />.
        ///     <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="collection">The parent collection as a <see cref="IWebDavStoreCollection" /></param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        ///     The <see cref="IWebDavStoreItem" /> from the <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">
        ///     If user is not authorized to get access to
        ///     the item
        /// </exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If item not found.</exception>
        public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
        {
            IWebDavStoreItem item = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\')));
            if (item == null)
                throw new WebDavNotFoundException();
            item.Href = childUri;

            return item;
        }
        ///// <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);
        }
Пример #10
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);
            }

            for (int index = prefixUri.Segments.Length; index < uri.Segments.Length; index++)
            {
                string           segmentName = Uri.UnescapeDataString(uri.Segments[index]);
                IWebDavStoreItem 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);
        }
Пример #11
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="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(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[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.SendSimpleResponse(HttpStatusCode.Created);
        }
        /// <summary>
        /// Get the item in the collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="collection">The parent collection as a <see cref="IWebDavStoreCollection" /></param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The <see cref="IWebDavStoreItem" /> from the <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If user is not authorized to get access to the item</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If item not found.</exception>
        public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
        {
            IWebDavStoreItem item;
            try
            {
                item = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\')));
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException)
            {
                throw new WebDavNotFoundException();
            }
            if (item == null)
                throw new WebDavNotFoundException();

            return 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>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
        public 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(HttpStatusCode.Created);
        }
Пример #14
0
        /// <summary>
        /// Get the item in the collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="collection">The parent collection as a <see cref="IWebDavStoreCollection" /></param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The <see cref="IWebDavStoreItem" /> from the <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If user is not authorized to get access to the item</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If item not found.</exception>
        public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
        {
            IWebDavStoreItem item;

            try
            {
                item = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\')));
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException)
            {
                throw new WebDavNotFoundException();
            }
            if (item == null)
            {
                throw new WebDavNotFoundException();
            }

            return(item);
        }
        /// <summary>
        /// Get the item in the collection from the requested
        /// <see cref="Uri" />.
        /// <see cref="WebDavException" /> 409 Conflict possible.
        /// </summary>
        /// <param name="collection">The parent collection as a <see cref="IWebDavStoreCollection" /></param>
        /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
        /// <returns>
        /// The <see cref="IWebDavStoreItem" /> from the <see cref="IWebDavStoreCollection" />
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If user is not authorized to get access to the item</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If item not found.</exception>
        public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
        {
            IWebDavStoreItem item;
            String name = null;
            try
            {
                name = childUri.GetLastSegment(); 
                item = collection.GetItemByName(name);
            }
            catch (UnauthorizedAccessException)
            {
                throw new WebDavUnauthorizedException();
            }
            catch (WebDavNotFoundException wex)
            {
                throw new WebDavNotFoundException(String.Format("Cannot found name {0} from uri {1} father {2}", name, childUri, collection.ItemPath), wex);
            }
            if (item == null)
                throw new WebDavNotFoundException(String.Format("Cannot found name {0} from uri {1} father {2}", name, childUri, collection.ItemPath));

            return item;
        }