using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters;
using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions;
using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers
{
///
/// This class implements the COPY HTTP method for WebDAV#.
///
internal class WebDavCopyMethodHandler : WebDavMethodHandlerBase, IWebDavMethodHandler
{
///
/// Gets the collection of the names of the HTTP methods handled by this instance.
///
///
/// The names.
///
public IEnumerable Names => new[]
{
"COPY"
};
///
/// 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 void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
{
var source = context.Request.Url.GetItem(server, store);
if (source is IWebDavStoreDocument || source is IWebDavStoreCollection)
CopyItem(server, context, store, source);
else
throw new WebDavMethodNotAllowedException();
}
///
/// Copies the item.
///
/// The server.
/// The context.
/// The store.
/// The source.
///
///
private static void CopyItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
IWebDavStoreItem source)
{
var destinationUri = GetDestinationHeader(context.Request);
var destinationParentCollection = GetParentCollection(server, store, destinationUri);
var copyContent = GetDepthHeader(context.Request) != 0;
var isNew = true;
var destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
var 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);
}
}
}