123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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
- {
- /// <summary>
- /// This class implements the <c>COPY</c> HTTP method for WebDAV#.
- /// </summary>
- internal class WebDavCopyMethodHandler : WebDavMethodHandlerBase, IWebDavMethodHandler
- {
- /// <summary>
- /// Gets the collection of the names of the HTTP methods handled by this instance.
- /// </summary>
- /// <value>
- /// The names.
- /// </value>
- public IEnumerable<string> Names => new[]
- {
- "COPY"
- };
- /// <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="WebDavMethodNotAllowedException"></exception>
- 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();
- }
- /// <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="WebDavForbiddenException"></exception>
- /// <exception cref="WebDavPreconditionFailedException"></exception>
- 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);
- }
- }
- }
|