WebDAVMoveMethodHandler.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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>MOVE</c> HTTP method for WebDAV#.
  12. /// </summary>
  13. internal class WebDavMoveMethodHandler : 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. "MOVE"
  25. };
  26. #endregion
  27. #region 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. public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  39. {
  40. MoveItem(server, context, store, context.Request.Url.GetItem(server, store));
  41. }
  42. /// <summary>
  43. /// Moves the
  44. /// </summary>
  45. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  46. /// <param name="context">
  47. /// The
  48. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  49. /// objects to use.
  50. /// </param>
  51. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  52. /// <param name="sourceWebDavStoreItem">The <see cref="IWebDavStoreItem" /> that will be moved</param>
  53. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavForbiddenException">
  54. /// If the source path is the same as the
  55. /// destination path
  56. /// </exception>
  57. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException">If one of the preconditions failed</exception>
  58. private static void MoveItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
  59. IWebDavStoreItem sourceWebDavStoreItem)
  60. {
  61. Uri destinationUri = GetDestinationHeader(context.Request);
  62. IWebDavStoreCollection destinationParentCollection = GetParentCollection(server, store, destinationUri);
  63. bool isNew = true;
  64. string destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
  65. IWebDavStoreItem destination = destinationParentCollection.GetItemByName(destinationName);
  66. if (destination != null)
  67. {
  68. if (sourceWebDavStoreItem.ItemPath == destination.ItemPath)
  69. throw new WebDavForbiddenException();
  70. // if the overwrite header is F, statuscode = precondition failed
  71. if (!GetOverwriteHeader(context.Request))
  72. throw new WebDavPreconditionFailedException();
  73. // else delete destination and set isNew to false
  74. destinationParentCollection.Delete(destination);
  75. isNew = false;
  76. }
  77. destinationParentCollection.MoveItemHere(sourceWebDavStoreItem, destinationName);
  78. // send correct response
  79. context.SendSimpleResponse(isNew ? (int) HttpStatusCode.Created : (int) HttpStatusCode.NoContent);
  80. }
  81. #endregion
  82. }
  83. }