| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | |
| | | 8 | | namespace System.Net |
| | | 9 | | { |
| | | 10 | | // CookieCollection |
| | | 11 | | // |
| | | 12 | | // A list of cookies maintained in Sorted order. Only one cookie with matching Name/Domain/Path |
| | | 13 | | [Serializable] |
| | | 14 | | [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 |
| | | 15 | | public class CookieCollection : ICollection<Cookie>, IReadOnlyCollection<Cookie>, ICollection |
| | | 16 | | { |
| | | 17 | | internal enum Stamp |
| | | 18 | | { |
| | | 19 | | Check = 0, |
| | | 20 | | Set = 1, |
| | | 21 | | SetToUnused = 2, |
| | | 22 | | SetToMaxUsed = 3, |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | private readonly ArrayList m_list = new ArrayList(); |
| | | 26 | | |
| | | 27 | | private int m_version; // Do not rename (binary serialization). This field only exists for .NET Framework serial |
| | 0 | 28 | | private DateTime m_TimeStamp = DateTime.MinValue; // Do not rename (binary serialization) |
| | | 29 | | private bool m_has_other_versions; // Do not rename (binary serialization) |
| | | 30 | | |
| | 0 | 31 | | public CookieCollection() |
| | 0 | 32 | | { |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public Cookie this[int index] |
| | | 36 | | { |
| | | 37 | | get |
| | 0 | 38 | | { |
| | 0 | 39 | | ArgumentOutOfRangeException.ThrowIfNegative(index); |
| | 0 | 40 | | ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, m_list.Count); |
| | | 41 | | |
| | 0 | 42 | | return (m_list[index] as Cookie)!; |
| | 0 | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public Cookie? this[string name] |
| | | 47 | | { |
| | | 48 | | get |
| | 0 | 49 | | { |
| | 0 | 50 | | foreach (Cookie? c in m_list) |
| | 0 | 51 | | { |
| | 0 | 52 | | if (string.Equals(c!.Name, name, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 53 | | { |
| | 0 | 54 | | return c; |
| | | 55 | | } |
| | 0 | 56 | | } |
| | 0 | 57 | | return null; |
| | 0 | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [OnSerializing] |
| | | 62 | | private void OnSerializing(StreamingContext context) |
| | 0 | 63 | | { |
| | 0 | 64 | | m_version = m_list.Count; |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | public void Add(Cookie cookie) |
| | 0 | 68 | | { |
| | 0 | 69 | | ArgumentNullException.ThrowIfNull(cookie); |
| | | 70 | | |
| | 0 | 71 | | int idx = IndexOf(cookie); |
| | 0 | 72 | | if (idx == -1) |
| | 0 | 73 | | { |
| | 0 | 74 | | m_list.Add(cookie); |
| | 0 | 75 | | } |
| | | 76 | | else |
| | 0 | 77 | | { |
| | 0 | 78 | | m_list[idx] = cookie; |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public void Add(CookieCollection cookies) |
| | 0 | 83 | | { |
| | 0 | 84 | | ArgumentNullException.ThrowIfNull(cookies); |
| | | 85 | | |
| | 0 | 86 | | foreach (Cookie? cookie in cookies.m_list) |
| | 0 | 87 | | { |
| | 0 | 88 | | Add(cookie!); |
| | 0 | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public void Clear() |
| | 0 | 93 | | { |
| | 0 | 94 | | m_list.Clear(); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public bool Contains(Cookie cookie) |
| | 0 | 98 | | { |
| | 0 | 99 | | return IndexOf(cookie) >= 0; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public bool Remove(Cookie cookie) |
| | 0 | 103 | | { |
| | 0 | 104 | | int idx = IndexOf(cookie); |
| | 0 | 105 | | if (idx == -1) |
| | 0 | 106 | | { |
| | 0 | 107 | | return false; |
| | | 108 | | } |
| | 0 | 109 | | m_list.RemoveAt(idx); |
| | 0 | 110 | | return true; |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | public bool IsReadOnly |
| | | 114 | | { |
| | | 115 | | get |
| | 0 | 116 | | { |
| | 0 | 117 | | return false; |
| | 0 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public int Count |
| | | 122 | | { |
| | | 123 | | get |
| | 0 | 124 | | { |
| | 0 | 125 | | return m_list.Count; |
| | 0 | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public bool IsSynchronized |
| | | 130 | | { |
| | | 131 | | get |
| | 0 | 132 | | { |
| | 0 | 133 | | return false; |
| | 0 | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public object SyncRoot |
| | | 138 | | { |
| | | 139 | | get |
| | 0 | 140 | | { |
| | 0 | 141 | | return this; |
| | 0 | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public void CopyTo(Array array, int index) |
| | 0 | 146 | | { |
| | 0 | 147 | | ((ICollection)m_list).CopyTo(array, index); |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | public void CopyTo(Cookie[] array, int index) |
| | 0 | 151 | | { |
| | 0 | 152 | | m_list.CopyTo(array, index); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | internal DateTime TimeStamp(Stamp how) |
| | 0 | 156 | | { |
| | 0 | 157 | | switch (how) |
| | | 158 | | { |
| | | 159 | | case Stamp.Set: |
| | 0 | 160 | | m_TimeStamp = DateTime.Now; |
| | 0 | 161 | | break; |
| | | 162 | | case Stamp.SetToMaxUsed: |
| | 0 | 163 | | m_TimeStamp = DateTime.MaxValue; |
| | 0 | 164 | | break; |
| | | 165 | | case Stamp.SetToUnused: |
| | 0 | 166 | | m_TimeStamp = DateTime.MinValue; |
| | 0 | 167 | | break; |
| | | 168 | | case Stamp.Check: |
| | | 169 | | default: |
| | 0 | 170 | | break; |
| | | 171 | | } |
| | 0 | 172 | | return m_TimeStamp; |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | |
| | | 176 | | // This is for internal cookie container usage. |
| | | 177 | | // For others not that _hasOtherVersions gets changed ONLY in InternalAdd |
| | | 178 | | internal bool IsOtherVersionSeen |
| | | 179 | | { |
| | | 180 | | get |
| | 0 | 181 | | { |
| | 0 | 182 | | return m_has_other_versions; |
| | 0 | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | |
| | | 187 | | // If isStrict == false, assumes that incoming cookie is unique. |
| | | 188 | | // If isStrict == true, replace the cookie if found same with newest Variant. |
| | | 189 | | // Returns 1 if added, 0 if replaced or rejected. |
| | | 190 | | |
| | | 191 | | internal int InternalAdd(Cookie cookie, bool isStrict) |
| | 0 | 192 | | { |
| | 0 | 193 | | int ret = 1; |
| | 0 | 194 | | if (isStrict) |
| | 0 | 195 | | { |
| | 0 | 196 | | int idx = 0; |
| | 0 | 197 | | int listCount = m_list.Count; |
| | 0 | 198 | | for (int i = 0; i < listCount; i++) |
| | 0 | 199 | | { |
| | 0 | 200 | | Cookie c = (Cookie)m_list[i]!; |
| | 0 | 201 | | if (CookieComparer.Equals(cookie, c)) |
| | 0 | 202 | | { |
| | 0 | 203 | | ret = 0; // Will replace or reject |
| | | 204 | | |
| | | 205 | | // Cookie2 spec requires that new Variant cookie overwrite the old one. |
| | 0 | 206 | | if (c!.Variant <= cookie.Variant) |
| | 0 | 207 | | { |
| | 0 | 208 | | m_list[idx] = cookie; |
| | 0 | 209 | | } |
| | 0 | 210 | | break; |
| | | 211 | | } |
| | 0 | 212 | | ++idx; |
| | 0 | 213 | | } |
| | 0 | 214 | | if (idx == m_list.Count) |
| | 0 | 215 | | { |
| | 0 | 216 | | m_list.Add(cookie); |
| | 0 | 217 | | } |
| | 0 | 218 | | } |
| | | 219 | | else |
| | 0 | 220 | | { |
| | 0 | 221 | | m_list.Add(cookie); |
| | 0 | 222 | | } |
| | 0 | 223 | | if (cookie.Version != Cookie.MaxSupportedVersion) |
| | 0 | 224 | | { |
| | 0 | 225 | | m_has_other_versions = true; |
| | 0 | 226 | | } |
| | 0 | 227 | | return ret; |
| | 0 | 228 | | } |
| | | 229 | | |
| | | 230 | | internal int IndexOf(Cookie cookie) |
| | 0 | 231 | | { |
| | 0 | 232 | | int idx = 0; |
| | 0 | 233 | | foreach (Cookie? c in m_list) |
| | 0 | 234 | | { |
| | 0 | 235 | | if (CookieComparer.Equals(cookie, c!)) |
| | 0 | 236 | | { |
| | 0 | 237 | | return idx; |
| | | 238 | | } |
| | 0 | 239 | | ++idx; |
| | 0 | 240 | | } |
| | 0 | 241 | | return -1; |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | internal void RemoveAt(int idx) |
| | 0 | 245 | | { |
| | 0 | 246 | | m_list.RemoveAt(idx); |
| | 0 | 247 | | } |
| | | 248 | | |
| | | 249 | | IEnumerator<Cookie> IEnumerable<Cookie>.GetEnumerator() |
| | 0 | 250 | | { |
| | 0 | 251 | | foreach (Cookie? cookie in m_list) |
| | 0 | 252 | | { |
| | 0 | 253 | | yield return cookie!; |
| | 0 | 254 | | } |
| | 0 | 255 | | } |
| | | 256 | | |
| | | 257 | | public IEnumerator GetEnumerator() |
| | 0 | 258 | | { |
| | 0 | 259 | | return m_list.GetEnumerator(); |
| | 0 | 260 | | } |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |