< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 142
Coverable lines: 142
Total lines: 263
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 33
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
OnSerializing(...)100%110%
Add(...)0%220%
Add(...)0%220%
Clear()100%110%
Contains(...)100%110%
Remove(...)0%220%
CopyTo(...)100%110%
CopyTo(...)100%110%
TimeStamp(...)0%550%
InternalAdd(...)0%12120%
IndexOf(...)0%440%
RemoveAt(...)100%110%
System-Collections-Generic-IEnumerable<System-Net-Cookie>-GetEnumerator()0%220%
GetEnumerator()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Primitives/src/System/Net/CookieCollection.cs

#LineLine coverage
 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
 4using System.Collections;
 5using System.Collections.Generic;
 6using System.Runtime.Serialization;
 7
 8namespace 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
 025        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
 028        private DateTime m_TimeStamp = DateTime.MinValue; // Do not rename (binary serialization)
 29        private bool m_has_other_versions; // Do not rename (binary serialization)
 30
 031        public CookieCollection()
 032        {
 033        }
 34
 35        public Cookie this[int index]
 36        {
 37            get
 038            {
 039                ArgumentOutOfRangeException.ThrowIfNegative(index);
 040                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, m_list.Count);
 41
 042                return (m_list[index] as Cookie)!;
 043            }
 44        }
 45
 46        public Cookie? this[string name]
 47        {
 48            get
 049            {
 050                foreach (Cookie? c in m_list)
 051                {
 052                    if (string.Equals(c!.Name, name, StringComparison.OrdinalIgnoreCase))
 053                    {
 054                        return c;
 55                    }
 056                }
 057                return null;
 058            }
 59        }
 60
 61        [OnSerializing]
 62        private void OnSerializing(StreamingContext context)
 063        {
 064            m_version = m_list.Count;
 065        }
 66
 67        public void Add(Cookie cookie)
 068        {
 069            ArgumentNullException.ThrowIfNull(cookie);
 70
 071            int idx = IndexOf(cookie);
 072            if (idx == -1)
 073            {
 074                m_list.Add(cookie);
 075            }
 76            else
 077            {
 078                m_list[idx] = cookie;
 079            }
 080        }
 81
 82        public void Add(CookieCollection cookies)
 083        {
 084            ArgumentNullException.ThrowIfNull(cookies);
 85
 086            foreach (Cookie? cookie in cookies.m_list)
 087            {
 088                Add(cookie!);
 089            }
 090        }
 91
 92        public void Clear()
 093        {
 094            m_list.Clear();
 095        }
 96
 97        public bool Contains(Cookie cookie)
 098        {
 099            return IndexOf(cookie) >= 0;
 0100        }
 101
 102        public bool Remove(Cookie cookie)
 0103        {
 0104            int idx = IndexOf(cookie);
 0105            if (idx == -1)
 0106            {
 0107                return false;
 108            }
 0109            m_list.RemoveAt(idx);
 0110            return true;
 0111        }
 112
 113        public bool IsReadOnly
 114        {
 115            get
 0116            {
 0117                return false;
 0118            }
 119        }
 120
 121        public int Count
 122        {
 123            get
 0124            {
 0125                return m_list.Count;
 0126            }
 127        }
 128
 129        public bool IsSynchronized
 130        {
 131            get
 0132            {
 0133                return false;
 0134            }
 135        }
 136
 137        public object SyncRoot
 138        {
 139            get
 0140            {
 0141                return this;
 0142            }
 143        }
 144
 145        public void CopyTo(Array array, int index)
 0146        {
 0147            ((ICollection)m_list).CopyTo(array, index);
 0148        }
 149
 150        public void CopyTo(Cookie[] array, int index)
 0151        {
 0152            m_list.CopyTo(array, index);
 0153        }
 154
 155        internal DateTime TimeStamp(Stamp how)
 0156        {
 0157            switch (how)
 158            {
 159                case Stamp.Set:
 0160                    m_TimeStamp = DateTime.Now;
 0161                    break;
 162                case Stamp.SetToMaxUsed:
 0163                    m_TimeStamp = DateTime.MaxValue;
 0164                    break;
 165                case Stamp.SetToUnused:
 0166                    m_TimeStamp = DateTime.MinValue;
 0167                    break;
 168                case Stamp.Check:
 169                default:
 0170                    break;
 171            }
 0172            return m_TimeStamp;
 0173        }
 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
 0181            {
 0182                return m_has_other_versions;
 0183            }
 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)
 0192        {
 0193            int ret = 1;
 0194            if (isStrict)
 0195            {
 0196                int idx = 0;
 0197                int listCount = m_list.Count;
 0198                for (int i = 0; i < listCount; i++)
 0199                {
 0200                    Cookie c = (Cookie)m_list[i]!;
 0201                    if (CookieComparer.Equals(cookie, c))
 0202                    {
 0203                        ret = 0; // Will replace or reject
 204
 205                        // Cookie2 spec requires that new Variant cookie overwrite the old one.
 0206                        if (c!.Variant <= cookie.Variant)
 0207                        {
 0208                            m_list[idx] = cookie;
 0209                        }
 0210                        break;
 211                    }
 0212                    ++idx;
 0213                }
 0214                if (idx == m_list.Count)
 0215                {
 0216                    m_list.Add(cookie);
 0217                }
 0218            }
 219            else
 0220            {
 0221                m_list.Add(cookie);
 0222            }
 0223            if (cookie.Version != Cookie.MaxSupportedVersion)
 0224            {
 0225                m_has_other_versions = true;
 0226            }
 0227            return ret;
 0228        }
 229
 230        internal int IndexOf(Cookie cookie)
 0231        {
 0232            int idx = 0;
 0233            foreach (Cookie? c in m_list)
 0234            {
 0235                if (CookieComparer.Equals(cookie, c!))
 0236                {
 0237                    return idx;
 238                }
 0239                ++idx;
 0240            }
 0241            return -1;
 0242        }
 243
 244        internal void RemoveAt(int idx)
 0245        {
 0246            m_list.RemoveAt(idx);
 0247        }
 248
 249        IEnumerator<Cookie> IEnumerable<Cookie>.GetEnumerator()
 0250        {
 0251            foreach (Cookie? cookie in m_list)
 0252            {
 0253                yield return cookie!;
 0254            }
 0255        }
 256
 257        public IEnumerator GetEnumerator()
 0258        {
 0259            return m_list.GetEnumerator();
 0260        }
 261    }
 262}
 263