| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | public class EndpointAddress |
| | | 14 | | { |
| | | 15 | | private static Uri s_anonymousUri; |
| | | 16 | | private static Uri s_noneUri; |
| | | 17 | | private static EndpointAddress s_anonymousAddress; |
| | | 18 | | |
| | | 19 | | /* |
| | | 20 | | Conceptually, the agnostic EndpointAddress class represents all of UNION(v200408,v10) data thusly: |
| | | 21 | | - Address Uri (both versions - the Address) |
| | | 22 | | - AddressHeaderCollection (both versions - RefProp&RefParam both project into here) |
| | | 23 | | - PSP blob (200408 - this is PortType, ServiceName, Policy, it is not surfaced in OM) |
| | | 24 | | - metadata (both versions, but weird semantics in 200408) |
| | | 25 | | - identity (both versions, this is the one 'extension' that we know about) |
| | | 26 | | - extensions (both versions, the "any*" stuff at the end) |
| | | 27 | | |
| | | 28 | | When reading from 200408: |
| | | 29 | | - Address is projected into Uri |
| | | 30 | | - both RefProps and RefParams are projected into AddressHeaderCollection, |
| | | 31 | | they (internally) remember 'which kind' they are |
| | | 32 | | - PortType, ServiceName, Policy are projected into the (internal) PSP blob |
| | | 33 | | - if we see a wsx:metadata element next, we project that element and that element only into the metadata reader |
| | | 34 | | - we read the rest, recognizing and fishing out identity if there, projecting rest to extensions reader |
| | | 35 | | When reading from 10: |
| | | 36 | | - Address is projected into Uri |
| | | 37 | | - RefParams are projected into AddressHeaderCollection; they (internally) remember 'which kind' they are |
| | | 38 | | - nothing is projected into the (internal) PSP blob (it's empty) |
| | | 39 | | - if there's a wsa10:metadata element, everything inside it projects into metadatareader |
| | | 40 | | - we read the rest, recognizing and fishing out identity if there, projecting rest to extensions reader |
| | | 41 | | |
| | | 42 | | When writing to 200408: |
| | | 43 | | - Uri is written as Address |
| | | 44 | | - AddressHeaderCollection is written as RefProps & RefParams, based on what they internally remember selves to |
| | | 45 | | - PSP blob is written out verbatim (will have: PortType?, ServiceName?, Policy?) |
| | | 46 | | - metadata reader is written out verbatim |
| | | 47 | | - identity is written out as extension |
| | | 48 | | - extension reader is written out verbatim |
| | | 49 | | When writing to 10: |
| | | 50 | | - Uri is written as Address |
| | | 51 | | - AddressHeaderCollection is all written as RefParams, regardless of what they internally remember selves to be |
| | | 52 | | - PSP blob is ignored |
| | | 53 | | - if metadata reader is non-empty, we write its value out verbatim inside a wsa10:metadata element |
| | | 54 | | - identity is written out as extension |
| | | 55 | | - extension reader is written out verbatim |
| | | 56 | | |
| | | 57 | | EndpointAddressBuilder: |
| | | 58 | | - you can set metadata to any value you like; we don't (cannot) validate because 10 allows anything |
| | | 59 | | - you can set any extensions you like |
| | | 60 | | |
| | | 61 | | Known Weirdnesses: |
| | | 62 | | - PSP blob does not surface in OM - it can only roundtrip 200408wire->OM->200408wire |
| | | 63 | | - RefProperty distinction does not surface in OM - it can only roundtrip 200408wire->OM->200408wire |
| | | 64 | | - regardless of what metadata in reader, when you roundtrip OM->200408wire->OM, only wsx:metadata |
| | | 65 | | as first element after PSP will stay in metadata, anything else gets dumped in extensions |
| | | 66 | | - PSP blob is lost when doing OM->10wire->OM |
| | | 67 | | - RefProps turn into RefParams when doing OM->10wire->OM |
| | | 68 | | - Identity is always shuffled to front of extensions when doing anyWire->OM->anyWire |
| | | 69 | | */ |
| | | 70 | | |
| | | 71 | | private AddressingVersion _addressingVersion; |
| | | 72 | | private AddressHeaderCollection _headers; |
| | | 73 | | private int _extensionSection; |
| | | 74 | | private int _metadataSection; |
| | | 75 | | private int _pspSection; |
| | | 76 | | |
| | | 77 | | // these are the element name/namespace for the dummy wrapper element that wraps each buffer section |
| | | 78 | | internal const string DummyName = "Dummy"; |
| | | 79 | | internal const string DummyNamespace = "http://Dummy"; |
| | | 80 | | |
| | 0 | 81 | | private EndpointAddress(AddressingVersion version, Uri uri, EndpointIdentity identity, AddressHeaderCollection h |
| | | 82 | | { |
| | 0 | 83 | | Init(version, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection); |
| | 0 | 84 | | } |
| | | 85 | | |
| | 3 | 86 | | public EndpointAddress(string uri) |
| | | 87 | | { |
| | 3 | 88 | | if (uri == null) |
| | | 89 | | { |
| | 0 | 90 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri)); |
| | | 91 | | } |
| | | 92 | | |
| | 3 | 93 | | Uri u = new Uri(uri); |
| | | 94 | | |
| | 3 | 95 | | Init(u, (EndpointIdentity)null, (AddressHeaderCollection)null, null, -1, -1, -1); |
| | 3 | 96 | | } |
| | | 97 | | |
| | | 98 | | public EndpointAddress(Uri uri, params AddressHeader[] addressHeaders) |
| | 4186 | 99 | | : this(uri, (EndpointIdentity)null, addressHeaders) |
| | | 100 | | { |
| | 4186 | 101 | | } |
| | | 102 | | |
| | 4186 | 103 | | public EndpointAddress(Uri uri, EndpointIdentity identity, params AddressHeader[] addressHeaders) |
| | | 104 | | { |
| | 4186 | 105 | | if (uri == null) |
| | | 106 | | { |
| | 0 | 107 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri)); |
| | | 108 | | } |
| | | 109 | | |
| | 4186 | 110 | | Init(uri, identity, addressHeaders); |
| | 4186 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | public EndpointAddress(Uri newUri, EndpointAddress oldEndpointAddress) |
| | | 114 | | { |
| | 0 | 115 | | Init(oldEndpointAddress._addressingVersion, newUri, oldEndpointAddress.Identity, oldEndpointAddress._headers |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | //public EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers) |
| | | 119 | | //{ |
| | | 120 | | // if (uri == null) |
| | | 121 | | // { |
| | | 122 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri)); |
| | | 123 | | // } |
| | | 124 | | |
| | | 125 | | // Init(uri, identity, headers, null, -1, -1, -1); |
| | | 126 | | //} |
| | | 127 | | |
| | 0 | 128 | | internal EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlDictionaryReade |
| | | 129 | | { |
| | 0 | 130 | | if (uri == null) |
| | | 131 | | { |
| | 0 | 132 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri)); |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | XmlBuffer buffer = null; |
| | 0 | 136 | | PossiblyPopulateBuffer(metadataReader, ref buffer, out _metadataSection); |
| | | 137 | | |
| | 0 | 138 | | buffer = ReadExtensions(extensionReader, null, buffer, out EndpointIdentity ident2, out int extSection); |
| | | 139 | | |
| | 0 | 140 | | if (identity != null && ident2 != null) |
| | | 141 | | { |
| | 0 | 142 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.MultipleIdentities, n |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | PossiblyPopulateBuffer(pspReader, ref buffer, out _pspSection); |
| | | 146 | | |
| | 0 | 147 | | if (buffer != null) |
| | | 148 | | { |
| | 0 | 149 | | buffer.Close(); |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | Init(uri, identity ?? ident2, headers, buffer, _metadataSection, extSection, _pspSection); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | // metadataReader and extensionReader are assumed to not have a starting dummy wrapper element |
| | | 156 | | public EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlDictionaryReader |
| | 0 | 157 | | : this(uri, identity, headers, metadataReader, extensionReader, null) |
| | | 158 | | { |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | private void Init(Uri uri, EndpointIdentity identity, AddressHeader[] headers) |
| | | 162 | | { |
| | 4186 | 163 | | if (headers == null || headers.Length == 0) |
| | | 164 | | { |
| | 4186 | 165 | | Init(uri, identity, (AddressHeaderCollection)null, null, -1, -1, -1); |
| | | 166 | | } |
| | | 167 | | else |
| | | 168 | | { |
| | 0 | 169 | | Init(uri, identity, new AddressHeaderCollection(headers), null, -1, -1, -1); |
| | | 170 | | } |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | private void Init(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int met |
| | | 174 | | { |
| | 4189 | 175 | | Init(null, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection); |
| | 4189 | 176 | | } |
| | | 177 | | |
| | | 178 | | private void Init(AddressingVersion version, Uri uri, EndpointIdentity identity, AddressHeaderCollection headers |
| | | 179 | | { |
| | 4189 | 180 | | if (!uri.IsAbsoluteUri) |
| | | 181 | | { |
| | 0 | 182 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(uri), SR.UriMustBeAbsolute); |
| | | 183 | | } |
| | | 184 | | |
| | 4189 | 185 | | _addressingVersion = version; |
| | 4189 | 186 | | Uri = uri; |
| | 4189 | 187 | | Identity = identity; |
| | 4189 | 188 | | _headers = headers; |
| | 4189 | 189 | | Buffer = buffer; |
| | 4189 | 190 | | _metadataSection = metadataSection; |
| | 4189 | 191 | | _extensionSection = extensionSection; |
| | 4189 | 192 | | _pspSection = pspSection; |
| | | 193 | | |
| | 4189 | 194 | | if (version != null) |
| | | 195 | | { |
| | 0 | 196 | | IsAnonymous = uri == version.AnonymousUri; |
| | 0 | 197 | | IsNone = uri == version.NoneUri; |
| | | 198 | | } |
| | | 199 | | else |
| | | 200 | | { |
| | 4189 | 201 | | IsAnonymous = ReferenceEquals(uri, AnonymousUri) || uri == AnonymousUri; |
| | 4189 | 202 | | IsNone = ReferenceEquals(uri, NoneUri) || uri == NoneUri; |
| | | 203 | | } |
| | 4189 | 204 | | if (IsAnonymous) |
| | | 205 | | { |
| | 7 | 206 | | Uri = AnonymousUri; |
| | | 207 | | } |
| | 4189 | 208 | | if (IsNone) |
| | | 209 | | { |
| | 3 | 210 | | Uri = NoneUri; |
| | | 211 | | } |
| | 4189 | 212 | | } |
| | | 213 | | |
| | | 214 | | // TODO: This was internal, I needed to make it public. Is this a problem? |
| | | 215 | | public static EndpointAddress AnonymousAddress |
| | | 216 | | { |
| | | 217 | | get |
| | | 218 | | { |
| | 678 | 219 | | if (s_anonymousAddress == null) |
| | | 220 | | { |
| | 4 | 221 | | s_anonymousAddress = new EndpointAddress(AnonymousUri); |
| | | 222 | | } |
| | | 223 | | |
| | 678 | 224 | | return s_anonymousAddress; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public static Uri AnonymousUri |
| | | 229 | | { |
| | | 230 | | get |
| | | 231 | | { |
| | 8387 | 232 | | if (s_anonymousUri == null) |
| | | 233 | | { |
| | 11 | 234 | | s_anonymousUri = new Uri(AddressingStrings.AnonymousUri); |
| | | 235 | | } |
| | | 236 | | |
| | 8387 | 237 | | return s_anonymousUri; |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | public static Uri NoneUri |
| | | 242 | | { |
| | | 243 | | get |
| | | 244 | | { |
| | 8383 | 245 | | if (s_noneUri == null) |
| | | 246 | | { |
| | 11 | 247 | | s_noneUri = new Uri(AddressingStrings.NoneUri); |
| | | 248 | | } |
| | | 249 | | |
| | 8383 | 250 | | return s_noneUri; |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | 4189 | 254 | | internal XmlBuffer Buffer { get; private set; } |
| | | 255 | | |
| | | 256 | | public AddressHeaderCollection Headers |
| | | 257 | | { |
| | | 258 | | get |
| | | 259 | | { |
| | 1467 | 260 | | if (_headers == null) |
| | | 261 | | { |
| | 669 | 262 | | _headers = new AddressHeaderCollection(); |
| | | 263 | | } |
| | | 264 | | |
| | 1467 | 265 | | return _headers; |
| | | 266 | | } |
| | | 267 | | } |
| | | 268 | | |
| | 5528 | 269 | | public EndpointIdentity Identity { get; private set; } |
| | | 270 | | |
| | 8579 | 271 | | public bool IsAnonymous { get; private set; } |
| | | 272 | | |
| | 8703 | 273 | | public bool IsNone { get; private set; } |
| | | 274 | | |
| | 13003 | 275 | | public Uri Uri { get; private set; } |
| | | 276 | | |
| | | 277 | | public void ApplyTo(Message message) |
| | | 278 | | { |
| | 25 | 279 | | if (message == null) |
| | | 280 | | { |
| | 0 | 281 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 282 | | } |
| | | 283 | | |
| | 25 | 284 | | Uri uri = Uri; |
| | 25 | 285 | | if (IsAnonymous) |
| | | 286 | | { |
| | 10 | 287 | | if (message.Version.Addressing == AddressingVersion.WSAddressing10) |
| | | 288 | | { |
| | 10 | 289 | | message.Headers.To = null; |
| | | 290 | | } |
| | | 291 | | else |
| | | 292 | | { |
| | 0 | 293 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 294 | | new ProtocolException(SR.Format(SR.AddressingVersionNotSupported, message.Version.Addressing))); |
| | | 295 | | } |
| | | 296 | | } |
| | 15 | 297 | | else if (IsNone) |
| | | 298 | | { |
| | 0 | 299 | | message.Headers.To = message.Version.Addressing.NoneUri; |
| | | 300 | | } |
| | | 301 | | else |
| | | 302 | | { |
| | 15 | 303 | | message.Headers.To = uri; |
| | | 304 | | } |
| | 25 | 305 | | message.Properties.Via = message.Headers.To; |
| | 25 | 306 | | if (_headers != null) |
| | | 307 | | { |
| | 10 | 308 | | _headers.AddHeadersTo(message); |
| | | 309 | | } |
| | 25 | 310 | | } |
| | | 311 | | |
| | | 312 | | // NOTE: UserInfo, Query, and Fragment are ignored when comparing Uris as addresses |
| | | 313 | | // this is the WCF logic for comparing Uris that represent addresses |
| | | 314 | | // this method must be kept in sync with UriGetHashCode |
| | | 315 | | internal static bool UriEquals(Uri u1, Uri u2, bool ignoreCase, bool includeHostInComparison) |
| | | 316 | | { |
| | 5 | 317 | | return UriEquals(u1, u2, ignoreCase, includeHostInComparison, true, true); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | internal static bool UriEquals(Uri u1, Uri u2, bool ignoreCase, bool includeHostInComparison, bool includePortIn |
| | | 321 | | { |
| | | 322 | | // PERF: Equals compares everything but UserInfo and Fragments. It's more strict than |
| | | 323 | | // we are, and faster, so it is done first. |
| | 2479 | 324 | | if (u1.Equals(u2)) |
| | | 325 | | { |
| | 1909 | 326 | | return true; |
| | | 327 | | } |
| | | 328 | | |
| | 570 | 329 | | if (includeSchemeInComparison) |
| | | 330 | | { |
| | 6 | 331 | | if (u1.Scheme != u2.Scheme) // Uri.Scheme is always lowercase |
| | | 332 | | { |
| | 0 | 333 | | return false; |
| | | 334 | | } |
| | | 335 | | } |
| | | 336 | | |
| | 570 | 337 | | if (includePortInComparison) |
| | | 338 | | { |
| | 6 | 339 | | if (u1.Port != u2.Port) |
| | | 340 | | { |
| | 0 | 341 | | return false; |
| | | 342 | | } |
| | | 343 | | } |
| | 570 | 344 | | if (includeHostInComparison) |
| | | 345 | | { |
| | 0 | 346 | | if (string.Compare(u1.Host, u2.Host, StringComparison.OrdinalIgnoreCase) != 0) |
| | | 347 | | { |
| | 0 | 348 | | return false; |
| | | 349 | | } |
| | | 350 | | } |
| | | 351 | | |
| | 570 | 352 | | if (string.Compare(u1.AbsolutePath, u2.AbsolutePath, ignoreCase ? StringComparison.OrdinalIgnoreCase : Strin |
| | | 353 | | { |
| | 570 | 354 | | return true; |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | // Normalize for trailing slashes |
| | 0 | 358 | | string u1Path = u1.GetComponents(UriComponents.Path, UriFormat.Unescaped); |
| | 0 | 359 | | string u2Path = u2.GetComponents(UriComponents.Path, UriFormat.Unescaped); |
| | 0 | 360 | | int u1Len = (u1Path.Length > 0 && u1Path[u1Path.Length - 1] == '/') ? u1Path.Length - 1 : u1Path.Length; |
| | 0 | 361 | | int u2Len = (u2Path.Length > 0 && u2Path[u2Path.Length - 1] == '/') ? u2Path.Length - 1 : u2Path.Length; |
| | 0 | 362 | | if (u2Len != u1Len) |
| | | 363 | | { |
| | 0 | 364 | | return false; |
| | | 365 | | } |
| | 0 | 366 | | return string.Compare(u1Path, 0, u2Path, 0, u1Len, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringC |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | // this method must be kept in sync with UriEquals |
| | | 370 | | internal static int UriGetHashCode(Uri uri, bool includeHostInComparison) |
| | | 371 | | { |
| | 3333 | 372 | | return UriGetHashCode(uri, includeHostInComparison, true); |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | internal static int UriGetHashCode(Uri uri, bool includeHostInComparison, bool includePortInComparison) |
| | | 376 | | { |
| | 3333 | 377 | | UriComponents components = UriComponents.Scheme | UriComponents.Path; |
| | | 378 | | |
| | 3333 | 379 | | if (includePortInComparison) |
| | | 380 | | { |
| | 3333 | 381 | | components = components | UriComponents.Port; |
| | | 382 | | } |
| | 3333 | 383 | | if (includeHostInComparison) |
| | | 384 | | { |
| | 3333 | 385 | | components = components | UriComponents.Host; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | // Normalize for trailing slashes |
| | 3333 | 389 | | string uriString = uri.GetComponents(components, UriFormat.Unescaped); |
| | 3333 | 390 | | if (uriString.Length > 0 && uriString[uriString.Length - 1] != '/') |
| | | 391 | | { |
| | 3283 | 392 | | uriString = string.Concat(uriString, "/"); |
| | | 393 | | } |
| | | 394 | | |
| | 3333 | 395 | | return StringComparer.OrdinalIgnoreCase.GetHashCode(uriString); |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | internal bool EndpointEquals(EndpointAddress endpointAddress) |
| | | 399 | | { |
| | 3 | 400 | | if (endpointAddress == null) |
| | | 401 | | { |
| | 0 | 402 | | return false; |
| | | 403 | | } |
| | | 404 | | |
| | 3 | 405 | | if (ReferenceEquals(this, endpointAddress)) |
| | | 406 | | { |
| | 0 | 407 | | return true; |
| | | 408 | | } |
| | | 409 | | |
| | 3 | 410 | | Uri thisTo = Uri; |
| | 3 | 411 | | Uri otherTo = endpointAddress.Uri; |
| | | 412 | | |
| | 3 | 413 | | if (!UriEquals(thisTo, otherTo, false /* ignoreCase */, true /* includeHostInComparison */)) |
| | | 414 | | { |
| | 0 | 415 | | return false; |
| | | 416 | | } |
| | | 417 | | |
| | 3 | 418 | | if (Identity == null) |
| | | 419 | | { |
| | 3 | 420 | | if (endpointAddress.Identity != null) |
| | | 421 | | { |
| | 0 | 422 | | return false; |
| | | 423 | | } |
| | | 424 | | } |
| | 0 | 425 | | else if (!Identity.Equals(endpointAddress.Identity)) |
| | | 426 | | { |
| | 0 | 427 | | return false; |
| | | 428 | | } |
| | | 429 | | |
| | 3 | 430 | | if (!Headers.IsEquivalent(endpointAddress.Headers)) |
| | | 431 | | { |
| | 0 | 432 | | return false; |
| | | 433 | | } |
| | | 434 | | |
| | 3 | 435 | | return true; |
| | | 436 | | } |
| | | 437 | | |
| | | 438 | | public override bool Equals(object obj) |
| | | 439 | | { |
| | 1474 | 440 | | if (ReferenceEquals(obj, this)) |
| | | 441 | | { |
| | 1471 | 442 | | return true; |
| | | 443 | | } |
| | | 444 | | |
| | 3 | 445 | | if (obj == null) |
| | | 446 | | { |
| | 0 | 447 | | return false; |
| | | 448 | | } |
| | | 449 | | |
| | 3 | 450 | | EndpointAddress address = obj as EndpointAddress; |
| | 3 | 451 | | if (address == null) |
| | | 452 | | { |
| | 0 | 453 | | return false; |
| | | 454 | | } |
| | | 455 | | |
| | 3 | 456 | | return EndpointEquals(address); |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | public override int GetHashCode() |
| | | 460 | | { |
| | 1987 | 461 | | return UriGetHashCode(Uri, true /* includeHostInComparison */); |
| | | 462 | | } |
| | | 463 | | |
| | | 464 | | // returns reader without starting dummy wrapper element |
| | | 465 | | internal XmlDictionaryReader GetReaderAtPsp() |
| | | 466 | | { |
| | 0 | 467 | | return GetReaderAtSection(Buffer, _pspSection); |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | //// returns reader without starting dummy wrapper element |
| | | 471 | | internal XmlDictionaryReader GetReaderAtMetadata() |
| | | 472 | | { |
| | 0 | 473 | | return GetReaderAtSection(Buffer, _metadataSection); |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | //// returns reader without starting dummy wrapper element |
| | | 477 | | internal XmlDictionaryReader GetReaderAtExtensions() |
| | | 478 | | { |
| | 0 | 479 | | return GetReaderAtSection(Buffer, _extensionSection); |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | private static XmlDictionaryReader GetReaderAtSection(XmlBuffer buffer, int section) |
| | | 483 | | { |
| | 0 | 484 | | if (buffer == null || section < 0) |
| | | 485 | | { |
| | 0 | 486 | | return null; |
| | | 487 | | } |
| | | 488 | | |
| | 0 | 489 | | XmlDictionaryReader reader = buffer.GetReader(section); |
| | 0 | 490 | | reader.MoveToContent(); |
| | | 491 | | Fx.Assert(reader.Name == DummyName, "EndpointAddress: Expected dummy element not found"); |
| | 0 | 492 | | reader.Read(); // consume the dummy wrapper element |
| | 0 | 493 | | return reader; |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | private void PossiblyPopulateBuffer(XmlDictionaryReader reader, ref XmlBuffer buffer, out int section) |
| | | 497 | | { |
| | 0 | 498 | | if (reader == null) |
| | | 499 | | { |
| | 0 | 500 | | section = -1; |
| | | 501 | | } |
| | | 502 | | else |
| | | 503 | | { |
| | 0 | 504 | | if (buffer == null) |
| | | 505 | | { |
| | 0 | 506 | | buffer = new XmlBuffer(short.MaxValue); |
| | | 507 | | } |
| | 0 | 508 | | section = buffer.SectionCount; |
| | 0 | 509 | | XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas); |
| | 0 | 510 | | writer.WriteStartElement(DummyName, DummyNamespace); |
| | 0 | 511 | | Copy(writer, reader); |
| | 0 | 512 | | buffer.CloseSection(); |
| | | 513 | | } |
| | 0 | 514 | | } |
| | | 515 | | |
| | | 516 | | public static EndpointAddress ReadFrom(XmlDictionaryReader reader) |
| | | 517 | | { |
| | | 518 | | AddressingVersion dummyVersion; |
| | 0 | 519 | | return ReadFrom(reader, out dummyVersion); |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | internal static EndpointAddress ReadFrom(XmlDictionaryReader reader, out AddressingVersion version) |
| | | 523 | | { |
| | 0 | 524 | | if (reader == null) |
| | | 525 | | { |
| | 0 | 526 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 527 | | } |
| | | 528 | | |
| | 0 | 529 | | reader.ReadFullStartElement(); |
| | 0 | 530 | | reader.MoveToContent(); |
| | | 531 | | |
| | 0 | 532 | | if (reader.IsNamespaceUri(AddressingVersion.WSAddressing10.DictionaryNamespace)) |
| | | 533 | | { |
| | 0 | 534 | | version = AddressingVersion.WSAddressing10; |
| | | 535 | | } |
| | 0 | 536 | | else if (reader.IsNamespaceUri(AddressingVersion.WSAddressingAugust2004.DictionaryNamespace)) |
| | | 537 | | { |
| | 0 | 538 | | version = AddressingVersion.WSAddressingAugust2004; |
| | | 539 | | } |
| | 0 | 540 | | else if (reader.NodeType != XmlNodeType.Element) |
| | | 541 | | { |
| | 0 | 542 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( |
| | 0 | 543 | | nameof(reader), SR.CannotDetectAddressingVersion); |
| | | 544 | | } |
| | | 545 | | else |
| | | 546 | | { |
| | 0 | 547 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( |
| | 0 | 548 | | nameof(reader), SR.Format(SR.AddressingVersionNotSupported, reader.NamespaceURI)); |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | EndpointAddress ea = ReadFromDriver(version, reader); |
| | 0 | 552 | | reader.ReadEndElement(); |
| | 0 | 553 | | return ea; |
| | | 554 | | } |
| | | 555 | | |
| | | 556 | | //public static EndpointAddress ReadFrom(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionar |
| | | 557 | | //{ |
| | | 558 | | // AddressingVersion version; |
| | | 559 | | // return ReadFrom(reader, localName, ns, out version); |
| | | 560 | | //} |
| | | 561 | | |
| | | 562 | | internal static EndpointAddress ReadFrom(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionar |
| | | 563 | | { |
| | 0 | 564 | | if (reader == null) |
| | | 565 | | { |
| | 0 | 566 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 567 | | } |
| | | 568 | | |
| | 0 | 569 | | reader.ReadFullStartElement(localName, ns); |
| | 0 | 570 | | reader.MoveToContent(); |
| | | 571 | | |
| | 0 | 572 | | if (reader.IsNamespaceUri(AddressingVersion.WSAddressing10.DictionaryNamespace)) |
| | | 573 | | { |
| | 0 | 574 | | version = AddressingVersion.WSAddressing10; |
| | | 575 | | } |
| | 0 | 576 | | else if (reader.IsNamespaceUri(AddressingVersion.WSAddressingAugust2004.DictionaryNamespace)) |
| | | 577 | | { |
| | 0 | 578 | | version = AddressingVersion.WSAddressingAugust2004; |
| | | 579 | | } |
| | 0 | 580 | | else if (reader.NodeType != XmlNodeType.Element) |
| | | 581 | | { |
| | 0 | 582 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( |
| | 0 | 583 | | nameof(reader), SR.CannotDetectAddressingVersion); |
| | | 584 | | } |
| | | 585 | | else |
| | | 586 | | { |
| | 0 | 587 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( |
| | 0 | 588 | | nameof(reader), SR.Format(SR.AddressingVersionNotSupported, reader.NamespaceURI)); |
| | | 589 | | } |
| | | 590 | | |
| | 0 | 591 | | EndpointAddress ea = ReadFromDriver(version, reader); |
| | 0 | 592 | | reader.ReadEndElement(); |
| | 0 | 593 | | return ea; |
| | | 594 | | } |
| | | 595 | | |
| | | 596 | | //public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlReader reader) |
| | | 597 | | //{ |
| | | 598 | | // return ReadFrom(addressingVersion, XmlDictionaryReader.CreateDictionaryReader(reader)); |
| | | 599 | | //} |
| | | 600 | | |
| | | 601 | | //public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlReader reader, string localName |
| | | 602 | | //{ |
| | | 603 | | // if (reader == null) |
| | | 604 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 605 | | // if (addressingVersion == null) |
| | | 606 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion)); |
| | | 607 | | |
| | | 608 | | // XmlDictionaryReader dictReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | | 609 | | // dictReader.ReadFullStartElement(localName, ns); |
| | | 610 | | // EndpointAddress ea = ReadFromDriver(addressingVersion, dictReader); |
| | | 611 | | // reader.ReadEndElement(); |
| | | 612 | | // return ea; |
| | | 613 | | //} |
| | | 614 | | |
| | | 615 | | public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlDictionaryReader reader) |
| | | 616 | | { |
| | 278 | 617 | | if (reader == null) |
| | | 618 | | { |
| | 0 | 619 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 620 | | } |
| | | 621 | | |
| | 278 | 622 | | if (addressingVersion == null) |
| | | 623 | | { |
| | 0 | 624 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion)); |
| | | 625 | | } |
| | | 626 | | |
| | 278 | 627 | | reader.ReadFullStartElement(); |
| | 278 | 628 | | EndpointAddress ea = ReadFromDriver(addressingVersion, reader); |
| | 278 | 629 | | reader.ReadEndElement(); |
| | 278 | 630 | | return ea; |
| | | 631 | | } |
| | | 632 | | |
| | | 633 | | //public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlDictionaryReader reader, XmlDic |
| | | 634 | | //{ |
| | | 635 | | // if (reader == null) |
| | | 636 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 637 | | // if (addressingVersion == null) |
| | | 638 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion)); |
| | | 639 | | |
| | | 640 | | // reader.ReadFullStartElement(localName, ns); |
| | | 641 | | // EndpointAddress ea = ReadFromDriver(addressingVersion, reader); |
| | | 642 | | // reader.ReadEndElement(); |
| | | 643 | | // return ea; |
| | | 644 | | //} |
| | | 645 | | |
| | | 646 | | private static EndpointAddress ReadFromDriver(AddressingVersion addressingVersion, XmlDictionaryReader reader) |
| | | 647 | | { |
| | | 648 | | AddressHeaderCollection headers; |
| | | 649 | | EndpointIdentity identity; |
| | | 650 | | Uri uri; |
| | | 651 | | XmlBuffer buffer; |
| | | 652 | | bool isAnonymous; |
| | | 653 | | int extensionSection; |
| | | 654 | | int metadataSection; |
| | 278 | 655 | | int pspSection = -1; |
| | | 656 | | |
| | 278 | 657 | | if (addressingVersion == AddressingVersion.WSAddressing10) |
| | | 658 | | { |
| | 263 | 659 | | isAnonymous = ReadContentsFrom10(reader, out uri, out headers, out identity, out buffer, out metadataSec |
| | | 660 | | } |
| | 15 | 661 | | else if (addressingVersion == AddressingVersion.WSAddressingAugust2004) |
| | | 662 | | { |
| | 15 | 663 | | isAnonymous = ReadContentsFrom200408(reader, out uri, out headers, out identity, out buffer, out metadat |
| | | 664 | | } |
| | | 665 | | else |
| | | 666 | | { |
| | 0 | 667 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(addressingVersion), |
| | 0 | 668 | | SR.Format(SR.AddressingVersionNotSupported, addressingVersion)); |
| | | 669 | | } |
| | | 670 | | |
| | 278 | 671 | | if (isAnonymous && headers == null && identity == null && buffer == null) |
| | | 672 | | { |
| | 278 | 673 | | return AnonymousAddress; |
| | | 674 | | } |
| | | 675 | | else |
| | | 676 | | { |
| | 0 | 677 | | return new EndpointAddress(addressingVersion, uri, identity, headers, buffer, metadataSection, extension |
| | | 678 | | } |
| | | 679 | | } |
| | | 680 | | |
| | | 681 | | internal static XmlBuffer ReadExtensions(XmlDictionaryReader reader, AddressingVersion version, XmlBuffer buffer |
| | | 682 | | { |
| | 278 | 683 | | if (reader == null) |
| | | 684 | | { |
| | 0 | 685 | | identity = null; |
| | 0 | 686 | | section = -1; |
| | 0 | 687 | | return buffer; |
| | | 688 | | } |
| | | 689 | | |
| | | 690 | | // EndpointIdentity and extensions |
| | 278 | 691 | | identity = null; |
| | 278 | 692 | | XmlDictionaryWriter bufferWriter = null; |
| | 278 | 693 | | reader.MoveToContent(); |
| | 278 | 694 | | while (reader.IsStartElement()) |
| | | 695 | | { |
| | 0 | 696 | | if (reader.IsStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNam |
| | | 697 | | { |
| | 0 | 698 | | if (identity != null) |
| | | 699 | | { |
| | 0 | 700 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.Format(S |
| | | 701 | | } |
| | | 702 | | |
| | 0 | 703 | | identity = EndpointIdentity.ReadIdentity(reader); |
| | | 704 | | } |
| | 0 | 705 | | else if (version != null && reader.NamespaceURI == version.Namespace) |
| | | 706 | | { |
| | 0 | 707 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.Format(SR.Ad |
| | | 708 | | } |
| | | 709 | | else |
| | | 710 | | { |
| | 0 | 711 | | if (bufferWriter == null) |
| | | 712 | | { |
| | 0 | 713 | | if (buffer == null) |
| | | 714 | | { |
| | 0 | 715 | | buffer = new XmlBuffer(short.MaxValue); |
| | | 716 | | } |
| | | 717 | | |
| | 0 | 718 | | bufferWriter = buffer.OpenSection(reader.Quotas); |
| | 0 | 719 | | bufferWriter.WriteStartElement(DummyName, DummyNamespace); |
| | | 720 | | } |
| | | 721 | | |
| | 0 | 722 | | bufferWriter.WriteNode(reader, true); |
| | | 723 | | } |
| | 0 | 724 | | reader.MoveToContent(); |
| | | 725 | | } |
| | | 726 | | |
| | 278 | 727 | | if (bufferWriter != null) |
| | | 728 | | { |
| | 0 | 729 | | bufferWriter.WriteEndElement(); |
| | 0 | 730 | | buffer.CloseSection(); |
| | 0 | 731 | | section = buffer.SectionCount - 1; |
| | | 732 | | } |
| | | 733 | | else |
| | | 734 | | { |
| | 278 | 735 | | section = -1; |
| | | 736 | | } |
| | | 737 | | |
| | 278 | 738 | | return buffer; |
| | | 739 | | } |
| | | 740 | | |
| | | 741 | | private static bool ReadContentsFrom200408(XmlDictionaryReader reader, out Uri uri, out AddressHeaderCollection |
| | | 742 | | { |
| | 15 | 743 | | buffer = null; |
| | 15 | 744 | | headers = null; |
| | 15 | 745 | | extensionSection = -1; |
| | 15 | 746 | | metadataSection = -1; |
| | 15 | 747 | | pspSection = -1; |
| | | 748 | | |
| | | 749 | | // Cache address string |
| | 15 | 750 | | reader.MoveToContent(); |
| | 15 | 751 | | if (!reader.IsStartElement(XD.AddressingDictionary.Address, AddressingVersion.WSAddressingAugust2004.Diction |
| | | 752 | | { |
| | 0 | 753 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.Format(SR.Unexpe |
| | | 754 | | } |
| | 15 | 755 | | string address = reader.ReadElementContentAsString(); |
| | | 756 | | |
| | | 757 | | // ReferenceProperites |
| | 15 | 758 | | reader.MoveToContent(); |
| | 15 | 759 | | if (reader.IsStartElement(XD.AddressingDictionary.ReferenceProperties, AddressingVersion.WSAddressingAugust2 |
| | | 760 | | { |
| | 0 | 761 | | headers = AddressHeaderCollection.ReadServiceParameters(reader, true); |
| | | 762 | | } |
| | | 763 | | |
| | | 764 | | // ReferenceParameters |
| | 15 | 765 | | reader.MoveToContent(); |
| | 15 | 766 | | if (reader.IsStartElement(XD.AddressingDictionary.ReferenceParameters, AddressingVersion.WSAddressingAugust2 |
| | | 767 | | { |
| | 0 | 768 | | if (headers != null) |
| | | 769 | | { |
| | 0 | 770 | | List<AddressHeader> headerList = new List<AddressHeader>(); |
| | 0 | 771 | | foreach (AddressHeader ah in headers) |
| | | 772 | | { |
| | 0 | 773 | | headerList.Add(ah); |
| | | 774 | | } |
| | 0 | 775 | | AddressHeaderCollection tmp = AddressHeaderCollection.ReadServiceParameters(reader); |
| | 0 | 776 | | foreach (AddressHeader ah in tmp) |
| | | 777 | | { |
| | 0 | 778 | | headerList.Add(ah); |
| | | 779 | | } |
| | 0 | 780 | | headers = new AddressHeaderCollection(headerList); |
| | | 781 | | } |
| | | 782 | | else |
| | | 783 | | { |
| | 0 | 784 | | headers = AddressHeaderCollection.ReadServiceParameters(reader); |
| | | 785 | | } |
| | | 786 | | } |
| | | 787 | | |
| | 15 | 788 | | XmlDictionaryWriter bufferWriter = null; |
| | | 789 | | |
| | | 790 | | // PortType |
| | 15 | 791 | | reader.MoveToContent(); |
| | 15 | 792 | | if (reader.IsStartElement(XD.AddressingDictionary.PortType, AddressingVersion.WSAddressingAugust2004.Diction |
| | | 793 | | { |
| | 0 | 794 | | if (bufferWriter == null) |
| | | 795 | | { |
| | 0 | 796 | | if (buffer == null) |
| | 0 | 797 | | buffer = new XmlBuffer(short.MaxValue); |
| | 0 | 798 | | bufferWriter = buffer.OpenSection(reader.Quotas); |
| | 0 | 799 | | bufferWriter.WriteStartElement(DummyName, DummyNamespace); |
| | | 800 | | } |
| | 0 | 801 | | bufferWriter.WriteNode(reader, true); |
| | | 802 | | } |
| | | 803 | | |
| | | 804 | | // ServiceName |
| | 15 | 805 | | reader.MoveToContent(); |
| | 15 | 806 | | if (reader.IsStartElement(XD.AddressingDictionary.ServiceName, AddressingVersion.WSAddressingAugust2004.Dict |
| | | 807 | | { |
| | 0 | 808 | | if (bufferWriter == null) |
| | | 809 | | { |
| | 0 | 810 | | if (buffer == null) |
| | 0 | 811 | | buffer = new XmlBuffer(short.MaxValue); |
| | 0 | 812 | | bufferWriter = buffer.OpenSection(reader.Quotas); |
| | 0 | 813 | | bufferWriter.WriteStartElement(DummyName, DummyNamespace); |
| | | 814 | | } |
| | 0 | 815 | | bufferWriter.WriteNode(reader, true); |
| | | 816 | | } |
| | | 817 | | |
| | | 818 | | // Policy |
| | 15 | 819 | | reader.MoveToContent(); |
| | 15 | 820 | | while (reader.IsNamespaceUri(XD.PolicyDictionary.Namespace)) |
| | | 821 | | { |
| | 0 | 822 | | if (bufferWriter == null) |
| | | 823 | | { |
| | 0 | 824 | | if (buffer == null) |
| | 0 | 825 | | buffer = new XmlBuffer(short.MaxValue); |
| | 0 | 826 | | bufferWriter = buffer.OpenSection(reader.Quotas); |
| | 0 | 827 | | bufferWriter.WriteStartElement(DummyName, DummyNamespace); |
| | | 828 | | } |
| | 0 | 829 | | bufferWriter.WriteNode(reader, true); |
| | 0 | 830 | | reader.MoveToContent(); |
| | | 831 | | } |
| | | 832 | | |
| | | 833 | | // Finish PSP |
| | 15 | 834 | | if (bufferWriter != null) |
| | | 835 | | { |
| | 0 | 836 | | bufferWriter.WriteEndElement(); |
| | 0 | 837 | | buffer.CloseSection(); |
| | 0 | 838 | | pspSection = buffer.SectionCount - 1; |
| | 0 | 839 | | bufferWriter = null; |
| | | 840 | | } |
| | | 841 | | else |
| | | 842 | | { |
| | 15 | 843 | | pspSection = -1; |
| | | 844 | | } |
| | | 845 | | |
| | | 846 | | |
| | | 847 | | // Metadata |
| | 15 | 848 | | if (reader.IsStartElement(Description.MetadataStrings.MetadataExchangeStrings.Metadata, |
| | 15 | 849 | | Description.MetadataStrings.MetadataExchangeStrings.Namespace)) |
| | | 850 | | { |
| | 0 | 851 | | if (bufferWriter == null) |
| | | 852 | | { |
| | 0 | 853 | | if (buffer == null) |
| | 0 | 854 | | buffer = new XmlBuffer(short.MaxValue); |
| | 0 | 855 | | bufferWriter = buffer.OpenSection(reader.Quotas); |
| | 0 | 856 | | bufferWriter.WriteStartElement(DummyName, DummyNamespace); |
| | | 857 | | } |
| | 0 | 858 | | bufferWriter.WriteNode(reader, true); |
| | | 859 | | } |
| | | 860 | | |
| | | 861 | | // Finish metadata |
| | 15 | 862 | | if (bufferWriter != null) |
| | | 863 | | { |
| | 0 | 864 | | bufferWriter.WriteEndElement(); |
| | 0 | 865 | | buffer.CloseSection(); |
| | 0 | 866 | | metadataSection = buffer.SectionCount - 1; |
| | | 867 | | } |
| | | 868 | | else |
| | | 869 | | { |
| | 15 | 870 | | metadataSection = -1; |
| | | 871 | | } |
| | | 872 | | |
| | | 873 | | // Extensions |
| | 15 | 874 | | reader.MoveToContent(); |
| | 15 | 875 | | buffer = ReadExtensions(reader, AddressingVersion.WSAddressingAugust2004, buffer, out identity, out extensio |
| | | 876 | | |
| | | 877 | | // Finished reading |
| | 15 | 878 | | if (buffer != null) |
| | 0 | 879 | | buffer.Close(); |
| | | 880 | | |
| | | 881 | | // Process Address |
| | 15 | 882 | | if (address == Addressing200408Strings.Anonymous) |
| | | 883 | | { |
| | 15 | 884 | | uri = AddressingVersion.WSAddressingAugust2004.AnonymousUri; |
| | 15 | 885 | | if (headers == null && identity == null) |
| | 15 | 886 | | return true; |
| | | 887 | | } |
| | | 888 | | else |
| | | 889 | | { |
| | 0 | 890 | | if (!Uri.TryCreate(address, UriKind.Absolute, out uri)) |
| | 0 | 891 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.InvalidUriVa |
| | | 892 | | } |
| | | 893 | | |
| | 0 | 894 | | return false; |
| | | 895 | | } |
| | | 896 | | |
| | | 897 | | private static bool ReadContentsFrom10(XmlDictionaryReader reader, out Uri uri, out AddressHeaderCollection head |
| | | 898 | | { |
| | 263 | 899 | | buffer = null; |
| | 263 | 900 | | extensionSection = -1; |
| | 263 | 901 | | metadataSection = -1; |
| | | 902 | | |
| | | 903 | | // Cache address string |
| | 263 | 904 | | if (!reader.IsStartElement(XD.AddressingDictionary.Address, XD.Addressing10Dictionary.Namespace)) |
| | | 905 | | { |
| | 0 | 906 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.Format(SR.Unexpe |
| | | 907 | | } |
| | | 908 | | |
| | 263 | 909 | | string address = reader.ReadElementContentAsString(); |
| | | 910 | | |
| | | 911 | | // Headers |
| | 263 | 912 | | if (reader.IsStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing10Dictionary.Namespace)) |
| | | 913 | | { |
| | 0 | 914 | | headers = AddressHeaderCollection.ReadServiceParameters(reader); |
| | | 915 | | } |
| | | 916 | | else |
| | | 917 | | { |
| | 263 | 918 | | headers = null; |
| | | 919 | | } |
| | | 920 | | |
| | | 921 | | // Metadata |
| | 263 | 922 | | if (reader.IsStartElement(XD.Addressing10Dictionary.Metadata, XD.Addressing10Dictionary.Namespace)) |
| | | 923 | | { |
| | 0 | 924 | | reader.ReadFullStartElement(); // the wsa10:Metadata element is never stored in the buffer |
| | 0 | 925 | | buffer = new XmlBuffer(short.MaxValue); |
| | 0 | 926 | | metadataSection = 0; |
| | 0 | 927 | | XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas); |
| | 0 | 928 | | writer.WriteStartElement(DummyName, DummyNamespace); |
| | 0 | 929 | | while (reader.NodeType != XmlNodeType.EndElement && !reader.EOF) |
| | | 930 | | { |
| | 0 | 931 | | writer.WriteNode(reader, true); |
| | | 932 | | } |
| | 0 | 933 | | writer.Flush(); |
| | 0 | 934 | | buffer.CloseSection(); |
| | 0 | 935 | | reader.ReadEndElement(); |
| | | 936 | | } |
| | | 937 | | |
| | | 938 | | // Extensions |
| | 263 | 939 | | buffer = ReadExtensions(reader, AddressingVersion.WSAddressing10, buffer, out identity, out extensionSection |
| | 263 | 940 | | if (buffer != null) |
| | | 941 | | { |
| | 0 | 942 | | buffer.Close(); |
| | | 943 | | } |
| | | 944 | | |
| | | 945 | | // Process Address |
| | 263 | 946 | | if (address == Addressing10Strings.Anonymous) |
| | | 947 | | { |
| | 263 | 948 | | uri = AddressingVersion.WSAddressing10.AnonymousUri; |
| | 263 | 949 | | if (headers == null && identity == null) |
| | | 950 | | { |
| | 263 | 951 | | return true; |
| | | 952 | | } |
| | | 953 | | } |
| | 0 | 954 | | else if (address == Addressing10Strings.NoneAddress) |
| | | 955 | | { |
| | 0 | 956 | | uri = AddressingVersion.WSAddressing10.NoneUri; |
| | 0 | 957 | | return false; |
| | | 958 | | } |
| | | 959 | | else |
| | | 960 | | { |
| | 0 | 961 | | if (!Uri.TryCreate(address, UriKind.Absolute, out uri)) |
| | | 962 | | { |
| | 0 | 963 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.InvalidUriVa |
| | | 964 | | } |
| | | 965 | | } |
| | 0 | 966 | | return false; |
| | | 967 | | } |
| | | 968 | | |
| | | 969 | | private static XmlException CreateXmlException(XmlDictionaryReader reader, string message) |
| | | 970 | | { |
| | 0 | 971 | | if (reader is IXmlLineInfo lineInfo) |
| | | 972 | | { |
| | 0 | 973 | | return new XmlException(message, null, lineInfo.LineNumber, lineInfo.LinePosition); |
| | | 974 | | } |
| | | 975 | | |
| | 0 | 976 | | return new XmlException(message); |
| | | 977 | | } |
| | | 978 | | |
| | | 979 | | // this function has a side-effect on the reader (MoveToContent) |
| | | 980 | | private static bool Done(XmlDictionaryReader reader) |
| | | 981 | | { |
| | 0 | 982 | | reader.MoveToContent(); |
| | 0 | 983 | | return (reader.NodeType == XmlNodeType.EndElement || reader.EOF); |
| | | 984 | | } |
| | | 985 | | |
| | | 986 | | // copy all of reader to writer |
| | | 987 | | internal static void Copy(XmlDictionaryWriter writer, XmlDictionaryReader reader) |
| | | 988 | | { |
| | 0 | 989 | | while (!Done(reader)) |
| | | 990 | | { |
| | 0 | 991 | | writer.WriteNode(reader, true); |
| | | 992 | | } |
| | 0 | 993 | | } |
| | | 994 | | |
| | | 995 | | public override string ToString() |
| | | 996 | | { |
| | 0 | 997 | | return Uri.ToString(); |
| | | 998 | | } |
| | | 999 | | |
| | | 1000 | | public void WriteContentsTo(AddressingVersion addressingVersion, XmlDictionaryWriter writer) |
| | | 1001 | | { |
| | 21 | 1002 | | if (writer == null) |
| | | 1003 | | { |
| | 0 | 1004 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 1005 | | } |
| | | 1006 | | |
| | 21 | 1007 | | if (addressingVersion == null) |
| | | 1008 | | { |
| | 0 | 1009 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion)); |
| | | 1010 | | } |
| | | 1011 | | |
| | 21 | 1012 | | if (addressingVersion == AddressingVersion.WSAddressing10) |
| | | 1013 | | { |
| | 15 | 1014 | | WriteContentsTo10(writer); |
| | | 1015 | | } |
| | 6 | 1016 | | else if (addressingVersion == AddressingVersion.WSAddressingAugust2004) |
| | | 1017 | | { |
| | 3 | 1018 | | WriteContentsTo200408(writer); |
| | | 1019 | | } |
| | 3 | 1020 | | else if (addressingVersion == AddressingVersion.None) |
| | | 1021 | | { |
| | 3 | 1022 | | WriteContentsToNone(writer); |
| | | 1023 | | } |
| | | 1024 | | else |
| | | 1025 | | { |
| | 0 | 1026 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(addressingVersion), |
| | 0 | 1027 | | SR.Format(SR.AddressingVersionNotSupported, addressingVersion)); |
| | | 1028 | | } |
| | | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | private void WriteContentsToNone(XmlDictionaryWriter writer) |
| | | 1032 | | { |
| | 3 | 1033 | | writer.WriteString(Uri.AbsoluteUri); |
| | 3 | 1034 | | } |
| | | 1035 | | |
| | | 1036 | | private void WriteContentsTo10(XmlDictionaryWriter writer) |
| | | 1037 | | { |
| | | 1038 | | // Address |
| | 15 | 1039 | | writer.WriteStartElement(XD.AddressingDictionary.Address, XD.Addressing10Dictionary.Namespace); |
| | 15 | 1040 | | if (IsAnonymous) |
| | | 1041 | | { |
| | 1 | 1042 | | writer.WriteString(XD.Addressing10Dictionary.Anonymous); |
| | | 1043 | | } |
| | 14 | 1044 | | else if (IsNone) |
| | | 1045 | | { |
| | 1 | 1046 | | writer.WriteString(XD.Addressing10Dictionary.NoneAddress); |
| | | 1047 | | } |
| | | 1048 | | else |
| | | 1049 | | { |
| | 13 | 1050 | | writer.WriteString(Uri.AbsoluteUri); |
| | | 1051 | | } |
| | 15 | 1052 | | writer.WriteEndElement(); |
| | | 1053 | | |
| | | 1054 | | // Headers |
| | 15 | 1055 | | if (_headers != null && _headers.Count > 0) |
| | | 1056 | | { |
| | 0 | 1057 | | writer.WriteStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing10Dictionary.Namespac |
| | 0 | 1058 | | _headers.WriteContentsTo(writer); |
| | 0 | 1059 | | writer.WriteEndElement(); |
| | | 1060 | | } |
| | | 1061 | | |
| | | 1062 | | // Metadata |
| | 15 | 1063 | | if (_metadataSection >= 0) |
| | | 1064 | | { |
| | 0 | 1065 | | XmlDictionaryReader reader = GetReaderAtSection(Buffer, _metadataSection); |
| | 0 | 1066 | | writer.WriteStartElement(XD.Addressing10Dictionary.Metadata, XD.Addressing10Dictionary.Namespace); |
| | 0 | 1067 | | Copy(writer, reader); |
| | 0 | 1068 | | writer.WriteEndElement(); |
| | | 1069 | | } |
| | | 1070 | | |
| | | 1071 | | // EndpointIdentity |
| | 15 | 1072 | | if (Identity != null) |
| | | 1073 | | { |
| | 0 | 1074 | | Identity.WriteTo(writer); |
| | | 1075 | | } |
| | | 1076 | | |
| | | 1077 | | // Extensions |
| | 15 | 1078 | | if (_extensionSection >= 0) |
| | | 1079 | | { |
| | 0 | 1080 | | XmlDictionaryReader reader = GetReaderAtSection(Buffer, _extensionSection); |
| | 0 | 1081 | | while (reader.IsStartElement()) |
| | | 1082 | | { |
| | 0 | 1083 | | if (reader.NamespaceURI == AddressingVersion.WSAddressing10.Namespace) |
| | | 1084 | | { |
| | 0 | 1085 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.Format(S |
| | | 1086 | | } |
| | | 1087 | | |
| | 0 | 1088 | | writer.WriteNode(reader, true); |
| | | 1089 | | } |
| | | 1090 | | } |
| | 15 | 1091 | | } |
| | | 1092 | | |
| | | 1093 | | private void WriteContentsTo200408(XmlDictionaryWriter writer) |
| | | 1094 | | { |
| | | 1095 | | // Address |
| | 3 | 1096 | | writer.WriteStartElement(XD.AddressingDictionary.Address, XD.Addressing200408Dictionary.Namespace); |
| | 3 | 1097 | | if (IsAnonymous) |
| | | 1098 | | { |
| | 1 | 1099 | | writer.WriteString(XD.Addressing200408Dictionary.Anonymous); |
| | | 1100 | | } |
| | 2 | 1101 | | else if (IsNone) |
| | | 1102 | | { |
| | 1 | 1103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion", SR.SFxNone2004); |
| | | 1104 | | } |
| | | 1105 | | else |
| | | 1106 | | { |
| | 1 | 1107 | | writer.WriteString(Uri.AbsoluteUri); |
| | | 1108 | | } |
| | | 1109 | | |
| | 2 | 1110 | | writer.WriteEndElement(); |
| | | 1111 | | |
| | | 1112 | | // ReferenceProperties |
| | 2 | 1113 | | if (Headers != null && Headers.Count > 0) |
| | | 1114 | | { |
| | 0 | 1115 | | writer.WriteStartElement(XD.AddressingDictionary.ReferenceProperties, XD.Addressing200408Dictionary.Name |
| | 0 | 1116 | | Headers.WriteContentsTo(writer); |
| | 0 | 1117 | | writer.WriteEndElement(); |
| | | 1118 | | } |
| | | 1119 | | |
| | | 1120 | | // ReferenceParameters |
| | 2 | 1121 | | if (Headers != null && Headers.Count > 0) |
| | | 1122 | | { |
| | 0 | 1123 | | writer.WriteStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing200408Dictionary.Name |
| | 0 | 1124 | | Headers.WriteContentsTo(writer); |
| | 0 | 1125 | | writer.WriteEndElement(); |
| | | 1126 | | } |
| | | 1127 | | |
| | | 1128 | | // PSP (PortType, ServiceName, Policy) |
| | 2 | 1129 | | XmlDictionaryReader reader = null; |
| | 2 | 1130 | | if (_pspSection >= 0) |
| | | 1131 | | { |
| | 0 | 1132 | | reader = GetReaderAtSection(Buffer, _pspSection); |
| | 0 | 1133 | | Copy(writer, reader); |
| | | 1134 | | } |
| | | 1135 | | |
| | | 1136 | | // Metadata |
| | 2 | 1137 | | reader = null; |
| | 2 | 1138 | | if (_metadataSection >= 0) |
| | | 1139 | | { |
| | 0 | 1140 | | reader = GetReaderAtSection(Buffer, _metadataSection); |
| | 0 | 1141 | | Copy(writer, reader); |
| | | 1142 | | } |
| | | 1143 | | |
| | | 1144 | | // EndpointIdentity |
| | 2 | 1145 | | if (Identity != null) |
| | | 1146 | | { |
| | 0 | 1147 | | Identity.WriteTo(writer); |
| | | 1148 | | } |
| | | 1149 | | |
| | | 1150 | | // Extensions |
| | 2 | 1151 | | if (_extensionSection >= 0) |
| | | 1152 | | { |
| | 0 | 1153 | | reader = GetReaderAtSection(Buffer, _extensionSection); |
| | 0 | 1154 | | while (reader.IsStartElement()) |
| | | 1155 | | { |
| | 0 | 1156 | | if (reader.NamespaceURI == AddressingVersion.WSAddressingAugust2004.Namespace) |
| | | 1157 | | { |
| | 0 | 1158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.Format(S |
| | | 1159 | | } |
| | | 1160 | | |
| | 0 | 1161 | | writer.WriteNode(reader, true); |
| | | 1162 | | } |
| | | 1163 | | } |
| | 2 | 1164 | | } |
| | | 1165 | | |
| | | 1166 | | public void WriteContentsTo(AddressingVersion addressingVersion, XmlWriter writer) |
| | | 1167 | | { |
| | 9 | 1168 | | XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); |
| | 9 | 1169 | | WriteContentsTo(addressingVersion, dictionaryWriter); |
| | 8 | 1170 | | } |
| | | 1171 | | |
| | | 1172 | | public void WriteTo(AddressingVersion addressingVersion, XmlDictionaryWriter writer, XmlDictionaryString localNa |
| | | 1173 | | { |
| | 11 | 1174 | | if (writer == null) |
| | | 1175 | | { |
| | 0 | 1176 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 1177 | | } |
| | 11 | 1178 | | if (addressingVersion == null) |
| | | 1179 | | { |
| | 0 | 1180 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion)); |
| | | 1181 | | } |
| | 11 | 1182 | | if (localName == null) |
| | | 1183 | | { |
| | 0 | 1184 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(localName)); |
| | | 1185 | | } |
| | 11 | 1186 | | if (ns == null) |
| | | 1187 | | { |
| | 0 | 1188 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns)); |
| | | 1189 | | } |
| | 11 | 1190 | | writer.WriteStartElement(localName, ns); |
| | 11 | 1191 | | WriteContentsTo(addressingVersion, writer); |
| | 11 | 1192 | | writer.WriteEndElement(); |
| | 11 | 1193 | | } |
| | | 1194 | | |
| | | 1195 | | public void WriteTo(AddressingVersion addressingVersion, XmlWriter writer) |
| | | 1196 | | { |
| | 11 | 1197 | | XmlDictionaryString dictionaryNamespace = addressingVersion.DictionaryNamespace; |
| | 11 | 1198 | | if (dictionaryNamespace == null) |
| | | 1199 | | { |
| | 0 | 1200 | | dictionaryNamespace = XD.AddressingDictionary.Empty; |
| | | 1201 | | } |
| | | 1202 | | |
| | 11 | 1203 | | WriteTo(addressingVersion, XmlDictionaryWriter.CreateDictionaryWriter(writer), |
| | 11 | 1204 | | XD.AddressingDictionary.EndpointReference, dictionaryNamespace); |
| | 11 | 1205 | | } |
| | | 1206 | | |
| | | 1207 | | public static bool operator ==(EndpointAddress address1, EndpointAddress address2) |
| | | 1208 | | { |
| | 3398 | 1209 | | if (ReferenceEquals(address2, null)) |
| | | 1210 | | { |
| | 3207 | 1211 | | return (ReferenceEquals(address1, null)); |
| | | 1212 | | } |
| | | 1213 | | |
| | 191 | 1214 | | return address2.Equals(address1); |
| | | 1215 | | } |
| | | 1216 | | |
| | | 1217 | | public static bool operator !=(EndpointAddress address1, EndpointAddress address2) |
| | | 1218 | | { |
| | 2809 | 1219 | | if (ReferenceEquals(address2, null)) |
| | | 1220 | | { |
| | 2809 | 1221 | | return !ReferenceEquals(address1, null); |
| | | 1222 | | } |
| | | 1223 | | |
| | 0 | 1224 | | return !address2.Equals(address1); |
| | | 1225 | | } |
| | | 1226 | | } |
| | | 1227 | | |
| | | 1228 | | public class EndpointAddressBuilder |
| | | 1229 | | { |
| | | 1230 | | private XmlBuffer _extensionBuffer; // this buffer is wrapped just like in EndpointAddress |
| | | 1231 | | private XmlBuffer _metadataBuffer; // this buffer is wrapped just like in EndpointAddress |
| | | 1232 | | private bool _hasExtension; |
| | | 1233 | | private bool _hasMetadata; |
| | | 1234 | | private readonly EndpointAddress _epr; |
| | | 1235 | | |
| | | 1236 | | public EndpointAddressBuilder() |
| | | 1237 | | { |
| | | 1238 | | Headers = new Collection<AddressHeader>(); |
| | | 1239 | | } |
| | | 1240 | | |
| | | 1241 | | public EndpointAddressBuilder(EndpointAddress address) |
| | | 1242 | | { |
| | | 1243 | | _epr = address ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(address)); |
| | | 1244 | | Uri = address.Uri; |
| | | 1245 | | Identity = address.Identity; |
| | | 1246 | | Headers = new Collection<AddressHeader>(); |
| | | 1247 | | for (int i = 0; i < address.Headers.Count; i++) |
| | | 1248 | | { |
| | | 1249 | | Headers.Add(address.Headers[i]); |
| | | 1250 | | } |
| | | 1251 | | } |
| | | 1252 | | |
| | | 1253 | | public Uri Uri { get; set; } |
| | | 1254 | | |
| | | 1255 | | public EndpointIdentity Identity { get; set; } |
| | | 1256 | | |
| | | 1257 | | public Collection<AddressHeader> Headers { get; } |
| | | 1258 | | |
| | | 1259 | | public XmlDictionaryReader GetReaderAtMetadata() |
| | | 1260 | | { |
| | | 1261 | | if (!_hasMetadata) |
| | | 1262 | | { |
| | | 1263 | | return _epr?.GetReaderAtMetadata(); |
| | | 1264 | | } |
| | | 1265 | | |
| | | 1266 | | if (_metadataBuffer == null) |
| | | 1267 | | { |
| | | 1268 | | return null; |
| | | 1269 | | } |
| | | 1270 | | |
| | | 1271 | | XmlDictionaryReader reader = _metadataBuffer.GetReader(0); |
| | | 1272 | | reader.MoveToContent(); |
| | | 1273 | | Fx.Assert(reader.Name == EndpointAddress.DummyName, "EndpointAddressBuilder: Expected dummy element not foun |
| | | 1274 | | reader.Read(); // consume the wrapper element |
| | | 1275 | | return reader; |
| | | 1276 | | } |
| | | 1277 | | |
| | | 1278 | | public void SetMetadataReader(XmlDictionaryReader reader) |
| | | 1279 | | { |
| | | 1280 | | _hasMetadata = true; |
| | | 1281 | | _metadataBuffer = null; |
| | | 1282 | | if (reader != null) |
| | | 1283 | | { |
| | | 1284 | | _metadataBuffer = new XmlBuffer(short.MaxValue); |
| | | 1285 | | XmlDictionaryWriter writer = _metadataBuffer.OpenSection(reader.Quotas); |
| | | 1286 | | writer.WriteStartElement(EndpointAddress.DummyName, EndpointAddress.DummyNamespace); |
| | | 1287 | | EndpointAddress.Copy(writer, reader); |
| | | 1288 | | _metadataBuffer.CloseSection(); |
| | | 1289 | | _metadataBuffer.Close(); |
| | | 1290 | | } |
| | | 1291 | | } |
| | | 1292 | | |
| | | 1293 | | public XmlDictionaryReader GetReaderAtExtensions() |
| | | 1294 | | { |
| | | 1295 | | if (!_hasExtension) |
| | | 1296 | | { |
| | | 1297 | | return _epr?.GetReaderAtExtensions(); |
| | | 1298 | | } |
| | | 1299 | | |
| | | 1300 | | if (_extensionBuffer == null) |
| | | 1301 | | { |
| | | 1302 | | return null; |
| | | 1303 | | } |
| | | 1304 | | |
| | | 1305 | | XmlDictionaryReader reader = _extensionBuffer.GetReader(0); |
| | | 1306 | | reader.MoveToContent(); |
| | | 1307 | | Fx.Assert(reader.Name == EndpointAddress.DummyName, "EndpointAddressBuilder: Expected dummy element not foun |
| | | 1308 | | reader.Read(); // consume the wrapper element |
| | | 1309 | | return reader; |
| | | 1310 | | } |
| | | 1311 | | |
| | | 1312 | | public void SetExtensionReader(XmlDictionaryReader reader) |
| | | 1313 | | { |
| | | 1314 | | _hasExtension = true; |
| | | 1315 | | _extensionBuffer = EndpointAddress.ReadExtensions(reader, null, null, out EndpointIdentity identity, out int |
| | | 1316 | | if (_extensionBuffer != null) |
| | | 1317 | | { |
| | | 1318 | | _extensionBuffer.Close(); |
| | | 1319 | | } |
| | | 1320 | | if (identity != null) |
| | | 1321 | | { |
| | | 1322 | | Identity = identity; |
| | | 1323 | | } |
| | | 1324 | | } |
| | | 1325 | | |
| | | 1326 | | public EndpointAddress ToEndpointAddress() |
| | | 1327 | | { |
| | | 1328 | | return new EndpointAddress( |
| | | 1329 | | Uri, |
| | | 1330 | | Identity, |
| | | 1331 | | new AddressHeaderCollection(Headers), |
| | | 1332 | | GetReaderAtMetadata(), |
| | | 1333 | | GetReaderAtExtensions(), |
| | | 1334 | | _epr?.GetReaderAtPsp()); |
| | | 1335 | | } |
| | | 1336 | | } |
| | | 1337 | | } |