WebDAVExtensions.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Web;
  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
  9. {
  10. /// <summary>
  11. /// This class holds extension methods for various types related to WebDAV#.
  12. /// </summary>
  13. internal static class WebDavExtensions
  14. {
  15. /// <summary>
  16. /// Gets the Uri to the parent object.
  17. /// </summary>
  18. /// <param name="uri">The <see cref="Uri" /> of a resource, for which the parent Uri should be retrieved.</param>
  19. /// <returns>
  20. /// The parent <see cref="Uri" />.
  21. /// </returns>
  22. /// <exception cref="System.ArgumentNullException">uri</exception>
  23. /// <exception cref="System.InvalidOperationException">Cannot get parent of root</exception>
  24. /// <exception cref="ArgumentNullException"><paramref name="uri" /> is <c>null</c>.</exception>
  25. /// <exception cref="InvalidOperationException"><paramref name="uri" /> has no parent, it refers to a root resource.</exception>
  26. public static Uri GetParentUri(this Uri uri)
  27. {
  28. if (uri == null)
  29. throw new ArgumentNullException(nameof(uri));
  30. if (uri.Segments.Length == 1)
  31. throw new InvalidOperationException("Cannot get parent of root");
  32. var url = uri.ToString();
  33. var index = url.Length - 1;
  34. if (url[index] == '/')
  35. index--;
  36. while (url[index] != '/')
  37. index--;
  38. return new Uri(url.Substring(0, index + 1));
  39. }
  40. /// <summary>
  41. /// Sends a simple response with a specified HTTP status code but no content.
  42. /// </summary>
  43. /// <param name="context">The <see cref="IHttpListenerContext" /> to send the response through.</param>
  44. /// <param name="statusCode">The HTTP status code for the response.</param>
  45. /// <exception cref="System.ArgumentNullException">context</exception>
  46. /// <exception cref="ArgumentNullException"><paramref name="context" /> is <c>null</c>.</exception>
  47. public static void SendSimpleResponse(this IHttpListenerContext context,
  48. HttpStatusCode statusCode = HttpStatusCode.OK)
  49. {
  50. if (context == null)
  51. throw new ArgumentNullException(nameof(context));
  52. context.Response.StatusCode = (int) statusCode;
  53. context.Response.StatusDescription = HttpWorkerRequest.GetStatusDescription((int) statusCode);
  54. context.Response.Close();
  55. }
  56. /// <summary>
  57. /// Gets the prefix <see cref="Uri" /> that matches the specified <see cref="Uri" />.
  58. /// </summary>
  59. /// <param name="uri">The <see cref="Uri" /> to find the most specific prefix <see cref="Uri" /> for.</param>
  60. /// <param name="server">
  61. /// The
  62. /// <see cref="WebDavServer" /> that hosts the WebDAV server and holds the collection
  63. /// of known prefixes.
  64. /// </param>
  65. /// <returns>
  66. /// The most specific <see cref="Uri" /> for the given <paramref name="uri" />.
  67. /// </returns>
  68. /// <exception cref="WebDavInternalServerException">Unable to find correct server root</exception>
  69. /// <exception cref="WebDavInternalServerException">
  70. /// <paramref name="uri" /> specifies a <see cref="Uri" /> that is not
  71. /// known to the <paramref name="server" />.
  72. /// </exception>
  73. public static Uri GetPrefixUri(this Uri uri, WebDavServer server)
  74. {
  75. var url = uri.ToString();
  76. foreach (
  77. var prefix in
  78. server.Listener.Prefixes.Where(
  79. prefix => url.StartsWith(uri.ToString(), StringComparison.OrdinalIgnoreCase)))
  80. return new Uri(prefix);
  81. throw new WebDavInternalServerException("Unable to find correct server root");
  82. }
  83. /// <summary>
  84. /// Retrieves a store item through the specified
  85. /// <see cref="Uri" /> from the
  86. /// specified
  87. /// <see cref="WebDavServer" /> and
  88. /// <see cref="IWebDavStore" />.
  89. /// </summary>
  90. /// <param name="uri">The <see cref="Uri" /> to retrieve the store item for.</param>
  91. /// <param name="server">The <see cref="WebDavServer" /> that hosts the <paramref name="store" />.</param>
  92. /// <param name="store">The <see cref="IWebDavStore" /> from which to retrieve the store item.</param>
  93. /// <returns>
  94. /// The retrieved store item.
  95. /// </returns>
  96. /// <exception cref="System.ArgumentNullException">
  97. /// <para>
  98. /// <paramref name="uri" /> is <c>null</c>.
  99. /// </para>
  100. /// <para>
  101. /// <paramref name="server" /> is <c>null</c>.
  102. /// </para>
  103. /// <para>
  104. /// <paramref name="store" /> is <c>null</c>.
  105. /// </para>
  106. /// </exception>
  107. /// <exception cref="WebDavNotFoundException">If the item was not found.</exception>
  108. /// <exception cref="WebDavConflictException">
  109. /// <paramref name="uri" /> refers to a document in a collection, where the
  110. /// collection does not exist.
  111. /// </exception>
  112. /// <exception cref="WebDavNotFoundException"><paramref name="uri" /> refers to a document that does not exist.</exception>
  113. public static IWebDavStoreItem GetItem(this Uri uri, WebDavServer server, IWebDavStore store)
  114. {
  115. if (uri == null)
  116. throw new ArgumentNullException(nameof(uri));
  117. if (server == null)
  118. throw new ArgumentNullException(nameof(server));
  119. if (store == null)
  120. throw new ArgumentNullException(nameof(store));
  121. var prefixUri = uri.GetPrefixUri(server);
  122. var collection = store.Root;
  123. IWebDavStoreItem item = null;
  124. if (prefixUri.Segments.Length == uri.Segments.Length)
  125. return collection;
  126. for (var index = prefixUri.Segments.Length; index < uri.Segments.Length; index++)
  127. {
  128. var segmentName = Uri.UnescapeDataString(uri.Segments[index]);
  129. var nextItem = collection.GetItemByName(segmentName.TrimEnd('/', '\\'));
  130. if (nextItem == null)
  131. throw new WebDavNotFoundException(uri.ToString()); //throw new WebDavConflictException();
  132. if (index == uri.Segments.Length - 1)
  133. item = nextItem;
  134. else
  135. {
  136. collection = nextItem as IWebDavStoreCollection;
  137. if (collection == null)
  138. throw new WebDavNotFoundException();
  139. }
  140. }
  141. if (item == null)
  142. throw new WebDavNotFoundException();
  143. return item;
  144. }
  145. }
  146. }