PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/websocket-sharp/Net/HttpListenerPrefixCollection.cs

https://gitlab.com/OriumVR/websocket-sharp
C# | 278 lines | 97 code | 29 blank | 152 comment | 9 complexity | ed7d954b7dacb786f7f939d3ef9264e1 MD5 | raw file
  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-2015 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 the collection used to store the URI prefixes for the <see cref="HttpListener"/>.
  45. /// </summary>
  46. /// <remarks>
  47. /// The <see cref="HttpListener"/> responds to the request which has a requested URI that
  48. /// the prefixes most closely match.
  49. /// </remarks>
  50. public class HttpListenerPrefixCollection : ICollection<string>, IEnumerable<string>, IEnumerable
  51. {
  52. #region Private Fields
  53. private HttpListener _listener;
  54. private List<string> _prefixes;
  55. #endregion
  56. #region Internal Constructors
  57. internal HttpListenerPrefixCollection (HttpListener listener)
  58. {
  59. _listener = listener;
  60. _prefixes = new List<string> ();
  61. }
  62. #endregion
  63. #region Public Properties
  64. /// <summary>
  65. /// Gets the number of prefixes in the collection.
  66. /// </summary>
  67. /// <value>
  68. /// An <see cref="int"/> that represents the number of prefixes.
  69. /// </value>
  70. public int Count {
  71. get {
  72. return _prefixes.Count;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets a value indicating whether the access to the collection is read-only.
  77. /// </summary>
  78. /// <value>
  79. /// Always returns <c>false</c>.
  80. /// </value>
  81. public bool IsReadOnly {
  82. get {
  83. return false;
  84. }
  85. }
  86. /// <summary>
  87. /// Gets a value indicating whether the access to the collection is synchronized.
  88. /// </summary>
  89. /// <value>
  90. /// Always returns <c>false</c>.
  91. /// </value>
  92. public bool IsSynchronized {
  93. get {
  94. return false;
  95. }
  96. }
  97. #endregion
  98. #region Public Methods
  99. /// <summary>
  100. /// Adds the specified <paramref name="uriPrefix"/> to the collection.
  101. /// </summary>
  102. /// <param name="uriPrefix">
  103. /// A <see cref="string"/> that represents the URI prefix to add. The prefix must be
  104. /// a well-formed URI prefix with http or https scheme, and must end with a <c>'/'</c>.
  105. /// </param>
  106. /// <exception cref="ArgumentNullException">
  107. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
  108. /// </exception>
  109. /// <exception cref="ArgumentException">
  110. /// <paramref name="uriPrefix"/> is invalid.
  111. /// </exception>
  112. /// <exception cref="ObjectDisposedException">
  113. /// The <see cref="HttpListener"/> associated with this collection is closed.
  114. /// </exception>
  115. public void Add (string uriPrefix)
  116. {
  117. _listener.CheckDisposed ();
  118. HttpListenerPrefix.CheckPrefix (uriPrefix);
  119. if (_prefixes.Contains (uriPrefix))
  120. return;
  121. _prefixes.Add (uriPrefix);
  122. if (_listener.IsListening)
  123. EndPointManager.AddPrefix (uriPrefix, _listener);
  124. }
  125. /// <summary>
  126. /// Removes all URI prefixes from the collection.
  127. /// </summary>
  128. /// <exception cref="ObjectDisposedException">
  129. /// The <see cref="HttpListener"/> associated with this collection is closed.
  130. /// </exception>
  131. public void Clear ()
  132. {
  133. _listener.CheckDisposed ();
  134. _prefixes.Clear ();
  135. if (_listener.IsListening)
  136. EndPointManager.RemoveListener (_listener);
  137. }
  138. /// <summary>
  139. /// Returns a value indicating whether the collection contains the specified
  140. /// <paramref name="uriPrefix"/>.
  141. /// </summary>
  142. /// <returns>
  143. /// <c>true</c> if the collection contains <paramref name="uriPrefix"/>;
  144. /// otherwise, <c>false</c>.
  145. /// </returns>
  146. /// <param name="uriPrefix">
  147. /// A <see cref="string"/> that represents the URI prefix to test.
  148. /// </param>
  149. /// <exception cref="ArgumentNullException">
  150. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
  151. /// </exception>
  152. /// <exception cref="ObjectDisposedException">
  153. /// The <see cref="HttpListener"/> associated with this collection is closed.
  154. /// </exception>
  155. public bool Contains (string uriPrefix)
  156. {
  157. _listener.CheckDisposed ();
  158. if (uriPrefix == null)
  159. throw new ArgumentNullException ("uriPrefix");
  160. return _prefixes.Contains (uriPrefix);
  161. }
  162. /// <summary>
  163. /// Copies the contents of the collection to the specified <see cref="Array"/>.
  164. /// </summary>
  165. /// <param name="array">
  166. /// An <see cref="Array"/> that receives the URI prefix strings in the collection.
  167. /// </param>
  168. /// <param name="offset">
  169. /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
  170. /// at which copying begins.
  171. /// </param>
  172. /// <exception cref="ObjectDisposedException">
  173. /// The <see cref="HttpListener"/> associated with this collection is closed.
  174. /// </exception>
  175. public void CopyTo (Array array, int offset)
  176. {
  177. _listener.CheckDisposed ();
  178. ((ICollection) _prefixes).CopyTo (array, offset);
  179. }
  180. /// <summary>
  181. /// Copies the contents of the collection to the specified array of <see cref="string"/>.
  182. /// </summary>
  183. /// <param name="array">
  184. /// An array of <see cref="string"/> that receives the URI prefix strings in the collection.
  185. /// </param>
  186. /// <param name="offset">
  187. /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
  188. /// at which copying begins.
  189. /// </param>
  190. /// <exception cref="ObjectDisposedException">
  191. /// The <see cref="HttpListener"/> associated with this collection is closed.
  192. /// </exception>
  193. public void CopyTo (string[] array, int offset)
  194. {
  195. _listener.CheckDisposed ();
  196. _prefixes.CopyTo (array, offset);
  197. }
  198. /// <summary>
  199. /// Gets the enumerator used to iterate through the <see cref="HttpListenerPrefixCollection"/>.
  200. /// </summary>
  201. /// <returns>
  202. /// An <see cref="T:System.Collections.Generic.IEnumerator{string}"/> instance used to iterate
  203. /// through the collection.
  204. /// </returns>
  205. public IEnumerator<string> GetEnumerator ()
  206. {
  207. return _prefixes.GetEnumerator ();
  208. }
  209. /// <summary>
  210. /// Removes the specified <paramref name="uriPrefix"/> from the collection.
  211. /// </summary>
  212. /// <returns>
  213. /// <c>true</c> if <paramref name="uriPrefix"/> is successfully found and removed;
  214. /// otherwise, <c>false</c>.
  215. /// </returns>
  216. /// <param name="uriPrefix">
  217. /// A <see cref="string"/> that represents the URI prefix to remove.
  218. /// </param>
  219. /// <exception cref="ArgumentNullException">
  220. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
  221. /// </exception>
  222. /// <exception cref="ObjectDisposedException">
  223. /// The <see cref="HttpListener"/> associated with this collection is closed.
  224. /// </exception>
  225. public bool Remove (string uriPrefix)
  226. {
  227. _listener.CheckDisposed ();
  228. if (uriPrefix == null)
  229. throw new ArgumentNullException ("uriPrefix");
  230. var ret = _prefixes.Remove (uriPrefix);
  231. if (ret && _listener.IsListening)
  232. EndPointManager.RemovePrefix (uriPrefix, _listener);
  233. return ret;
  234. }
  235. #endregion
  236. #region Explicit Interface Implementations
  237. /// <summary>
  238. /// Gets the enumerator used to iterate through the <see cref="HttpListenerPrefixCollection"/>.
  239. /// </summary>
  240. /// <returns>
  241. /// An <see cref="IEnumerator"/> instance used to iterate through the collection.
  242. /// </returns>
  243. IEnumerator IEnumerable.GetEnumerator ()
  244. {
  245. return _prefixes.GetEnumerator ();
  246. }
  247. #endregion
  248. }
  249. }