WebDAVMoveMethodHandler.cs 3.9 KB

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