using System; using System.Collections.Generic; using System.Linq; using System.Net; using WebDAVSharp.Server.Adapters; using WebDAVSharp.Server.Exceptions; using WebDAVSharp.Server.Stores; namespace WebDAVSharp.Server.MethodHandlers { /// /// This class implements the COPY HTTP method for WebDAV#. /// internal class WebDavCopyMethodHandler : WebDavMethodHandlerBase { #region Properties /// /// Gets the collection of the names of the HTTP methods handled by this instance. /// /// /// The names. /// public override IEnumerable Names => new[] { "COPY" }; #endregion #region Public Functions /// /// Processes the request. /// /// The through which the request came in from the client. /// /// The /// object containing both the request and response /// objects to use. /// /// The that the is hosting. /// public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store) { IWebDavStoreItem source = context.Request.Url.GetItem(server, store); CopyItem(server, context, store, source); } /// /// Copies the item. /// /// The server. /// The context. /// The store. /// The source. /// /// 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 ? (int) HttpStatusCode.Created : (int) HttpStatusCode.NoContent); } #endregion } }