WebDAVMethodHandlerBase.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Linq;
  3. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters;
  4. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions;
  5. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
  6. namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers
  7. {
  8. /// <summary>
  9. /// This is the base class for <see cref="IWebDavMethodHandler" /> implementations.
  10. /// </summary>
  11. internal abstract class WebDavMethodHandlerBase
  12. {
  13. private const int DepthInfinity = -1;
  14. /// <summary>
  15. /// Get the parent collection from the requested
  16. /// <see cref="Uri" />.
  17. /// <see cref="WebDavException" /> 409 Conflict possible.
  18. /// </summary>
  19. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  20. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  21. /// <param name="childUri">The <see cref="Uri" /> object containing the specific location of the child</param>
  22. /// <returns>
  23. /// The parrent collection as an <see cref="IWebDavStoreCollection" />
  24. /// </returns>
  25. /// <exception cref="WebDavUnauthorizedException"></exception>
  26. /// <exception cref="WebDavConflictException">
  27. /// </exception>
  28. /// <exception cref="WebDavUnauthorizedException">When the user is unauthorized and doesn't have access</exception>
  29. /// <exception cref="WebDavConflictException">When the parent collection doesn't exist</exception>
  30. public static IWebDavStoreCollection GetParentCollection(WebDavServer server, IWebDavStore store, Uri childUri)
  31. {
  32. var parentCollectionUri = childUri.GetParentUri();
  33. IWebDavStoreCollection collection;
  34. try
  35. {
  36. collection = parentCollectionUri.GetItem(server, store) as IWebDavStoreCollection;
  37. }
  38. catch (UnauthorizedAccessException)
  39. {
  40. throw new WebDavUnauthorizedException();
  41. }
  42. catch (WebDavNotFoundException)
  43. {
  44. throw new WebDavConflictException();
  45. }
  46. if (collection == null)
  47. throw new WebDavConflictException();
  48. return collection;
  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="WebDavUnauthorizedException">
  61. /// If user is not authorized to get access to
  62. /// the item
  63. /// </exception>
  64. /// <exception cref="WebDavNotFoundException">If item not found.</exception>
  65. public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri)
  66. {
  67. IWebDavStoreItem item;
  68. try
  69. {
  70. item = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\')));
  71. }
  72. catch (UnauthorizedAccessException)
  73. {
  74. throw new WebDavUnauthorizedException();
  75. }
  76. catch (WebDavNotFoundException)
  77. {
  78. throw new WebDavNotFoundException();
  79. }
  80. if (item == null)
  81. throw new WebDavNotFoundException();
  82. return item;
  83. }
  84. /// <summary>
  85. /// Gets the Depth header : 0, 1 or infinity
  86. /// </summary>
  87. /// <param name="request">The <see cref="IHttpListenerContext" /> with the response included</param>
  88. /// <returns>
  89. /// The values 0, 1 or -1 (for infinity)
  90. /// </returns>
  91. public static int GetDepthHeader(IHttpListenerRequest request)
  92. {
  93. // get the value of the depth header as a string
  94. var depth = request.Headers["Depth"];
  95. // check if the string is valid or not infinity
  96. // if so, try to parse it to an int
  97. if (string.IsNullOrEmpty(depth) || depth.Equals("infinity"))
  98. return DepthInfinity;
  99. int value;
  100. if (!int.TryParse(depth, out value))
  101. return DepthInfinity;
  102. if (value == 0 || value == 1)
  103. return value;
  104. // else, return the infinity value
  105. return DepthInfinity;
  106. }
  107. /// <summary>
  108. /// Gets the Overwrite header : T or F
  109. /// </summary>
  110. /// <param name="request">The <see cref="IHttpListenerRequest" /> has the header included</param>
  111. /// <returns>The <see cref="bool" /> true if overwrite, false if no overwrite</returns>
  112. public static bool GetOverwriteHeader(IHttpListenerRequest request)
  113. {
  114. // get the value of the Overwrite header as a string
  115. var overwrite = request.Headers["Overwrite"];
  116. // check if the string is valid and if it equals T
  117. return overwrite != null && overwrite.Equals("T");
  118. // else, return false
  119. }
  120. /// <summary>
  121. /// Gets the Timeout header : Second-number
  122. /// </summary>
  123. /// <param name="request">The request with the request included</param>
  124. /// <returns>The value of the Timeout header as a string</returns>
  125. public static string GetTimeoutHeader(IHttpListenerRequest request)
  126. {
  127. // get the value of the timeout header as a string
  128. var timeout = request.Headers["Timeout"];
  129. // check if the string is valid or not infinity
  130. // if so, try to parse it to an int
  131. if (!string.IsNullOrEmpty(timeout) && !timeout.Equals("infinity") &&
  132. !timeout.Equals("Infinite, Second-4100000000"))
  133. return timeout;
  134. // else, return the timeout value as if it was requested to be 4 days
  135. return "Second-345600";
  136. }
  137. /// <summary>
  138. /// Gets the Destination header as an URI
  139. /// </summary>
  140. /// <param name="request">The <see cref="IHttpListenerRequest" /> has the header included</param>
  141. /// <returns>The <see cref="Uri" /> containing the destination</returns>
  142. public static Uri GetDestinationHeader(IHttpListenerRequest request)
  143. {
  144. // get the value of the Destination header as a string
  145. var destinationUri = request.Headers["Destination"];
  146. // check if the string is valid
  147. if (!string.IsNullOrEmpty(destinationUri))
  148. return new Uri(destinationUri);
  149. // else, throw exception
  150. throw new WebDavConflictException();
  151. }
  152. }
  153. }