WebDAVCopyMethodHandler.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using WebDAVSharp.Server.Adapters;
  6. using WebDAVSharp.Server.Exceptions;
  7. using WebDAVSharp.Server.Stores;
  8. namespace WebDAVSharp.Server.MethodHandlers
  9. {
  10. /// <summary>
  11. /// This class implements the <c>COPY</c> HTTP method for WebDAV#.
  12. /// </summary>
  13. internal class WebDavCopyMethodHandler : WebDavMethodHandlerBase
  14. {
  15. #region Properties
  16. /// <summary>
  17. /// Gets the collection of the names of the HTTP methods handled by this instance.
  18. /// </summary>
  19. /// <value>
  20. /// The names.
  21. /// </value>
  22. public override IEnumerable<string> Names => new[]
  23. {
  24. "COPY"
  25. };
  26. #endregion
  27. #region Public Functions
  28. /// <summary>
  29. /// Processes the request.
  30. /// </summary>
  31. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  32. /// <param name="context">
  33. /// The
  34. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  35. /// objects to use.
  36. /// </param>
  37. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  38. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
  39. public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  40. {
  41. IWebDavStoreItem source = context.Request.Url.GetItem(server, store);
  42. CopyItem(server, context, store, source);
  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="WebDAVSharp.Server.Exceptions.WebDavForbiddenException"></exception>
  52. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException"></exception>
  53. private static void CopyItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
  54. IWebDavStoreItem source)
  55. {
  56. Uri destinationUri = GetDestinationHeader(context.Request);
  57. IWebDavStoreCollection destinationParentCollection = GetParentCollection(server, store, destinationUri);
  58. bool copyContent = (GetDepthHeader(context.Request) != 0);
  59. bool isNew = true;
  60. string destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
  61. IWebDavStoreItem 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 ? (int) HttpStatusCode.Created : (int) HttpStatusCode.NoContent);
  74. }
  75. #endregion
  76. }
  77. }