| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | public sealed class AddressHeaderCollection : System.Collections.ObjectModel.ReadOnlyCollection<AddressHeader> |
| | | 12 | | { |
| | 3 | 13 | | private static readonly AddressHeaderCollection s_emptyHeaderCollection = new AddressHeaderCollection(); |
| | | 14 | | |
| | | 15 | | public AddressHeaderCollection() |
| | 672 | 16 | | : base(new List<AddressHeader>()) |
| | | 17 | | { |
| | 672 | 18 | | } |
| | | 19 | | |
| | | 20 | | public AddressHeaderCollection(IEnumerable<AddressHeader> addressHeaders) |
| | 0 | 21 | | : base(new List<AddressHeader>(addressHeaders)) |
| | | 22 | | { |
| | | 23 | | // avoid allocating an enumerator when possible |
| | 0 | 24 | | if (addressHeaders is IList<AddressHeader> collection) |
| | | 25 | | { |
| | 0 | 26 | | for (int i = 0; i < collection.Count; i++) |
| | | 27 | | { |
| | 0 | 28 | | if (collection[i] == null) |
| | | 29 | | { |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 31 | | new ArgumentException(SR.MessageHeaderIsNull0)); |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | 0 | 37 | | foreach (AddressHeader addressHeader in addressHeaders) |
| | | 38 | | { |
| | 0 | 39 | | if (addressHeader == null) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 42 | | new ArgumentException(SR.MessageHeaderIsNull0)); |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | private int InternalCount |
| | | 49 | | { |
| | | 50 | | get |
| | | 51 | | { |
| | 22 | 52 | | if (this == (object)s_emptyHeaderCollection) |
| | | 53 | | { |
| | 0 | 54 | | return 0; |
| | | 55 | | } |
| | | 56 | | |
| | 22 | 57 | | return Count; |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public void AddHeadersTo(Message message) |
| | | 62 | | { |
| | 10 | 63 | | if (message == null) |
| | | 64 | | { |
| | 0 | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 66 | | } |
| | | 67 | | |
| | 20 | 68 | | for (int i = 0; i < InternalCount; i++) |
| | | 69 | | { |
| | 0 | 70 | | message.Headers.Add(this[i].ToMessageHeader()); |
| | | 71 | | } |
| | 10 | 72 | | } |
| | | 73 | | |
| | | 74 | | public AddressHeader[] FindAll(string name, string ns) |
| | | 75 | | { |
| | 0 | 76 | | if (name == null) |
| | | 77 | | { |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | if (ns == null) |
| | | 82 | | { |
| | 0 | 83 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns)); |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | List<AddressHeader> results = new List<AddressHeader>(); |
| | 0 | 87 | | for (int i = 0; i < Count; i++) |
| | | 88 | | { |
| | 0 | 89 | | AddressHeader header = this[i]; |
| | 0 | 90 | | if (header.Name == name && header.Namespace == ns) |
| | | 91 | | { |
| | 0 | 92 | | results.Add(header); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return results.ToArray(); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public AddressHeader FindHeader(string name, string ns) |
| | | 100 | | { |
| | 0 | 101 | | if (name == null) |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | if (ns == null) |
| | | 107 | | { |
| | 0 | 108 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns)); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | AddressHeader matchingHeader = null; |
| | | 112 | | |
| | 0 | 113 | | for (int i = 0; i < Count; i++) |
| | | 114 | | { |
| | 0 | 115 | | AddressHeader header = this[i]; |
| | 0 | 116 | | if (header.Name == name && header.Namespace == ns) |
| | | 117 | | { |
| | 0 | 118 | | if (matchingHeader != null) |
| | | 119 | | { |
| | 0 | 120 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Mul |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | matchingHeader = header; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | return matchingHeader; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | internal bool IsEquivalent(AddressHeaderCollection col) |
| | | 131 | | { |
| | 3 | 132 | | if (InternalCount != col.InternalCount) |
| | | 133 | | { |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | | 136 | | |
| | 3 | 137 | | StringBuilder builder = new StringBuilder(); |
| | 3 | 138 | | Dictionary<string, int> myHeaders = new Dictionary<string, int>(); |
| | 3 | 139 | | PopulateHeaderDictionary(builder, myHeaders); |
| | | 140 | | |
| | 3 | 141 | | Dictionary<string, int> otherHeaders = new Dictionary<string, int>(); |
| | 3 | 142 | | col.PopulateHeaderDictionary(builder, otherHeaders); |
| | | 143 | | |
| | 3 | 144 | | if (myHeaders.Count != otherHeaders.Count) |
| | | 145 | | { |
| | 0 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 6 | 149 | | foreach (KeyValuePair<string, int> pair in myHeaders) |
| | | 150 | | { |
| | 0 | 151 | | if (otherHeaders.TryGetValue(pair.Key, out int count)) |
| | | 152 | | { |
| | 0 | 153 | | if (count != pair.Value) |
| | | 154 | | { |
| | 0 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | else |
| | | 159 | | { |
| | 0 | 160 | | return false; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | 3 | 164 | | return true; |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | internal void PopulateHeaderDictionary(StringBuilder builder, Dictionary<string, int> headers) |
| | | 168 | | { |
| | | 169 | | string key; |
| | 12 | 170 | | for (int i = 0; i < InternalCount; ++i) |
| | | 171 | | { |
| | 0 | 172 | | builder.Remove(0, builder.Length); |
| | 0 | 173 | | key = this[i].GetComparableForm(builder); |
| | 0 | 174 | | if (headers.ContainsKey(key)) |
| | | 175 | | { |
| | 0 | 176 | | headers[key] = headers[key] + 1; |
| | | 177 | | } |
| | | 178 | | else |
| | | 179 | | { |
| | 0 | 180 | | headers.Add(key, 1); |
| | | 181 | | } |
| | | 182 | | } |
| | 6 | 183 | | } |
| | | 184 | | |
| | | 185 | | internal static AddressHeaderCollection ReadServiceParameters(XmlDictionaryReader reader) |
| | | 186 | | { |
| | 0 | 187 | | return ReadServiceParameters(reader, false); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | internal static AddressHeaderCollection ReadServiceParameters(XmlDictionaryReader reader, bool isReferenceProper |
| | | 191 | | { |
| | 0 | 192 | | reader.MoveToContent(); |
| | 0 | 193 | | if (reader.IsEmptyElement) |
| | | 194 | | { |
| | 0 | 195 | | reader.Skip(); |
| | 0 | 196 | | return null; |
| | | 197 | | } |
| | | 198 | | else |
| | | 199 | | { |
| | 0 | 200 | | reader.ReadStartElement(); |
| | 0 | 201 | | List<AddressHeader> headerList = new List<AddressHeader>(); |
| | 0 | 202 | | while (reader.IsStartElement()) |
| | | 203 | | { |
| | 0 | 204 | | headerList.Add(new BufferedAddressHeader(reader, isReferenceProperty)); |
| | | 205 | | } |
| | 0 | 206 | | reader.ReadEndElement(); |
| | 0 | 207 | | return new AddressHeaderCollection(headerList); |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | internal void WriteContentsTo(XmlDictionaryWriter writer) |
| | | 212 | | { |
| | 0 | 213 | | for (int i = 0; i < InternalCount; i++) |
| | | 214 | | { |
| | 0 | 215 | | this[i].WriteAddressHeader(writer); |
| | | 216 | | } |
| | 0 | 217 | | } |
| | | 218 | | } |
| | | 219 | | } |