HttpListenerPrefixCollection.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #region License
  2. /*
  3. * HttpListenerPrefixCollection.cs
  4. *
  5. * This code is derived from HttpListenerPrefixCollection.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  11. * Copyright (c) 2012-2020 sta.blockhead
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #endregion
  32. #region Authors
  33. /*
  34. * Authors:
  35. * - Gonzalo Paniagua Javier <gonzalo@novell.com>
  36. */
  37. #endregion
  38. using System;
  39. using System.Collections;
  40. using System.Collections.Generic;
  41. namespace WebSocketSharp.Net
  42. {
  43. /// <summary>
  44. /// Provides a collection used to store the URI prefixes for a instance of
  45. /// the <see cref="HttpListener"/> class.
  46. /// </summary>
  47. /// <remarks>
  48. /// The <see cref="HttpListener"/> instance responds to the request which has
  49. /// a requested URI that the prefixes most closely match.
  50. /// </remarks>
  51. public class HttpListenerPrefixCollection : ICollection<string>
  52. {
  53. #region Private Fields
  54. private HttpListener _listener;
  55. private List<string> _prefixes;
  56. #endregion
  57. #region Internal Constructors
  58. internal HttpListenerPrefixCollection (HttpListener listener)
  59. {
  60. _listener = listener;
  61. _prefixes = new List<string> ();
  62. }
  63. #endregion
  64. #region Public Properties
  65. /// <summary>
  66. /// Gets the number of prefixes in the collection.
  67. /// </summary>
  68. /// <value>
  69. /// An <see cref="int"/> that represents the number of prefixes.
  70. /// </value>
  71. public int Count {
  72. get {
  73. return _prefixes.Count;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets a value indicating whether the access to the collection is
  78. /// read-only.
  79. /// </summary>
  80. /// <value>
  81. /// Always returns <c>false</c>.
  82. /// </value>
  83. public bool IsReadOnly {
  84. get {
  85. return false;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets a value indicating whether the access to the collection is
  90. /// synchronized.
  91. /// </summary>
  92. /// <value>
  93. /// Always returns <c>false</c>.
  94. /// </value>
  95. public bool IsSynchronized {
  96. get {
  97. return false;
  98. }
  99. }
  100. #endregion
  101. #region Public Methods
  102. /// <summary>
  103. /// Adds the specified URI prefix to the collection.
  104. /// </summary>
  105. /// <param name="uriPrefix">
  106. /// <para>
  107. /// A <see cref="string"/> that specifies the URI prefix to add.
  108. /// </para>
  109. /// <para>
  110. /// It must be a well-formed URI prefix with http or https scheme,
  111. /// and must end with a '/'.
  112. /// </para>
  113. /// </param>
  114. /// <exception cref="ArgumentNullException">
  115. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
  116. /// </exception>
  117. /// <exception cref="ArgumentException">
  118. /// <paramref name="uriPrefix"/> is invalid.
  119. /// </exception>
  120. /// <exception cref="ObjectDisposedException">
  121. /// The <see cref="HttpListener"/> instance associated with this
  122. /// collection is closed.
  123. /// </exception>
  124. public void Add (string uriPrefix)
  125. {
  126. _listener.CheckDisposed ();
  127. HttpListenerPrefix.CheckPrefix (uriPrefix);
  128. if (_prefixes.Contains (uriPrefix))
  129. return;
  130. if (_listener.IsListening)
  131. EndPointManager.AddPrefix (uriPrefix, _listener);
  132. _prefixes.Add (uriPrefix);
  133. }
  134. /// <summary>
  135. /// Removes all URI prefixes from the collection.
  136. /// </summary>
  137. /// <exception cref="ObjectDisposedException">
  138. /// The <see cref="HttpListener"/> instance associated with this
  139. /// collection is closed.
  140. /// </exception>
  141. public void Clear ()
  142. {
  143. _listener.CheckDisposed ();
  144. if (_listener.IsListening)
  145. EndPointManager.RemoveListener (_listener);
  146. _prefixes.Clear ();
  147. }
  148. /// <summary>
  149. /// Returns a value indicating whether the collection contains the
  150. /// specified URI prefix.
  151. /// </summary>
  152. /// <returns>
  153. /// <c>true</c> if the collection contains the URI prefix; otherwise,
  154. /// <c>false</c>.
  155. /// </returns>
  156. /// <param name="uriPrefix">
  157. /// A <see cref="string"/> that specifies the URI prefix to test.
  158. /// </param>
  159. /// <exception cref="ArgumentNullException">
  160. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
  161. /// </exception>
  162. /// <exception cref="ObjectDisposedException">
  163. /// The <see cref="HttpListener"/> instance associated with this
  164. /// collection is closed.
  165. /// </exception>
  166. public bool Contains (string uriPrefix)
  167. {
  168. _listener.CheckDisposed ();
  169. if (uriPrefix == null)
  170. throw new ArgumentNullException ("uriPrefix");
  171. return _prefixes.Contains (uriPrefix);
  172. }
  173. /// <summary>
  174. /// Copies the contents of the collection to the specified array of string.
  175. /// </summary>
  176. /// <param name="array">
  177. /// An array of <see cref="string"/> that specifies the destination of
  178. /// the URI prefix strings copied from the collection.
  179. /// </param>
  180. /// <param name="offset">
  181. /// An <see cref="int"/> that specifies the zero-based index in
  182. /// the array at which copying begins.
  183. /// </param>
  184. /// <exception cref="ArgumentNullException">
  185. /// <paramref name="array"/> is <see langword="null"/>.
  186. /// </exception>
  187. /// <exception cref="ArgumentOutOfRangeException">
  188. /// <paramref name="offset"/> is less than zero.
  189. /// </exception>
  190. /// <exception cref="ArgumentException">
  191. /// The space from <paramref name="offset"/> to the end of
  192. /// <paramref name="array"/> is not enough to copy to.
  193. /// </exception>
  194. /// <exception cref="ObjectDisposedException">
  195. /// The <see cref="HttpListener"/> instance associated with this
  196. /// collection is closed.
  197. /// </exception>
  198. public void CopyTo (string[] array, int offset)
  199. {
  200. _listener.CheckDisposed ();
  201. _prefixes.CopyTo (array, offset);
  202. }
  203. /// <summary>
  204. /// Gets the enumerator that iterates through the collection.
  205. /// </summary>
  206. /// <returns>
  207. /// An <see cref="T:System.Collections.Generic.IEnumerator{string}"/>
  208. /// instance that can be used to iterate through the collection.
  209. /// </returns>
  210. public IEnumerator<string> GetEnumerator ()
  211. {
  212. return _prefixes.GetEnumerator ();
  213. }
  214. /// <summary>
  215. /// Removes the specified URI prefix from the collection.
  216. /// </summary>
  217. /// <returns>
  218. /// <c>true</c> if the URI prefix is successfully removed; otherwise,
  219. /// <c>false</c>.
  220. /// </returns>
  221. /// <param name="uriPrefix">
  222. /// A <see cref="string"/> that specifies the URI prefix to remove.
  223. /// </param>
  224. /// <exception cref="ArgumentNullException">
  225. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
  226. /// </exception>
  227. /// <exception cref="ObjectDisposedException">
  228. /// The <see cref="HttpListener"/> instance associated with this
  229. /// collection is closed.
  230. /// </exception>
  231. public bool Remove (string uriPrefix)
  232. {
  233. _listener.CheckDisposed ();
  234. if (uriPrefix == null)
  235. throw new ArgumentNullException ("uriPrefix");
  236. if (!_prefixes.Contains (uriPrefix))
  237. return false;
  238. if (_listener.IsListening)
  239. EndPointManager.RemovePrefix (uriPrefix, _listener);
  240. return _prefixes.Remove (uriPrefix);
  241. }
  242. #endregion
  243. #region Explicit Interface Implementations
  244. /// <summary>
  245. /// Gets the enumerator that iterates through the collection.
  246. /// </summary>
  247. /// <returns>
  248. /// An <see cref="IEnumerator"/> instance that can be used to iterate
  249. /// through the collection.
  250. /// </returns>
  251. IEnumerator IEnumerable.GetEnumerator ()
  252. {
  253. return _prefixes.GetEnumerator ();
  254. }
  255. #endregion
  256. }
  257. }