Refactored HttpListenerPrefixCollection.cs

master
sta 12 years ago
parent 93d4c77a4b
commit 99f7c9616e

@ -1,32 +1,41 @@
// #region License
// HttpListenerPrefixCollection.cs /*
// Copied from System.Net.HttpListenerPrefixCollection.cs * HttpListenerPrefixCollection.cs
// *
// Author: * This code is derived from System.Net.HttpListenerPrefixCollection.cs of Mono
// Gonzalo Paniagua Javier (gonzalo@novell.com) * (http://www.mono-project.com).
// *
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com) * The MIT License
// Copyright (c) 2012-2013 sta.blockhead *
// * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
// Permission is hereby granted, free of charge, to any person obtaining * Copyright (c) 2012-2014 sta.blockhead
// a copy of this software and associated documentation files (the *
// "Software"), to deal in the Software without restriction, including * Permission is hereby granted, free of charge, to any person obtaining a copy
// without limitation the rights to use, copy, modify, merge, publish, * of this software and associated documentation files (the "Software"), to deal
// distribute, sublicense, and/or sell copies of the Software, and to * in the Software without restriction, including without limitation the rights
// permit persons to whom the Software is furnished to do so, subject to * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// the following conditions: * 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 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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// * THE SOFTWARE.
*/
#endregion
#region Authors
/*
* Authors:
* - Gonzalo Paniagua Javier <gonzalo@novell.com>
*/
#endregion
using System; using System;
using System.Collections; using System.Collections;
@ -34,255 +43,262 @@ using System.Collections.Generic;
namespace WebSocketSharp.Net namespace WebSocketSharp.Net
{ {
/// <summary> /// <summary>
/// Provides the collection used to store the URI prefixes for the <see cref="HttpListener"/>. /// Provides the collection used to store the URI prefixes for the <see cref="HttpListener"/>.
/// </summary> /// </summary>
public class HttpListenerPrefixCollection : ICollection<string>, IEnumerable<string>, IEnumerable /// <remarks>
{ /// The <see cref="HttpListener"/> responds to the request which has a requested URI that
#region Private Fields /// the prefixes most closely match.
/// </remarks>
private HttpListener _listener; public class HttpListenerPrefixCollection
private List<string> _prefixes; : ICollection<string>, IEnumerable<string>, IEnumerable
{
#endregion #region Private Fields
#region Private Constructors private HttpListener _listener;
private List<string> _prefixes;
private HttpListenerPrefixCollection ()
{ #endregion
_prefixes = new List<string> ();
} #region Private Constructors
#endregion private HttpListenerPrefixCollection ()
{
#region Internal Constructors _prefixes = new List<string> ();
}
internal HttpListenerPrefixCollection (HttpListener listener)
: this () #endregion
{
_listener = listener; #region Internal Constructors
}
internal HttpListenerPrefixCollection (HttpListener listener)
#endregion : this ()
{
#region Public Properties _listener = listener;
}
/// <summary>
/// Gets the number of prefixes contained in the <see cref="HttpListenerPrefixCollection"/>. #endregion
/// </summary>
/// <value> #region Public Properties
/// A <see cref="int"/> that contains the number of prefixes.
/// </value> /// <summary>
public int Count { /// Gets the number of prefixes contained in the <see cref="HttpListenerPrefixCollection"/>.
get { /// </summary>
return _prefixes.Count; /// <value>
} /// An <see cref="int"/> that represents the number of prefixes.
} /// </value>
public int Count {
/// <summary> get {
/// Gets a value indicating whether access to the <see cref="HttpListenerPrefixCollection"/> return _prefixes.Count;
/// is read-only. }
/// </summary> }
/// <value>
/// Always returns <c>false</c>. /// <summary>
/// </value> /// Gets a value indicating whether the access to the <see cref="HttpListenerPrefixCollection"/>
public bool IsReadOnly { /// is read-only.
get { /// </summary>
return false; /// <value>
} /// Always returns <c>false</c>.
} /// </value>
public bool IsReadOnly {
/// <summary> get {
/// Gets a value indicating whether access to the <see cref="HttpListenerPrefixCollection"/> return false;
/// is synchronized. }
/// </summary> }
/// <value>
/// Always returns <c>false</c>. /// <summary>
/// </value> /// Gets a value indicating whether the access to the <see cref="HttpListenerPrefixCollection"/>
public bool IsSynchronized { /// is synchronized.
get { /// </summary>
return false; /// <value>
} /// Always returns <c>false</c>.
} /// </value>
public bool IsSynchronized {
#endregion get {
return false;
#region Public Methods }
}
/// <summary>
/// Adds the specified <paramref name="uriPrefix"/> to the <see cref="HttpListenerPrefixCollection"/>. #endregion
/// </summary>
/// <param name="uriPrefix"> #region Public Methods
/// A <see cref="string"/> that contains a URI prefix to add.
/// </param> /// <summary>
/// <exception cref="ArgumentNullException"> /// Adds the specified <paramref name="uriPrefix"/> to
/// <paramref name="uriPrefix"/> is <see langword="null"/>. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </exception> /// </summary>
/// <exception cref="ArgumentException"> /// <param name="uriPrefix">
/// <paramref name="uriPrefix"/> is invalid. /// A <see cref="string"/> that represents the URI prefix to add. The prefix must be
/// </exception> /// a well-formed URI prefix with http or https scheme, and must be terminated with
/// <exception cref="ObjectDisposedException"> /// a <c>"/"</c>.
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// </param>
/// is closed. /// <exception cref="ArgumentNullException">
/// </exception> /// <paramref name="uriPrefix"/> is <see langword="null"/>.
public void Add (string uriPrefix) /// </exception>
{ /// <exception cref="ArgumentException">
_listener.CheckDisposed (); /// <paramref name="uriPrefix"/> is invalid.
ListenerPrefix.CheckUriPrefix (uriPrefix); /// </exception>
if (_prefixes.Contains (uriPrefix)) /// <exception cref="ObjectDisposedException">
return; /// The <see cref="HttpListener"/> associated with
/// this <see cref="HttpListenerPrefixCollection"/> is closed.
_prefixes.Add (uriPrefix); /// </exception>
if (_listener.IsListening) public void Add (string uriPrefix)
EndPointManager.AddPrefix (uriPrefix, _listener); {
} _listener.CheckDisposed ();
ListenerPrefix.CheckUriPrefix (uriPrefix);
/// <summary> if (_prefixes.Contains (uriPrefix))
/// Removes all URI prefixes from the <see cref="HttpListenerPrefixCollection"/>. return;
/// </summary>
/// <exception cref="ObjectDisposedException"> _prefixes.Add (uriPrefix);
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> if (_listener.IsListening)
/// is closed. EndPointManager.AddPrefix (uriPrefix, _listener);
/// </exception> }
public void Clear ()
{ /// <summary>
_listener.CheckDisposed (); /// Removes all URI prefixes from the <see cref="HttpListenerPrefixCollection"/>.
_prefixes.Clear (); /// </summary>
if (_listener.IsListening) /// <exception cref="ObjectDisposedException">
EndPointManager.RemoveListener (_listener); /// The <see cref="HttpListener"/> associated with
} /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// </exception>
/// <summary> public void Clear ()
/// Returns a value indicating whether the <see cref="HttpListenerPrefixCollection"/> contains {
/// the specified <paramref name="uriPrefix"/>. _listener.CheckDisposed ();
/// </summary> _prefixes.Clear ();
/// <returns> if (_listener.IsListening)
/// <c>true</c> if the <see cref="HttpListenerPrefixCollection"/> contains <paramref name="uriPrefix"/>; EndPointManager.RemoveListener (_listener);
/// otherwise, <c>false</c>. }
/// </returns>
/// <param name="uriPrefix"> /// <summary>
/// A <see cref="string"/> that contains a URI prefix to test. /// Returns a value indicating whether the <see cref="HttpListenerPrefixCollection"/> contains
/// </param> /// the specified <paramref name="uriPrefix"/>.
/// <exception cref="ArgumentNullException"> /// </summary>
/// <paramref name="uriPrefix"/> is <see langword="null"/>. /// <returns>
/// </exception> /// <c>true</c> if the <see cref="HttpListenerPrefixCollection"/> contains
/// <exception cref="ObjectDisposedException"> /// <paramref name="uriPrefix"/>; otherwise, <c>false</c>.
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// </returns>
/// is closed. /// <param name="uriPrefix">
/// </exception> /// A <see cref="string"/> that represents the URI prefix to test.
public bool Contains (string uriPrefix) /// </param>
{ /// <exception cref="ArgumentNullException">
_listener.CheckDisposed (); /// <paramref name="uriPrefix"/> is <see langword="null"/>.
if (uriPrefix == null) /// </exception>
throw new ArgumentNullException ("uriPrefix"); /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with
return _prefixes.Contains (uriPrefix); /// this <see cref="HttpListenerPrefixCollection"/> is closed.
} /// </exception>
public bool Contains (string uriPrefix)
/// <summary> {
/// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to _listener.CheckDisposed ();
/// the specified <see cref="Array"/>. if (uriPrefix == null)
/// </summary> throw new ArgumentNullException ("uriPrefix");
/// <param name="array">
/// An <see cref="Array"/> that receives the URI prefix strings return _prefixes.Contains (uriPrefix);
/// in the <see cref="HttpListenerPrefixCollection"/>. }
/// </param>
/// <param name="offset"> /// <summary>
/// An <see cref="int"/> that contains the zero-based index in <paramref name="array"/> /// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to
/// at which copying begins. /// the specified <see cref="Array"/>.
/// </param> /// </summary>
/// <exception cref="ObjectDisposedException"> /// <param name="array">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// An <see cref="Array"/> that receives the URI prefix strings in
/// is closed. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </exception> /// </param>
public void CopyTo (Array array, int offset) /// <param name="offset">
{ /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
_listener.CheckDisposed (); /// at which copying begins.
((ICollection) _prefixes).CopyTo (array, offset); /// </param>
} /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with
/// <summary> /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to /// </exception>
/// the specified array of <see cref="string"/>. public void CopyTo (Array array, int offset)
/// </summary> {
/// <param name="array"> _listener.CheckDisposed ();
/// An array of <see cref="string"/> that receives the URI prefix strings ((ICollection) _prefixes).CopyTo (array, offset);
/// in the <see cref="HttpListenerPrefixCollection"/>. }
/// </param>
/// <param name="offset"> /// <summary>
/// An <see cref="int"/> that contains the zero-based index in <paramref name="array"/> /// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to
/// at which copying begins. /// the specified array of <see cref="string"/>.
/// </param> /// </summary>
/// <exception cref="ObjectDisposedException"> /// <param name="array">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// An array of <see cref="string"/> that receives the URI prefix strings in
/// is closed. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </exception> /// </param>
public void CopyTo (string [] array, int offset) /// <param name="offset">
{ /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
_listener.CheckDisposed (); /// at which copying begins.
_prefixes.CopyTo (array, offset); /// </param>
} /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with
/// <summary> /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// Gets an object that can be used to iterate through the <see cref="HttpListenerPrefixCollection"/>. /// </exception>
/// </summary> public void CopyTo (string [] array, int offset)
/// <returns> {
/// An object that implements the IEnumerator&lt;string&gt; interface and provides access to _listener.CheckDisposed ();
/// the URI prefix strings in the <see cref="HttpListenerPrefixCollection"/>. _prefixes.CopyTo (array, offset);
/// </returns> }
public IEnumerator<string> GetEnumerator ()
{ /// <summary>
return _prefixes.GetEnumerator (); /// Gets the enumerator used to iterate through the <see cref="HttpListenerPrefixCollection"/>.
} /// </summary>
/// <returns>
/// <summary> /// An <see cref="T:System.Collections.Generic.IEnumerator{string}"/> instance used to iterate
/// Removes the specified <paramref name="uriPrefix"/> from the list of prefixes /// through the <see cref="HttpListenerPrefixCollection"/>.
/// in the <see cref="HttpListenerPrefixCollection"/>. /// </returns>
/// </summary> public IEnumerator<string> GetEnumerator ()
/// <returns> {
/// <c>true</c> if <paramref name="uriPrefix"/> is successfully found and removed; return _prefixes.GetEnumerator ();
/// otherwise, <c>false</c>. }
/// </returns>
/// <param name="uriPrefix"> /// <summary>
/// A <see cref="string"/> that contains a URI prefix to remove. /// Removes the specified <paramref name="uriPrefix"/> from the list of prefixes in
/// </param> /// the <see cref="HttpListenerPrefixCollection"/>.
/// <exception cref="ArgumentNullException"> /// </summary>
/// <paramref name="uriPrefix"/> is <see langword="null"/>. /// <returns>
/// </exception> /// <c>true</c> if <paramref name="uriPrefix"/> is successfully found and removed;
/// <exception cref="ObjectDisposedException"> /// otherwise, <c>false</c>.
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// </returns>
/// is closed. /// <param name="uriPrefix">
/// </exception> /// A <see cref="string"/> that represents the URI prefix to remove.
public bool Remove (string uriPrefix) /// </param>
{ /// <exception cref="ArgumentNullException">
_listener.CheckDisposed (); /// <paramref name="uriPrefix"/> is <see langword="null"/>.
if (uriPrefix == null) /// </exception>
throw new ArgumentNullException ("uriPrefix"); /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with
var result = _prefixes.Remove (uriPrefix); /// this <see cref="HttpListenerPrefixCollection"/> is closed.
if (result && _listener.IsListening) /// </exception>
EndPointManager.RemovePrefix (uriPrefix, _listener); public bool Remove (string uriPrefix)
{
return result; _listener.CheckDisposed ();
} if (uriPrefix == null)
throw new ArgumentNullException ("uriPrefix");
#endregion
var result = _prefixes.Remove (uriPrefix);
#region Explicit Interface Implementation if (result && _listener.IsListening)
EndPointManager.RemovePrefix (uriPrefix, _listener);
/// <summary>
/// Gets an object that can be used to iterate through the <see cref="HttpListenerPrefixCollection"/>. return result;
/// </summary> }
/// <returns>
/// An object that implements the <see cref="IEnumerator"/> interface and provides access to #endregion
/// the URI prefix strings in the <see cref="HttpListenerPrefixCollection"/>.
/// </returns> #region Explicit Interface Implementation
IEnumerator IEnumerable.GetEnumerator ()
{ /// <summary>
return _prefixes.GetEnumerator (); /// Gets the enumerator used to iterate through the <see cref="HttpListenerPrefixCollection"/>.
} /// </summary>
/// <returns>
#endregion /// An <see cref="IEnumerator"/> instance used to iterate through
/// the <see cref="HttpListenerPrefixCollection"/>.
} /// </returns>
IEnumerator IEnumerable.GetEnumerator ()
{
return _prefixes.GetEnumerator ();
}
#endregion
}
} }

Loading…
Cancel
Save