WebDAVMethodHandlerBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using WebDAVSharp.Server.Adapters;
  5. using WebDAVSharp.Server.Exceptions;
  6. using WebDAVSharp.Server.Stores;
  7. using static System.String;
  8. namespace WebDAVSharp.Server.MethodHandlers
  9. {
  10. /// <summary>
  11. /// This is the base class for <see cref="IWebDavMethodHandler" /> implementations.
  12. /// </summary>
  13. public abstract class WebDavMethodHandlerBase : IWebDavMethodHandler
  14. {
  15. #region Variables
  16. private const int DepthInfinity = -1;
  17. //public WindowsIdentity UserIdentity;
  18. #endregion
  19. /// <summary>
  20. /// </summary>
  21. public abstract IEnumerable<string> Names { get; }
  22. /// <summary>
  23. /// </summary>
  24. /// <param name="server"></param>
  25. /// <param name="context"></param>
  26. /// <param name="store"></param>
  27. public abstract void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store);
  28. #region Static Functions
  29. /// <summary>
  30. /// Get the parent collection from the requested
  31. /// <see cref="Uri" />.
  32. /// <see cref="WebDavException" /> 409 Conflict possible.
  33. /// </summary>
  34. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  35. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  36. /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
  37. /// <returns>
  38. /// The parrent collection as an <see cref="IWebDavStoreCollection" />
  39. /// </returns>
  40. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException"></exception>
  41. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavConflictException">
  42. /// </exception>
  43. /// <exception cref="WebDavUnauthorizedException">When the user is unauthorized and doesn't have access</exception>
  44. /// <exception cref="WebDavConflictException">When the parent collection doesn't exist</exception>
  45. public static IWebDavStoreCollection GetParentCollection(WebDavServer server, IWebDavStore store, Uri childUri)
  46. {
  47. Uri parentCollectionUri = childUri.GetParentUri();
  48. return parentCollectionUri.GetItem(server, store) as IWebDavStoreCollection;
  49. }
  50. /// <summary>
  51. /// Get the item in the collection from the requested
  52. /// <see cref="Uri" />.
  53. /// <see cref="WebDavException" /> 409 Conflict possible.
  54. /// </summary>
  55. /// <param name="collection">The parent collection as a <see cref="IWebDavStoreCollection" /></param>
  56. /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
  57. /// <returns>
  58. /// The <see cref="IWebDavStoreItem" /> from the <see cref="IWebDavStoreCollection" />
  59. /// </returns>
  60. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">
  61. /// If user is not authorized to get access to
  62. /// the item
  63. /// </exception>
  64. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If item not found.</exception>
  65. public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
  66. {
  67. IWebDavStoreItem item = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\')));
  68. if (item == null)
  69. throw new WebDavNotFoundException();
  70. item.Href = childUri;
  71. return item;
  72. }
  73. /// <summary>
  74. /// Gets the Depth header : 0, 1 or infinity
  75. /// </summary>
  76. /// <param name="request">The <see cref="IHttpListenerContext" /> with the response included</param>
  77. /// <returns>
  78. /// The values 0, 1 or -1 (for infinity)
  79. /// </returns>
  80. public static int GetDepthHeader(IHttpListenerRequest request)
  81. {
  82. // get the value of the depth header as a string
  83. string depth = request.Headers["Depth"];
  84. // check if the string is valid or not infinity
  85. // if so, try to parse it to an int
  86. if (IsNullOrEmpty(depth) || depth.Equals("infinity"))
  87. return DepthInfinity;
  88. int value;
  89. if (!int.TryParse(depth, out value))
  90. return DepthInfinity;
  91. if (value == 0 || value == 1)
  92. return value;
  93. // else, return the infinity value
  94. return DepthInfinity;
  95. }
  96. /// <summary>
  97. /// Gets the Overwrite header : T or F
  98. /// </summary>
  99. /// <param name="request">The <see cref="IHttpListenerRequest" /> has the header included</param>
  100. /// <returns>The <see cref="bool" /> true if overwrite, false if no overwrite</returns>
  101. public static bool GetOverwriteHeader(IHttpListenerRequest request)
  102. {
  103. // get the value of the Overwrite header as a string
  104. string overwrite = request.Headers["Overwrite"];
  105. // check if the string is valid and if it equals T
  106. return overwrite != null && overwrite.Equals("T");
  107. // else, return false
  108. }
  109. /// <summary>
  110. /// Gets the Destination header as an URI
  111. /// </summary>
  112. /// <param name="request">The <see cref="IHttpListenerRequest" /> has the header included</param>
  113. /// <returns>The <see cref="Uri" /> containing the destination</returns>
  114. public static Uri GetDestinationHeader(IHttpListenerRequest request)
  115. {
  116. // get the value of the Destination header as a string
  117. string destinationUri = request.Headers["Destination"];
  118. // check if the string is valid
  119. if (!IsNullOrEmpty(destinationUri))
  120. return new Uri(destinationUri);
  121. // else, throw exception
  122. throw new WebDavConflictException();
  123. }
  124. #endregion
  125. }
  126. }