WebDAVCopyMethodHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters;
  6. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions;
  7. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
  8. namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers
  9. {
  10. /// <summary>
  11. /// This class implements the <c>COPY</c> HTTP method for WebDAV#.
  12. /// </summary>
  13. internal class WebDavCopyMethodHandler : WebDavMethodHandlerBase, IWebDavMethodHandler
  14. {
  15. /// <summary>
  16. /// Gets the collection of the names of the HTTP methods handled by this instance.
  17. /// </summary>
  18. /// <value>
  19. /// The names.
  20. /// </value>
  21. public IEnumerable<string> Names => new[]
  22. {
  23. "COPY"
  24. };
  25. /// <summary>
  26. /// Processes the request.
  27. /// </summary>
  28. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  29. /// <param name="context">
  30. /// The
  31. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  32. /// objects to use.
  33. /// </param>
  34. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  35. /// <exception cref="WebDavMethodNotAllowedException"></exception>
  36. public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  37. {
  38. var source = context.Request.Url.GetItem(server, store);
  39. if (source is IWebDavStoreDocument || source is IWebDavStoreCollection)
  40. CopyItem(server, context, store, source);
  41. else
  42. throw new WebDavMethodNotAllowedException();
  43. }
  44. /// <summary>
  45. /// Copies the item.
  46. /// </summary>
  47. /// <param name="server">The server.</param>
  48. /// <param name="context">The context.</param>
  49. /// <param name="store">The store.</param>
  50. /// <param name="source">The source.</param>
  51. /// <exception cref="WebDavForbiddenException"></exception>
  52. /// <exception cref="WebDavPreconditionFailedException"></exception>
  53. private static void CopyItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
  54. IWebDavStoreItem source)
  55. {
  56. var destinationUri = GetDestinationHeader(context.Request);
  57. var destinationParentCollection = GetParentCollection(server, store, destinationUri);
  58. var copyContent = GetDepthHeader(context.Request) != 0;
  59. var isNew = true;
  60. var destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
  61. var destination = destinationParentCollection.GetItemByName(destinationName);
  62. if (destination != null)
  63. {
  64. if (source.ItemPath == destination.ItemPath)
  65. throw new WebDavForbiddenException();
  66. if (!GetOverwriteHeader(context.Request))
  67. throw new WebDavPreconditionFailedException();
  68. if (destination is IWebDavStoreCollection)
  69. destinationParentCollection.Delete(destination);
  70. isNew = false;
  71. }
  72. destinationParentCollection.CopyItemHere(source, destinationName, copyContent);
  73. context.SendSimpleResponse(isNew ? HttpStatusCode.Created : HttpStatusCode.NoContent);
  74. }
  75. }
  76. }