| | | 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.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Dispatcher |
| | | 13 | | { |
| | | 14 | | internal class EndpointAddressProcessor |
| | | 15 | | { |
| | 0 | 16 | | internal static readonly QNameKeyComparer QNameComparer = new QNameKeyComparer(); |
| | | 17 | | |
| | | 18 | | // QName Attributes |
| | | 19 | | //internal static readonly string XsiNs = XmlSchema.InstanceNamespace; |
| | 0 | 20 | | internal static readonly string XsiNs = "http://www.w3.org/2001/XMLSchema-instance"; |
| | | 21 | | internal const string SerNs = "http://schemas.microsoft.com/2003/10/Serialization/"; |
| | | 22 | | internal const string TypeLN = "type"; |
| | | 23 | | internal const string ItemTypeLN = "ItemType"; |
| | | 24 | | internal const string FactoryTypeLN = "FactoryType"; |
| | | 25 | | |
| | | 26 | | // Pooling |
| | | 27 | | private readonly StringBuilder _builder; |
| | | 28 | | private byte[] _resultData; |
| | | 29 | | |
| | 0 | 30 | | internal EndpointAddressProcessor(int length) |
| | | 31 | | { |
| | 0 | 32 | | _builder = new StringBuilder(); |
| | 0 | 33 | | _resultData = new byte[length]; |
| | 0 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | internal EndpointAddressProcessor Next { get; set; } |
| | | 37 | | |
| | | 38 | | internal static string GetComparableForm(StringBuilder builder, XmlReader reader) |
| | | 39 | | { |
| | 0 | 40 | | List<Attr> attrSet = new List<Attr>(); |
| | 0 | 41 | | int valueLength = -1; |
| | 0 | 42 | | while (!reader.EOF) |
| | | 43 | | { |
| | 0 | 44 | | XmlNodeType type = reader.MoveToContent(); |
| | | 45 | | switch (type) |
| | | 46 | | { |
| | | 47 | | case XmlNodeType.Element: |
| | 0 | 48 | | CompleteValue(builder, valueLength); |
| | 0 | 49 | | valueLength = -1; |
| | | 50 | | |
| | 0 | 51 | | builder.Append("<"); |
| | 0 | 52 | | AppendString(builder, reader.LocalName); |
| | 0 | 53 | | builder.Append(":"); |
| | 0 | 54 | | AppendString(builder, reader.NamespaceURI); |
| | 0 | 55 | | builder.Append(" "); |
| | | 56 | | |
| | | 57 | | // Scan attributes |
| | 0 | 58 | | attrSet.Clear(); |
| | 0 | 59 | | if (reader.MoveToFirstAttribute()) |
| | | 60 | | { |
| | | 61 | | do |
| | | 62 | | { |
| | | 63 | | // Ignore namespaces |
| | 0 | 64 | | if (reader.Prefix == "xmlns" || reader.Name == "xmlns") |
| | | 65 | | { |
| | | 66 | | continue; |
| | | 67 | | } |
| | 0 | 68 | | if (reader.LocalName == AddressingStrings.IsReferenceParameter && reader.NamespaceURI == |
| | | 69 | | { |
| | | 70 | | continue; // ignore IsReferenceParameter |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | string val = reader.Value; |
| | 0 | 74 | | if ((reader.LocalName == TypeLN && reader.NamespaceURI == XsiNs) || |
| | 0 | 75 | | (reader.NamespaceURI == SerNs && (reader.LocalName == ItemTypeLN || reader.LocalName |
| | | 76 | | { |
| | 0 | 77 | | XmlUtil.ParseQName(reader, val, out string local, out string ns); |
| | 0 | 78 | | val = local + "^" + local.Length.ToString(CultureInfo.InvariantCulture) + ":" + ns + |
| | | 79 | | } |
| | 0 | 80 | | else if (reader.LocalName == XD.UtilityDictionary.IdAttribute.Value && reader.NamespaceU |
| | | 81 | | { |
| | | 82 | | // ignore wsu:Id attributes added by security to sign the header |
| | | 83 | | continue; |
| | | 84 | | } |
| | 0 | 85 | | attrSet.Add(new Attr(reader.LocalName, reader.NamespaceURI, val)); |
| | 0 | 86 | | } while (reader.MoveToNextAttribute()); |
| | | 87 | | } |
| | 0 | 88 | | reader.MoveToElement(); |
| | | 89 | | |
| | 0 | 90 | | if (attrSet.Count > 0) |
| | | 91 | | { |
| | 0 | 92 | | attrSet.Sort(); |
| | 0 | 93 | | for (int i = 0; i < attrSet.Count; ++i) |
| | | 94 | | { |
| | 0 | 95 | | Attr a = attrSet[i]; |
| | | 96 | | |
| | 0 | 97 | | AppendString(builder, a._local); |
| | 0 | 98 | | builder.Append(":"); |
| | 0 | 99 | | AppendString(builder, a._ns); |
| | 0 | 100 | | builder.Append("=\""); |
| | 0 | 101 | | AppendString(builder, a._val); |
| | 0 | 102 | | builder.Append("\" "); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | if (reader.IsEmptyElement) |
| | | 107 | | { |
| | 0 | 108 | | builder.Append("></>"); // Should be the same as an empty tag. |
| | | 109 | | } |
| | | 110 | | else |
| | | 111 | | { |
| | 0 | 112 | | builder.Append(">"); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | break; |
| | | 116 | | |
| | | 117 | | case XmlNodeType.EndElement: |
| | 0 | 118 | | CompleteValue(builder, valueLength); |
| | 0 | 119 | | valueLength = -1; |
| | 0 | 120 | | builder.Append("</>"); |
| | 0 | 121 | | break; |
| | | 122 | | |
| | | 123 | | // Need to escape CDATA values |
| | | 124 | | case XmlNodeType.CDATA: |
| | 0 | 125 | | CompleteValue(builder, valueLength); |
| | 0 | 126 | | valueLength = -1; |
| | | 127 | | |
| | 0 | 128 | | builder.Append("<![CDATA["); |
| | 0 | 129 | | AppendString(builder, reader.Value); |
| | 0 | 130 | | builder.Append("]]>"); |
| | 0 | 131 | | break; |
| | | 132 | | |
| | | 133 | | case XmlNodeType.SignificantWhitespace: |
| | | 134 | | case XmlNodeType.Text: |
| | 0 | 135 | | if (valueLength < 0) |
| | | 136 | | { |
| | 0 | 137 | | valueLength = builder.Length; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | builder.Append(reader.Value); |
| | | 141 | | break; |
| | | 142 | | |
| | | 143 | | default: |
| | | 144 | | // Do nothing |
| | | 145 | | break; |
| | | 146 | | } |
| | 0 | 147 | | reader.Read(); |
| | | 148 | | } |
| | 0 | 149 | | return builder.ToString(); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private static void AppendString(StringBuilder builder, string s) |
| | | 153 | | { |
| | 0 | 154 | | builder.Append(s); |
| | 0 | 155 | | builder.Append("^"); |
| | 0 | 156 | | builder.Append(s.Length.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | private static void CompleteValue(StringBuilder builder, int startLength) |
| | | 160 | | { |
| | 0 | 161 | | if (startLength < 0) |
| | | 162 | | { |
| | 0 | 163 | | return; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | int len = builder.Length - startLength; |
| | 0 | 167 | | builder.Append("^"); |
| | 0 | 168 | | builder.Append(len.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | internal void Clear(int length) |
| | | 172 | | { |
| | 0 | 173 | | if (_resultData.Length == length) |
| | | 174 | | { |
| | 0 | 175 | | Array.Clear(_resultData, 0, _resultData.Length); |
| | | 176 | | } |
| | | 177 | | else |
| | | 178 | | { |
| | 0 | 179 | | _resultData = new byte[length]; |
| | | 180 | | } |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | internal void ProcessHeaders(Message msg, Dictionary<QName, int> qnameLookup, Dictionary<string, HeaderBit[]> he |
| | | 184 | | { |
| | | 185 | | string key; |
| | | 186 | | QName qname; |
| | 0 | 187 | | MessageHeaders headers = msg.Headers; |
| | 0 | 188 | | for (int j = 0; j < headers.Count; ++j) |
| | | 189 | | { |
| | 0 | 190 | | qname.name = headers[j].Name; |
| | 0 | 191 | | qname.ns = headers[j].Namespace; |
| | 0 | 192 | | if (headers.MessageVersion.Addressing == AddressingVersion.WSAddressing10 |
| | 0 | 193 | | && !headers[j].IsReferenceParameter) |
| | | 194 | | { |
| | | 195 | | continue; |
| | | 196 | | } |
| | 0 | 197 | | if (qnameLookup.ContainsKey(qname)) |
| | | 198 | | { |
| | 0 | 199 | | _builder.Remove(0, _builder.Length); |
| | 0 | 200 | | XmlReader reader = headers.GetReaderAtHeader(j).ReadSubtree(); |
| | 0 | 201 | | reader.Read(); // Needed after call to ReadSubtree |
| | 0 | 202 | | key = GetComparableForm(_builder, reader); |
| | | 203 | | |
| | 0 | 204 | | if (headerLookup.TryGetValue(key, out HeaderBit[] bits)) |
| | | 205 | | { |
| | 0 | 206 | | SetBit(bits); |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | } |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | internal void SetBit(HeaderBit[] bits) |
| | | 213 | | { |
| | 0 | 214 | | if (bits.Length == 1) |
| | | 215 | | { |
| | 0 | 216 | | _resultData[bits[0]._index] |= bits[0]._mask; |
| | | 217 | | } |
| | | 218 | | else |
| | | 219 | | { |
| | 0 | 220 | | byte[] results = _resultData; |
| | 0 | 221 | | for (int i = 0; i < bits.Length; ++i) |
| | | 222 | | { |
| | 0 | 223 | | if ((results[bits[i]._index] & bits[i]._mask) == 0) |
| | | 224 | | { |
| | 0 | 225 | | results[bits[i]._index] |= bits[i]._mask; |
| | 0 | 226 | | break; |
| | | 227 | | } |
| | | 228 | | } |
| | | 229 | | } |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | internal bool TestExact(byte[] exact) |
| | | 233 | | { |
| | | 234 | | Fx.Assert(_resultData.Length == exact.Length, ""); |
| | | 235 | | |
| | 0 | 236 | | byte[] results = _resultData; |
| | 0 | 237 | | for (int i = 0; i < exact.Length; ++i) |
| | | 238 | | { |
| | 0 | 239 | | if (results[i] != exact[i]) |
| | | 240 | | { |
| | 0 | 241 | | return false; |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return true; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | internal bool TestMask(byte[] mask) |
| | | 249 | | { |
| | 0 | 250 | | if (mask == null) |
| | | 251 | | { |
| | 0 | 252 | | return true; |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | byte[] results = _resultData; |
| | 0 | 256 | | for (int i = 0; i < mask.Length; ++i) |
| | | 257 | | { |
| | 0 | 258 | | if ((results[i] & mask[i]) != mask[i]) |
| | | 259 | | { |
| | 0 | 260 | | return false; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | return true; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | internal struct QName |
| | | 268 | | { |
| | | 269 | | internal string name; |
| | | 270 | | internal string ns; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | internal class QNameKeyComparer : IComparer<QName>, IEqualityComparer<QName> |
| | | 274 | | { |
| | 0 | 275 | | internal QNameKeyComparer() |
| | | 276 | | { |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | public int Compare(QName x, QName y) |
| | | 280 | | { |
| | 0 | 281 | | int i = string.CompareOrdinal(x.name, y.name); |
| | 0 | 282 | | if (i != 0) |
| | | 283 | | { |
| | 0 | 284 | | return i; |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return string.CompareOrdinal(x.ns, y.ns); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | public bool Equals(QName x, QName y) |
| | | 291 | | { |
| | 0 | 292 | | int i = string.CompareOrdinal(x.name, y.name); |
| | 0 | 293 | | if (i != 0) |
| | | 294 | | { |
| | 0 | 295 | | return false; |
| | | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | return string.CompareOrdinal(x.ns, y.ns) == 0; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | public int GetHashCode(QName obj) |
| | | 302 | | { |
| | 0 | 303 | | return obj.name.GetHashCode() ^ obj.ns.GetHashCode(); |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | internal struct HeaderBit |
| | | 308 | | { |
| | | 309 | | internal int _index; |
| | | 310 | | internal byte _mask; |
| | | 311 | | |
| | | 312 | | internal HeaderBit(int bitNum) |
| | | 313 | | { |
| | 0 | 314 | | _index = bitNum / 8; |
| | 0 | 315 | | _mask = (byte)(1 << (bitNum % 8)); |
| | 0 | 316 | | } |
| | | 317 | | |
| | | 318 | | internal void AddToMask(ref byte[] mask) |
| | | 319 | | { |
| | 0 | 320 | | if (mask == null) |
| | | 321 | | { |
| | 0 | 322 | | mask = new byte[_index + 1]; |
| | | 323 | | } |
| | 0 | 324 | | else if (mask.Length <= _index) |
| | | 325 | | { |
| | 0 | 326 | | Array.Resize(ref mask, _index + 1); |
| | | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | mask[_index] |= _mask; |
| | 0 | 330 | | } |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | private class Attr : IComparable<Attr> |
| | | 334 | | { |
| | | 335 | | internal string _local; |
| | | 336 | | internal string _ns; |
| | | 337 | | internal string _val; |
| | | 338 | | private readonly string _key; |
| | | 339 | | |
| | 0 | 340 | | internal Attr(string l, string ns, string v) |
| | | 341 | | { |
| | 0 | 342 | | _local = l; |
| | 0 | 343 | | _ns = ns; |
| | 0 | 344 | | _val = v; |
| | 0 | 345 | | _key = ns + ":" + l; |
| | 0 | 346 | | } |
| | | 347 | | |
| | | 348 | | public int CompareTo(Attr a) |
| | | 349 | | { |
| | 0 | 350 | | return string.Compare(_key, a._key, StringComparison.Ordinal); |
| | | 351 | | } |
| | | 352 | | } |
| | | 353 | | } |
| | | 354 | | } |