Refactored HttpListenerPrefixCollection.cs

This commit is contained in:
sta 2014-04-12 17:59:40 +09:00
parent 93d4c77a4b
commit 99f7c9616e

View File

@ -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>
public class HttpListenerPrefixCollection
: ICollection<string>, IEnumerable<string>, IEnumerable
{
#region Private Fields
private HttpListener _listener; private HttpListener _listener;
private List<string> _prefixes; private List<string> _prefixes;
#endregion #endregion
#region Private Constructors #region Private Constructors
private HttpListenerPrefixCollection () private HttpListenerPrefixCollection ()
{ {
_prefixes = new List<string> (); _prefixes = new List<string> ();
} }
#endregion #endregion
#region Internal Constructors #region Internal Constructors
internal HttpListenerPrefixCollection (HttpListener listener) internal HttpListenerPrefixCollection (HttpListener listener)
: this () : this ()
{ {
_listener = listener; _listener = listener;
} }
#endregion #endregion
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets the number of prefixes contained in the <see cref="HttpListenerPrefixCollection"/>. /// Gets the number of prefixes contained in the <see cref="HttpListenerPrefixCollection"/>.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="int"/> that contains the number of prefixes. /// An <see cref="int"/> that represents the number of prefixes.
/// </value> /// </value>
public int Count { public int Count {
get { get {
return _prefixes.Count; return _prefixes.Count;
} }
} }
/// <summary> /// <summary>
/// Gets a value indicating whether access to the <see cref="HttpListenerPrefixCollection"/> /// Gets a value indicating whether the access to the <see cref="HttpListenerPrefixCollection"/>
/// is read-only. /// is read-only.
/// </summary> /// </summary>
/// <value> /// <value>
/// Always returns <c>false</c>. /// Always returns <c>false</c>.
/// </value> /// </value>
public bool IsReadOnly { public bool IsReadOnly {
get { get {
return false; return false;
} }
} }
/// <summary> /// <summary>
/// Gets a value indicating whether access to the <see cref="HttpListenerPrefixCollection"/> /// Gets a value indicating whether the access to the <see cref="HttpListenerPrefixCollection"/>
/// is synchronized. /// is synchronized.
/// </summary> /// </summary>
/// <value> /// <value>
/// Always returns <c>false</c>. /// Always returns <c>false</c>.
/// </value> /// </value>
public bool IsSynchronized { public bool IsSynchronized {
get { get {
return false; return false;
} }
} }
#endregion #endregion
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Adds the specified <paramref name="uriPrefix"/> to the <see cref="HttpListenerPrefixCollection"/>. /// Adds the specified <paramref name="uriPrefix"/> to
/// </summary> /// the <see cref="HttpListenerPrefixCollection"/>.
/// <param name="uriPrefix"> /// </summary>
/// A <see cref="string"/> that contains a URI prefix to add. /// <param name="uriPrefix">
/// </param> /// A <see cref="string"/> that represents the URI prefix to add. The prefix must be
/// <exception cref="ArgumentNullException"> /// a well-formed URI prefix with http or https scheme, and must be terminated with
/// <paramref name="uriPrefix"/> is <see langword="null"/>. /// a <c>"/"</c>.
/// </exception> /// </param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentNullException">
/// <paramref name="uriPrefix"/> is invalid. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ArgumentException">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// <paramref name="uriPrefix"/> is invalid.
/// is closed. /// </exception>
/// </exception> /// <exception cref="ObjectDisposedException">
public void Add (string uriPrefix) /// The <see cref="HttpListener"/> associated with
{ /// this <see cref="HttpListenerPrefixCollection"/> is closed.
_listener.CheckDisposed (); /// </exception>
ListenerPrefix.CheckUriPrefix (uriPrefix); public void Add (string uriPrefix)
if (_prefixes.Contains (uriPrefix)) {
return; _listener.CheckDisposed ();
ListenerPrefix.CheckUriPrefix (uriPrefix);
if (_prefixes.Contains (uriPrefix))
return;
_prefixes.Add (uriPrefix); _prefixes.Add (uriPrefix);
if (_listener.IsListening) if (_listener.IsListening)
EndPointManager.AddPrefix (uriPrefix, _listener); EndPointManager.AddPrefix (uriPrefix, _listener);
} }
/// <summary> /// <summary>
/// Removes all URI prefixes from the <see cref="HttpListenerPrefixCollection"/>. /// Removes all URI prefixes from the <see cref="HttpListenerPrefixCollection"/>.
/// </summary> /// </summary>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// The <see cref="HttpListener"/> associated with
/// is closed. /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// </exception> /// </exception>
public void Clear () public void Clear ()
{ {
_listener.CheckDisposed (); _listener.CheckDisposed ();
_prefixes.Clear (); _prefixes.Clear ();
if (_listener.IsListening) if (_listener.IsListening)
EndPointManager.RemoveListener (_listener); EndPointManager.RemoveListener (_listener);
} }
/// <summary> /// <summary>
/// Returns a value indicating whether the <see cref="HttpListenerPrefixCollection"/> contains /// Returns a value indicating whether the <see cref="HttpListenerPrefixCollection"/> contains
/// the specified <paramref name="uriPrefix"/>. /// the specified <paramref name="uriPrefix"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="HttpListenerPrefixCollection"/> contains <paramref name="uriPrefix"/>; /// <c>true</c> if the <see cref="HttpListenerPrefixCollection"/> contains
/// otherwise, <c>false</c>. /// <paramref name="uriPrefix"/>; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="uriPrefix"> /// <param name="uriPrefix">
/// A <see cref="string"/> that contains a URI prefix to test. /// A <see cref="string"/> that represents the URI prefix to test.
/// </param> /// </param>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="uriPrefix"/> is <see langword="null"/>. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// The <see cref="HttpListener"/> associated with
/// is closed. /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// </exception> /// </exception>
public bool Contains (string uriPrefix) public bool Contains (string uriPrefix)
{ {
_listener.CheckDisposed (); _listener.CheckDisposed ();
if (uriPrefix == null) if (uriPrefix == null)
throw new ArgumentNullException ("uriPrefix"); throw new ArgumentNullException ("uriPrefix");
return _prefixes.Contains (uriPrefix); return _prefixes.Contains (uriPrefix);
} }
/// <summary> /// <summary>
/// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to /// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to
/// the specified <see cref="Array"/>. /// the specified <see cref="Array"/>.
/// </summary> /// </summary>
/// <param name="array"> /// <param name="array">
/// An <see cref="Array"/> that receives the URI prefix strings /// An <see cref="Array"/> that receives the URI prefix strings in
/// in the <see cref="HttpListenerPrefixCollection"/>. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </param> /// </param>
/// <param name="offset"> /// <param name="offset">
/// An <see cref="int"/> that contains the zero-based index in <paramref name="array"/> /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
/// at which copying begins. /// at which copying begins.
/// </param> /// </param>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// The <see cref="HttpListener"/> associated with
/// is closed. /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// </exception> /// </exception>
public void CopyTo (Array array, int offset) public void CopyTo (Array array, int offset)
{ {
_listener.CheckDisposed (); _listener.CheckDisposed ();
((ICollection) _prefixes).CopyTo (array, offset); ((ICollection) _prefixes).CopyTo (array, offset);
} }
/// <summary> /// <summary>
/// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to /// Copies the contents of the <see cref="HttpListenerPrefixCollection"/> to
/// the specified array of <see cref="string"/>. /// the specified array of <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="array"> /// <param name="array">
/// An array of <see cref="string"/> that receives the URI prefix strings /// An array of <see cref="string"/> that receives the URI prefix strings in
/// in the <see cref="HttpListenerPrefixCollection"/>. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </param> /// </param>
/// <param name="offset"> /// <param name="offset">
/// An <see cref="int"/> that contains the zero-based index in <paramref name="array"/> /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
/// at which copying begins. /// at which copying begins.
/// </param> /// </param>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// The <see cref="HttpListener"/> associated with
/// is closed. /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// </exception> /// </exception>
public void CopyTo (string [] array, int offset) public void CopyTo (string [] array, int offset)
{ {
_listener.CheckDisposed (); _listener.CheckDisposed ();
_prefixes.CopyTo (array, offset); _prefixes.CopyTo (array, offset);
} }
/// <summary> /// <summary>
/// Gets an object that can be used to iterate through the <see cref="HttpListenerPrefixCollection"/>. /// Gets the enumerator used to iterate through the <see cref="HttpListenerPrefixCollection"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An object that implements the IEnumerator&lt;string&gt; interface and provides access to /// An <see cref="T:System.Collections.Generic.IEnumerator{string}"/> instance used to iterate
/// the URI prefix strings in the <see cref="HttpListenerPrefixCollection"/>. /// through the <see cref="HttpListenerPrefixCollection"/>.
/// </returns> /// </returns>
public IEnumerator<string> GetEnumerator () public IEnumerator<string> GetEnumerator ()
{ {
return _prefixes.GetEnumerator (); return _prefixes.GetEnumerator ();
} }
/// <summary> /// <summary>
/// Removes the specified <paramref name="uriPrefix"/> from the list of prefixes /// Removes the specified <paramref name="uriPrefix"/> from the list of prefixes in
/// in the <see cref="HttpListenerPrefixCollection"/>. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if <paramref name="uriPrefix"/> is successfully found and removed; /// <c>true</c> if <paramref name="uriPrefix"/> is successfully found and removed;
/// otherwise, <c>false</c>. /// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="uriPrefix"> /// <param name="uriPrefix">
/// A <see cref="string"/> that contains a URI prefix to remove. /// A <see cref="string"/> that represents the URI prefix to remove.
/// </param> /// </param>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="uriPrefix"/> is <see langword="null"/>. /// <paramref name="uriPrefix"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ObjectDisposedException">
/// The <see cref="HttpListener"/> associated with this <see cref="HttpListenerPrefixCollection"/> /// The <see cref="HttpListener"/> associated with
/// is closed. /// this <see cref="HttpListenerPrefixCollection"/> is closed.
/// </exception> /// </exception>
public bool Remove (string uriPrefix) public bool Remove (string uriPrefix)
{ {
_listener.CheckDisposed (); _listener.CheckDisposed ();
if (uriPrefix == null) if (uriPrefix == null)
throw new ArgumentNullException ("uriPrefix"); throw new ArgumentNullException ("uriPrefix");
var result = _prefixes.Remove (uriPrefix); var result = _prefixes.Remove (uriPrefix);
if (result && _listener.IsListening) if (result && _listener.IsListening)
EndPointManager.RemovePrefix (uriPrefix, _listener); EndPointManager.RemovePrefix (uriPrefix, _listener);
return result; return result;
} }
#endregion #endregion
#region Explicit Interface Implementation #region Explicit Interface Implementation
/// <summary> /// <summary>
/// Gets an object that can be used to iterate through the <see cref="HttpListenerPrefixCollection"/>. /// Gets the enumerator used to iterate through the <see cref="HttpListenerPrefixCollection"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An object that implements the <see cref="IEnumerator"/> interface and provides access to /// An <see cref="IEnumerator"/> instance used to iterate through
/// the URI prefix strings in the <see cref="HttpListenerPrefixCollection"/>. /// the <see cref="HttpListenerPrefixCollection"/>.
/// </returns> /// </returns>
IEnumerator IEnumerable.GetEnumerator () IEnumerator IEnumerable.GetEnumerator ()
{ {
return _prefixes.GetEnumerator (); return _prefixes.GetEnumerator ();
} }
#endregion #endregion
}
}
} }