websocket-sharp/websocket-sharp/Net/CookieCollection.cs

751 lines
19 KiB
C#
Raw Normal View History

2014-03-24 20:02:06 +08:00
#region License
/*
* CookieCollection.cs
*
2019-04-08 19:47:08 +08:00
* This code is derived from CookieCollection.cs (System.Net) of Mono
2014-03-24 20:02:06 +08:00
* (http://www.mono-project.com).
*
* The MIT License
*
* Copyright (c) 2004,2009 Novell, Inc. (http://www.novell.com)
* Copyright (c) 2012-2014 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:
* - Lawrence Pit <loz@cable.a2000.nl>
* - Gonzalo Paniagua Javier <gonzalo@ximian.com>
* - Sebastien Pouliot <sebastien@ximian.com>
*/
#endregion
2012-09-10 00:36:22 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
2013-04-06 15:21:41 +08:00
using System.Text;
2012-09-10 00:36:22 +08:00
2014-03-24 20:02:06 +08:00
namespace WebSocketSharp.Net
{
/// <summary>
2019-04-07 16:48:53 +08:00
/// Provides a collection of instances of the <see cref="Cookie"/> class.
2014-03-24 20:02:06 +08:00
/// </summary>
[Serializable]
public class CookieCollection : ICollection, IEnumerable
{
#region Private Fields
private List<Cookie> _list;
2019-03-26 20:24:36 +08:00
private bool _readOnly;
2014-03-24 20:02:06 +08:00
private object _sync;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CookieCollection"/> class.
/// </summary>
public CookieCollection ()
{
_list = new List<Cookie> ();
2019-03-20 21:54:40 +08:00
_sync = ((ICollection) _list).SyncRoot;
2014-03-24 20:02:06 +08:00
}
#endregion
#region Internal Properties
internal IList<Cookie> List {
get {
return _list;
}
}
internal IEnumerable<Cookie> Sorted {
get {
2014-03-26 11:17:58 +08:00
var list = new List<Cookie> (_list);
if (list.Count > 1)
2019-04-02 20:16:23 +08:00
list.Sort (compareForSorted);
2014-03-26 11:17:58 +08:00
return list;
2014-03-24 20:02:06 +08:00
}
}
#endregion
#region Public Properties
/// <summary>
2014-04-21 19:59:13 +08:00
/// Gets the number of cookies in the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <value>
2019-03-25 20:14:00 +08:00
/// An <see cref="int"/> that represents the number of cookies in
/// the collection.
2014-03-24 20:02:06 +08:00
/// </value>
public int Count {
get {
return _list.Count;
}
}
/// <summary>
2014-04-21 19:59:13 +08:00
/// Gets a value indicating whether the collection is read-only.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <value>
2019-03-24 20:37:12 +08:00
/// <para>
/// <c>true</c> if the collection is read-only; otherwise, <c>false</c>.
/// </para>
/// <para>
2019-03-26 20:24:36 +08:00
/// The default value is <c>false</c>.
2019-03-24 20:37:12 +08:00
/// </para>
2014-03-24 20:02:06 +08:00
/// </value>
public bool IsReadOnly {
get {
2019-03-26 20:24:36 +08:00
return _readOnly;
}
internal set {
_readOnly = value;
2014-03-24 20:02:06 +08:00
}
}
/// <summary>
2019-03-24 20:32:24 +08:00
/// Gets a value indicating whether the access to the collection is
/// thread safe.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <value>
2019-03-24 20:32:24 +08:00
/// <para>
/// <c>true</c> if the access to the collection is thread safe;
/// otherwise, <c>false</c>.
/// </para>
/// <para>
/// The default value is <c>false</c>.
/// </para>
2014-03-24 20:02:06 +08:00
/// </value>
public bool IsSynchronized {
get {
return false;
}
}
/// <summary>
2019-03-23 20:56:19 +08:00
/// Gets the cookie at the specified index from the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <value>
2019-03-23 20:56:19 +08:00
/// A <see cref="Cookie"/> at the specified index in the collection.
2014-03-24 20:02:06 +08:00
/// </value>
/// <param name="index">
2019-03-23 20:56:19 +08:00
/// An <see cref="int"/> that specifies the zero-based index of the cookie
2014-03-24 20:02:06 +08:00
/// to find.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
2019-03-23 20:56:19 +08:00
/// <paramref name="index"/> is out of allowable range for the collection.
2014-03-24 20:02:06 +08:00
/// </exception>
2014-08-14 21:43:09 +08:00
public Cookie this[int index] {
2014-03-24 20:02:06 +08:00
get {
if (index < 0 || index >= _list.Count)
throw new ArgumentOutOfRangeException ("index");
2014-08-14 21:43:09 +08:00
return _list[index];
2014-03-24 20:02:06 +08:00
}
}
/// <summary>
2019-03-22 20:16:14 +08:00
/// Gets the cookie with the specified name from the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <value>
2019-03-22 20:16:14 +08:00
/// <para>
/// A <see cref="Cookie"/> with the specified name in the collection.
/// </para>
/// <para>
/// <see langword="null"/> if not found.
/// </para>
2014-03-24 20:02:06 +08:00
/// </value>
/// <param name="name">
2019-03-22 20:16:14 +08:00
/// A <see cref="string"/> that specifies the name of the cookie to find.
2014-03-24 20:02:06 +08:00
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="name"/> is <see langword="null"/>.
/// </exception>
2014-08-14 21:43:09 +08:00
public Cookie this[string name] {
2014-03-24 20:02:06 +08:00
get {
if (name == null)
throw new ArgumentNullException ("name");
2019-03-21 20:50:36 +08:00
var compType = StringComparison.InvariantCultureIgnoreCase;
foreach (var cookie in Sorted) {
if (cookie.Name.Equals (name, compType))
2014-03-24 20:02:06 +08:00
return cookie;
2019-03-21 20:50:36 +08:00
}
2014-03-24 20:02:06 +08:00
return null;
}
}
/// <summary>
2014-04-21 19:59:13 +08:00
/// Gets an object used to synchronize access to the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <value>
2019-03-21 20:41:24 +08:00
/// An <see cref="object"/> used to synchronize access to the collection.
2014-03-24 20:02:06 +08:00
/// </value>
2019-03-20 21:55:40 +08:00
public object SyncRoot {
2014-03-24 20:02:06 +08:00
get {
2019-03-20 21:54:40 +08:00
return _sync;
2014-03-24 20:02:06 +08:00
}
}
#endregion
#region Private Methods
2019-03-30 15:53:26 +08:00
private void add (Cookie cookie)
{
2019-04-01 19:37:54 +08:00
var idx = search (cookie);
2019-03-30 15:53:26 +08:00
if (idx == -1) {
_list.Add (cookie);
return;
}
_list[idx] = cookie;
}
2019-04-01 19:43:17 +08:00
private static int compareForSort (Cookie x, Cookie y)
2014-03-26 11:17:58 +08:00
{
2019-03-31 16:53:30 +08:00
return (x.Name.Length + x.Value.Length)
- (y.Name.Length + y.Value.Length);
2014-03-26 11:17:58 +08:00
}
2019-04-02 20:16:23 +08:00
private static int compareForSorted (Cookie x, Cookie y)
2014-03-26 11:17:58 +08:00
{
2019-04-01 19:33:47 +08:00
var ret = x.Version - y.Version;
return ret != 0
2014-03-26 11:17:58 +08:00
? ret
: (ret = x.Name.CompareTo (y.Name)) != 0
? ret
: y.Path.Length - x.Path.Length;
}
2014-03-24 20:02:06 +08:00
private static CookieCollection parseRequest (string value)
{
2019-04-09 20:45:44 +08:00
var ret = new CookieCollection ();
2014-03-24 20:02:06 +08:00
Cookie cookie = null;
2019-04-09 20:45:44 +08:00
var compType = StringComparison.InvariantCultureIgnoreCase;
2014-08-14 21:43:09 +08:00
var ver = 0;
2019-04-09 20:45:44 +08:00
2014-03-26 11:17:58 +08:00
var pairs = splitCookieHeaderValue (value);
2019-04-09 20:45:44 +08:00
2014-08-14 21:43:09 +08:00
for (var i = 0; i < pairs.Length; i++) {
var pair = pairs[i].Trim ();
2014-03-24 20:02:06 +08:00
if (pair.Length == 0)
continue;
2019-04-17 15:07:18 +08:00
var idx = pair.IndexOf ('=');
if (idx == -1) {
if (cookie == null)
continue;
if (pair.Equals ("$port", compType)) {
cookie.Port = "\"\"";
continue;
}
2019-04-09 20:45:44 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-09 20:45:44 +08:00
2019-04-17 15:07:18 +08:00
if (idx == 0) {
if (cookie != null) {
ret.Add (cookie);
cookie = null;
}
2019-04-09 20:45:44 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-09 20:45:44 +08:00
2019-04-17 15:07:18 +08:00
var name = pair.Substring (0, idx).TrimEnd (' ');
var val = idx < pair.Length - 1
? pair.Substring (idx + 1).TrimStart (' ')
: String.Empty;
2019-04-09 20:45:44 +08:00
2019-04-17 15:07:18 +08:00
if (name.Equals ("$version", compType)) {
ver = val.Length > 0 ? Int32.Parse (val.Unquote ()) : 0;
2019-04-09 20:45:44 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-17 15:07:18 +08:00
if (name.Equals ("$path", compType)) {
if (cookie == null)
continue;
2019-04-09 20:45:44 +08:00
2019-04-17 15:07:18 +08:00
if (val.Length == 0)
continue;
cookie.Path = val;
2019-04-09 20:45:44 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-17 15:07:18 +08:00
if (name.Equals ("$domain", compType)) {
if (cookie == null)
continue;
2014-03-24 20:02:06 +08:00
2019-04-17 15:07:18 +08:00
if (val.Length == 0)
continue;
cookie.Domain = val;
2019-04-11 20:27:39 +08:00
continue;
2019-04-17 15:07:18 +08:00
}
if (name.Equals ("$port", compType)) {
if (cookie == null)
continue;
2019-04-11 20:27:39 +08:00
2019-04-17 15:07:18 +08:00
if (val.Length == 0)
continue;
cookie.Port = val;
2019-04-11 20:27:39 +08:00
continue;
2019-04-17 15:07:18 +08:00
}
if (cookie != null)
ret.Add (cookie);
2019-04-09 20:45:44 +08:00
cookie = new Cookie (name, val);
2019-04-11 20:27:39 +08:00
2019-04-09 20:45:44 +08:00
if (ver != 0)
cookie.Version = ver;
2014-03-24 20:02:06 +08:00
}
if (cookie != null)
2019-04-09 20:45:44 +08:00
ret.Add (cookie);
2014-03-24 20:02:06 +08:00
2019-04-09 20:45:44 +08:00
return ret;
2014-03-24 20:02:06 +08:00
}
private static CookieCollection parseResponse (string value)
{
2019-04-12 20:47:39 +08:00
var ret = new CookieCollection ();
2014-03-24 20:02:06 +08:00
Cookie cookie = null;
2019-04-12 20:47:39 +08:00
var compType = StringComparison.InvariantCultureIgnoreCase;
2019-04-16 12:51:36 +08:00
var pairs = value.SplitHeaderValue (',', ';').ToList ();
2019-04-12 20:47:39 +08:00
2019-04-16 12:51:36 +08:00
for (var i = 0; i < pairs.Count; i++) {
2014-08-14 21:43:09 +08:00
var pair = pairs[i].Trim ();
2014-03-24 20:02:06 +08:00
if (pair.Length == 0)
continue;
2019-04-15 20:21:21 +08:00
var idx = pair.IndexOf ('=');
if (idx == -1) {
if (cookie == null)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (pair.Equals ("port", compType)) {
cookie.Port = "\"\"";
continue;
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (pair.Equals ("discard", compType)) {
cookie.Discard = true;
continue;
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (pair.Equals ("secure", compType)) {
cookie.Secure = true;
continue;
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (pair.Equals ("httponly", compType)) {
cookie.HttpOnly = true;
continue;
2019-04-12 20:47:39 +08:00
}
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (idx == 0) {
2019-04-12 20:47:39 +08:00
if (cookie != null) {
2019-04-15 20:21:21 +08:00
ret.Add (cookie);
cookie = null;
2019-04-12 20:47:39 +08:00
}
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
var name = pair.Substring (0, idx).TrimEnd (' ');
var val = idx < pair.Length - 1
? pair.Substring (idx + 1).TrimStart (' ')
: String.Empty;
if (name.Equals ("version", compType)) {
if (cookie == null)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (val.Length == 0)
continue;
cookie.Version = Int32.Parse (val.Unquote ());
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (name.Equals ("expires", compType)) {
if (val.Length == 0)
continue;
2019-04-16 12:51:36 +08:00
if (i == pairs.Count - 1)
2019-04-15 20:21:21 +08:00
break;
i++;
if (cookie == null)
continue;
if (cookie.Expires != DateTime.MinValue)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
var buff = new StringBuilder (val, 32);
buff.AppendFormat (", {0}", pairs[i].Trim ());
DateTime expires;
if (
!DateTime.TryParseExact (
buff.ToString (),
new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
CultureInfo.CreateSpecificCulture ("en-US"),
DateTimeStyles.AdjustToUniversal
| DateTimeStyles.AssumeUniversal,
out expires
)
)
expires = DateTime.Now;
cookie.Expires = expires.ToLocalTime ();
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-15 20:21:21 +08:00
if (name.Equals ("max-age", compType)) {
if (cookie == null)
continue;
if (val.Length == 0)
continue;
var max = Int32.Parse (val.Unquote ());
var expires = DateTime.Now.AddSeconds ((double) max);
cookie.Expires = expires;
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (name.Equals ("path", compType)) {
if (cookie == null)
continue;
if (val.Length == 0)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
cookie.Path = val;
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (name.Equals ("domain", compType)) {
if (cookie == null)
continue;
if (val.Length == 0)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
cookie.Domain = val;
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (name.Equals ("port", compType)) {
if (cookie == null)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (val.Length == 0)
continue;
cookie.Port = val;
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (name.Equals ("comment", compType)) {
if (cookie == null)
continue;
if (val.Length == 0)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
cookie.Comment = urlDecode (val, Encoding.UTF8);
2019-04-12 20:47:39 +08:00
continue;
2014-03-24 20:02:06 +08:00
}
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (name.Equals ("commenturl", compType)) {
if (cookie == null)
continue;
2019-04-12 20:47:39 +08:00
2019-04-15 20:21:21 +08:00
if (val.Length == 0)
continue;
cookie.CommentUri = val.Unquote ().ToUri ();
2019-04-12 20:47:39 +08:00
continue;
}
2019-04-16 12:54:26 +08:00
if (cookie != null)
2019-04-12 20:47:39 +08:00
ret.Add (cookie);
2014-03-24 20:02:06 +08:00
2019-04-12 20:47:39 +08:00
cookie = new Cookie (name, val);
2014-03-24 20:02:06 +08:00
}
if (cookie != null)
2019-04-12 20:47:39 +08:00
ret.Add (cookie);
2014-03-24 20:02:06 +08:00
2019-04-12 20:47:39 +08:00
return ret;
2014-03-24 20:02:06 +08:00
}
2019-04-01 19:37:54 +08:00
private int search (Cookie cookie)
2014-03-24 20:02:06 +08:00
{
2014-08-14 21:43:09 +08:00
for (var i = _list.Count - 1; i >= 0; i--) {
2019-03-27 21:58:30 +08:00
if (_list[i].EqualsWithoutValue (cookie))
2014-03-24 20:02:06 +08:00
return i;
}
return -1;
}
2014-08-14 21:43:09 +08:00
private static string[] splitCookieHeaderValue (string value)
2014-03-24 20:02:06 +08:00
{
2014-03-26 11:17:58 +08:00
return new List<string> (value.SplitHeaderValue (',', ';')).ToArray ();
2014-03-24 20:02:06 +08:00
}
2018-12-10 20:48:10 +08:00
private static string urlDecode (string s, Encoding encoding)
{
if (s == null)
2018-12-15 16:24:52 +08:00
return null;
2018-12-10 20:48:10 +08:00
if (s.IndexOfAny (new[] { '%', '+' }) == -1)
return s;
try {
return HttpUtility.UrlDecode (s, encoding);
}
catch {
return null;
}
}
2014-03-24 20:02:06 +08:00
#endregion
#region Internal Methods
internal static CookieCollection Parse (string value, bool response)
{
return response
? parseResponse (value)
: parseRequest (value);
}
internal void SetOrRemove (Cookie cookie)
{
2019-04-01 19:37:54 +08:00
var idx = search (cookie);
2019-03-28 20:22:28 +08:00
if (idx == -1) {
if (cookie.Expired)
return;
2014-03-24 20:02:06 +08:00
2019-03-28 20:22:28 +08:00
_list.Add (cookie);
2014-03-24 20:02:06 +08:00
return;
}
2019-03-28 20:22:28 +08:00
if (cookie.Expired) {
_list.RemoveAt (idx);
2014-03-24 20:02:06 +08:00
return;
}
2019-03-28 20:22:28 +08:00
_list[idx] = cookie;
2014-03-24 20:02:06 +08:00
}
internal void SetOrRemove (CookieCollection cookies)
{
foreach (Cookie cookie in cookies)
SetOrRemove (cookie);
}
internal void Sort ()
{
2014-03-26 11:17:58 +08:00
if (_list.Count > 1)
2019-04-01 19:43:17 +08:00
_list.Sort (compareForSort);
2014-03-24 20:02:06 +08:00
}
#endregion
#region Public Methods
/// <summary>
2019-03-29 21:03:11 +08:00
/// Adds the specified cookie to the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <param name="cookie">
2014-04-21 19:59:13 +08:00
/// A <see cref="Cookie"/> to add.
2014-03-24 20:02:06 +08:00
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="cookie"/> is <see langword="null"/>.
/// </exception>
2019-03-28 20:27:54 +08:00
public void Add (Cookie cookie)
2014-03-24 20:02:06 +08:00
{
if (cookie == null)
throw new ArgumentNullException ("cookie");
2019-03-30 15:56:07 +08:00
add (cookie);
2014-03-24 20:02:06 +08:00
}
/// <summary>
2019-03-29 21:08:31 +08:00
/// Adds the specified cookies to the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <param name="cookies">
2014-04-21 19:59:13 +08:00
/// A <see cref="CookieCollection"/> that contains the cookies to add.
2014-03-24 20:02:06 +08:00
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="cookies"/> is <see langword="null"/>.
/// </exception>
2019-03-28 20:29:51 +08:00
public void Add (CookieCollection cookies)
2014-03-24 20:02:06 +08:00
{
if (cookies == null)
throw new ArgumentNullException ("cookies");
foreach (Cookie cookie in cookies)
2019-03-30 15:57:17 +08:00
add (cookie);
2014-03-24 20:02:06 +08:00
}
/// <summary>
2019-04-06 16:20:10 +08:00
/// Copies the elements of the collection to the specified array,
/// starting at the specified index.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <param name="array">
2019-04-06 16:20:10 +08:00
/// An <see cref="Array"/> that specifies the destination of
/// the elements copied from the collection.
2014-03-24 20:02:06 +08:00
/// </param>
/// <param name="index">
2019-04-06 16:20:10 +08:00
/// An <see cref="int"/> that specifies the zero-based index in
/// the array at which copying starts.
2014-03-24 20:02:06 +08:00
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="array"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is less than zero.
/// </exception>
/// <exception cref="ArgumentException">
/// <para>
/// <paramref name="array"/> is multidimensional.
/// </para>
/// <para>
/// -or-
/// </para>
/// <para>
2019-04-06 16:20:10 +08:00
/// The space from <paramref name="index"/> to the end of
/// <paramref name="array"/> is not enough to copy to.
2014-03-24 20:02:06 +08:00
/// </para>
/// </exception>
/// <exception cref="InvalidCastException">
2019-04-06 16:20:10 +08:00
/// The element type of <paramref name="array"/> cannot be assigned.
2014-03-24 20:02:06 +08:00
/// </exception>
public void CopyTo (Array array, int index)
{
if (array == null)
throw new ArgumentNullException ("array");
if (index < 0)
2014-04-21 19:59:13 +08:00
throw new ArgumentOutOfRangeException ("index", "Less than zero.");
2014-03-24 20:02:06 +08:00
if (array.Rank > 1)
2014-04-21 19:59:13 +08:00
throw new ArgumentException ("Multidimensional.", "array");
2014-03-24 20:02:06 +08:00
2019-04-05 15:03:11 +08:00
if (array.Length - index < _list.Count) {
var msg = "The available space of the array is not enough to copy to.";
throw new ArgumentException (msg);
}
2014-03-24 20:02:06 +08:00
2019-04-05 15:03:11 +08:00
var elmType = array.GetType ().GetElementType ();
if (!elmType.IsAssignableFrom (typeof (Cookie))) {
var msg = "The element type of the array cannot be assigned.";
throw new InvalidCastException (msg);
}
2014-03-24 20:02:06 +08:00
2014-03-26 11:17:58 +08:00
((IList) _list).CopyTo (array, index);
2014-03-24 20:02:06 +08:00
}
/// <summary>
2019-04-04 16:17:09 +08:00
/// Copies the elements of the collection to the specified array,
2019-04-06 16:23:30 +08:00
/// starting at the specified index.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <param name="array">
2019-04-04 16:17:09 +08:00
/// An array of <see cref="Cookie"/> that specifies the destination of
/// the elements copied from the collection.
2014-03-24 20:02:06 +08:00
/// </param>
/// <param name="index">
2019-04-06 16:23:30 +08:00
/// An <see cref="int"/> that specifies the zero-based index in
2019-04-04 16:17:09 +08:00
/// the array at which copying starts.
2014-03-24 20:02:06 +08:00
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="array"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is less than zero.
/// </exception>
/// <exception cref="ArgumentException">
2019-04-04 16:17:09 +08:00
/// The space from <paramref name="index"/> to the end of
/// <paramref name="array"/> is not enough to copy to.
2014-03-24 20:02:06 +08:00
/// </exception>
2014-08-14 21:43:09 +08:00
public void CopyTo (Cookie[] array, int index)
2014-03-24 20:02:06 +08:00
{
if (array == null)
throw new ArgumentNullException ("array");
if (index < 0)
2014-04-21 19:59:13 +08:00
throw new ArgumentOutOfRangeException ("index", "Less than zero.");
2014-03-24 20:02:06 +08:00
2019-04-03 21:03:36 +08:00
if (array.Length - index < _list.Count) {
2019-04-04 16:19:15 +08:00
var msg = "The available space of the array is not enough to copy to.";
2019-04-03 21:03:36 +08:00
throw new ArgumentException (msg);
}
2014-03-24 20:02:06 +08:00
_list.CopyTo (array, index);
}
/// <summary>
2014-04-21 19:59:13 +08:00
/// Gets the enumerator used to iterate through the collection.
2014-03-24 20:02:06 +08:00
/// </summary>
/// <returns>
2019-04-02 20:18:32 +08:00
/// An <see cref="IEnumerator"/> instance used to iterate through
/// the collection.
2014-03-24 20:02:06 +08:00
/// </returns>
public IEnumerator GetEnumerator ()
{
return _list.GetEnumerator ();
}
#endregion
}
2012-09-10 00:36:22 +08:00
}