#region License /* * HttpListenerPrefixCollection.cs * * This code is derived from HttpListenerPrefixCollection.cs (System.Net) of Mono * (http://www.mono-project.com). * * The MIT License * * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) * Copyright (c) 2012-2020 sta.blockhead * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #endregion #region Authors /* * Authors: * - Gonzalo Paniagua Javier */ #endregion using System; using System.Collections; using System.Collections.Generic; namespace WebSocketSharp.Net { /// /// Provides a collection used to store the URI prefixes for a instance of /// the class. /// /// /// The instance responds to the request which has /// a requested URI that the prefixes most closely match. /// public class HttpListenerPrefixCollection : ICollection { #region Private Fields private HttpListener _listener; private List _prefixes; #endregion #region Internal Constructors internal HttpListenerPrefixCollection (HttpListener listener) { _listener = listener; _prefixes = new List (); } #endregion #region Public Properties /// /// Gets the number of prefixes in the collection. /// /// /// An that represents the number of prefixes. /// public int Count { get { return _prefixes.Count; } } /// /// Gets a value indicating whether the access to the collection is /// read-only. /// /// /// Always returns false. /// public bool IsReadOnly { get { return false; } } /// /// Gets a value indicating whether the access to the collection is /// synchronized. /// /// /// Always returns false. /// public bool IsSynchronized { get { return false; } } #endregion #region Public Methods /// /// Adds the specified URI prefix to the collection. /// /// /// /// A that specifies the URI prefix to add. /// /// /// It must be a well-formed URI prefix with http or https scheme, /// and must end with a '/'. /// /// /// /// is . /// /// /// is invalid. /// /// /// The instance associated with this /// collection is closed. /// public void Add (string uriPrefix) { _listener.CheckDisposed (); HttpListenerPrefix.CheckPrefix (uriPrefix); if (_prefixes.Contains (uriPrefix)) return; if (_listener.IsListening) EndPointManager.AddPrefix (uriPrefix, _listener); _prefixes.Add (uriPrefix); } /// /// Removes all URI prefixes from the collection. /// /// /// The instance associated with this /// collection is closed. /// public void Clear () { _listener.CheckDisposed (); if (_listener.IsListening) EndPointManager.RemoveListener (_listener); _prefixes.Clear (); } /// /// Returns a value indicating whether the collection contains the /// specified URI prefix. /// /// /// true if the collection contains the URI prefix; otherwise, /// false. /// /// /// A that specifies the URI prefix to test. /// /// /// is . /// /// /// The instance associated with this /// collection is closed. /// public bool Contains (string uriPrefix) { _listener.CheckDisposed (); if (uriPrefix == null) throw new ArgumentNullException ("uriPrefix"); return _prefixes.Contains (uriPrefix); } /// /// Copies the contents of the collection to the specified array of string. /// /// /// An array of that specifies the destination of /// the URI prefix strings copied from the collection. /// /// /// An that specifies the zero-based index in /// the array at which copying begins. /// /// /// is . /// /// /// is less than zero. /// /// /// The space from to the end of /// is not enough to copy to. /// /// /// The instance associated with this /// collection is closed. /// public void CopyTo (string[] array, int offset) { _listener.CheckDisposed (); _prefixes.CopyTo (array, offset); } /// /// Gets the enumerator that iterates through the collection. /// /// /// An /// instance that can be used to iterate through the collection. /// public IEnumerator GetEnumerator () { return _prefixes.GetEnumerator (); } /// /// Removes the specified URI prefix from the collection. /// /// /// true if the URI prefix is successfully removed; otherwise, /// false. /// /// /// A that specifies the URI prefix to remove. /// /// /// is . /// /// /// The instance associated with this /// collection is closed. /// public bool Remove (string uriPrefix) { _listener.CheckDisposed (); if (uriPrefix == null) throw new ArgumentNullException ("uriPrefix"); if (!_prefixes.Contains (uriPrefix)) return false; if (_listener.IsListening) EndPointManager.RemovePrefix (uriPrefix, _listener); return _prefixes.Remove (uriPrefix); } #endregion #region Explicit Interface Implementations /// /// Gets the enumerator that iterates through the collection. /// /// /// An instance that can be used to iterate /// through the collection. /// IEnumerator IEnumerable.GetEnumerator () { return _prefixes.GetEnumerator (); } #endregion } }