|
| 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 | +#pragma warning disable ASPDEPR005 // Type or member is obsolete |
| 5 | + |
| 6 | +using AspNetIPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork; |
| 7 | +using IPAddress = System.Net.IPAddress; |
| 8 | +using IPNetwork = System.Net.IPNetwork; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Builder; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Internal list implementation that keeps <see cref="System.Net.IPNetwork"/> and the obsolete |
| 14 | +/// <see cref="Microsoft.AspNetCore.HttpOverrides.IPNetwork"/> collections in sync. Modifications |
| 15 | +/// through either interface are reflected in the other. |
| 16 | +/// </summary> |
| 17 | +internal sealed class DualIPNetworkList : IList<IPNetwork>, IList<AspNetIPNetwork> |
| 18 | +{ |
| 19 | + private readonly List<(IPNetwork system, AspNetIPNetwork aspnet)> _items = new(); |
| 20 | + |
| 21 | + public DualIPNetworkList() |
| 22 | + { |
| 23 | + Add(new IPNetwork(IPAddress.Loopback, 8)); |
| 24 | + } |
| 25 | + |
| 26 | + int ICollection<IPNetwork>.Count => _items.Count; |
| 27 | + int ICollection<AspNetIPNetwork>.Count => _items.Count; |
| 28 | + |
| 29 | + bool ICollection<IPNetwork>.IsReadOnly => false; |
| 30 | + bool ICollection<AspNetIPNetwork>.IsReadOnly => false; |
| 31 | + |
| 32 | + IPNetwork IList<IPNetwork>.this[int index] |
| 33 | + { |
| 34 | + get => _items[index].system; |
| 35 | + set => _items[index] = (value, new AspNetIPNetwork(value.BaseAddress, value.PrefixLength)); |
| 36 | + } |
| 37 | + |
| 38 | + AspNetIPNetwork IList<AspNetIPNetwork>.this[int index] |
| 39 | + { |
| 40 | + get => _items[index].aspnet; |
| 41 | + set => _items[index] = (new IPNetwork(value.Prefix, value.PrefixLength), value); |
| 42 | + } |
| 43 | + |
| 44 | + public void Add(IPNetwork item) |
| 45 | + { |
| 46 | + _items.Add((item, new AspNetIPNetwork(item.BaseAddress, item.PrefixLength))); |
| 47 | + } |
| 48 | + |
| 49 | + public void Add(AspNetIPNetwork item) |
| 50 | + { |
| 51 | + _items.Add((new IPNetwork(item.Prefix, item.PrefixLength), item)); |
| 52 | + } |
| 53 | + |
| 54 | + void ICollection<IPNetwork>.Add(IPNetwork item) => Add(item); |
| 55 | + void ICollection<AspNetIPNetwork>.Add(AspNetIPNetwork item) => Add(item); |
| 56 | + |
| 57 | + public void Clear() => _items.Clear(); |
| 58 | + void ICollection<IPNetwork>.Clear() => Clear(); |
| 59 | + void ICollection<AspNetIPNetwork>.Clear() => Clear(); |
| 60 | + |
| 61 | + public bool Contains(IPNetwork item) |
| 62 | + { |
| 63 | + var text = item.ToString(); |
| 64 | + for (var i = 0; i < _items.Count; i++) |
| 65 | + { |
| 66 | + if (string.Equals(_items[i].system.ToString(), text, StringComparison.Ordinal)) |
| 67 | + { |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + public bool Contains(AspNetIPNetwork item) |
| 75 | + { |
| 76 | + for (var i = 0; i < _items.Count; i++) |
| 77 | + { |
| 78 | + if (_items[i].aspnet.Prefix.Equals(item.Prefix) && _items[i].aspnet.PrefixLength == item.PrefixLength) |
| 79 | + { |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + bool ICollection<IPNetwork>.Contains(IPNetwork item) => Contains(item); |
| 87 | + bool ICollection<AspNetIPNetwork>.Contains(AspNetIPNetwork item) => Contains(item); |
| 88 | + |
| 89 | + public void CopyTo(IPNetwork[] array, int arrayIndex) |
| 90 | + { |
| 91 | + for (var i = 0; i < _items.Count; i++) |
| 92 | + { |
| 93 | + array[arrayIndex + i] = _items[i].system; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public void CopyTo(AspNetIPNetwork[] array, int arrayIndex) |
| 98 | + { |
| 99 | + for (var i = 0; i < _items.Count; i++) |
| 100 | + { |
| 101 | + array[arrayIndex + i] = _items[i].aspnet; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void ICollection<IPNetwork>.CopyTo(IPNetwork[] array, int arrayIndex) => CopyTo(array, arrayIndex); |
| 106 | + void ICollection<AspNetIPNetwork>.CopyTo(AspNetIPNetwork[] array, int arrayIndex) => CopyTo(array, arrayIndex); |
| 107 | + |
| 108 | + public IEnumerator<IPNetwork> GetEnumerator() |
| 109 | + { |
| 110 | + for (var i = 0; i < _items.Count; i++) |
| 111 | + { |
| 112 | + yield return _items[i].system; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
| 117 | + |
| 118 | + IEnumerator<AspNetIPNetwork> IEnumerable<AspNetIPNetwork>.GetEnumerator() |
| 119 | + { |
| 120 | + for (var i = 0; i < _items.Count; i++) |
| 121 | + { |
| 122 | + yield return _items[i].aspnet; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public int IndexOf(IPNetwork item) |
| 127 | + { |
| 128 | + var text = item.ToString(); |
| 129 | + for (var i = 0; i < _items.Count; i++) |
| 130 | + { |
| 131 | + if (string.Equals(_items[i].system.ToString(), text, StringComparison.Ordinal)) |
| 132 | + { |
| 133 | + return i; |
| 134 | + } |
| 135 | + } |
| 136 | + return -1; |
| 137 | + } |
| 138 | + |
| 139 | + public int IndexOf(AspNetIPNetwork item) |
| 140 | + { |
| 141 | + for (var i = 0; i < _items.Count; i++) |
| 142 | + { |
| 143 | + var n = _items[i].aspnet; |
| 144 | + if (n.Prefix.Equals(item.Prefix) && n.PrefixLength == item.PrefixLength) |
| 145 | + { |
| 146 | + return i; |
| 147 | + } |
| 148 | + } |
| 149 | + return -1; |
| 150 | + } |
| 151 | + |
| 152 | + int IList<IPNetwork>.IndexOf(IPNetwork item) => IndexOf(item); |
| 153 | + int IList<AspNetIPNetwork>.IndexOf(AspNetIPNetwork item) => IndexOf(item); |
| 154 | + |
| 155 | + public void Insert(int index, IPNetwork item) |
| 156 | + { |
| 157 | + _items.Insert(index, (item, new AspNetIPNetwork(item.BaseAddress, item.PrefixLength))); |
| 158 | + } |
| 159 | + |
| 160 | + public void Insert(int index, AspNetIPNetwork item) |
| 161 | + { |
| 162 | + _items.Insert(index, (new IPNetwork(item.Prefix, item.PrefixLength), item)); |
| 163 | + } |
| 164 | + |
| 165 | + void IList<IPNetwork>.Insert(int index, IPNetwork item) => Insert(index, item); |
| 166 | + void IList<AspNetIPNetwork>.Insert(int index, AspNetIPNetwork item) => Insert(index, item); |
| 167 | + |
| 168 | + public bool Remove(IPNetwork item) |
| 169 | + { |
| 170 | + var idx = IndexOf(item); |
| 171 | + if (idx >= 0) |
| 172 | + { |
| 173 | + _items.RemoveAt(idx); |
| 174 | + return true; |
| 175 | + } |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + public bool Remove(AspNetIPNetwork item) |
| 180 | + { |
| 181 | + var idx = IndexOf(item); |
| 182 | + if (idx >= 0) |
| 183 | + { |
| 184 | + _items.RemoveAt(idx); |
| 185 | + return true; |
| 186 | + } |
| 187 | + return false; |
| 188 | + } |
| 189 | + |
| 190 | + bool ICollection<IPNetwork>.Remove(IPNetwork item) => Remove(item); |
| 191 | + bool ICollection<AspNetIPNetwork>.Remove(AspNetIPNetwork item) => Remove(item); |
| 192 | + |
| 193 | + public void RemoveAt(int index) => _items.RemoveAt(index); |
| 194 | + void IList<IPNetwork>.RemoveAt(int index) => RemoveAt(index); |
| 195 | + void IList<AspNetIPNetwork>.RemoveAt(int index) => RemoveAt(index); |
| 196 | +} |
| 197 | + |
| 198 | +#pragma warning restore ASPDEPR005 // Type or member is obsolete |
0 commit comments