| | | 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.Xml; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels |
| | | 8 | | { |
| | | 9 | | internal abstract class TransportOutputChannel : OutputChannel |
| | | 10 | | { |
| | | 11 | | private readonly bool _anyHeadersToAdd; |
| | | 12 | | private readonly EndpointAddress _to; |
| | | 13 | | private readonly Uri _via; |
| | | 14 | | private readonly ToHeader _toHeader; |
| | | 15 | | |
| | | 16 | | protected TransportOutputChannel(IDefaultCommunicationTimeouts timeouts, EndpointAddress to, Uri via, bool manua |
| | 11 | 17 | | : base(timeouts) |
| | | 18 | | { |
| | 11 | 19 | | ManualAddressing = manualAddressing; |
| | 11 | 20 | | MessageVersion = messageVersion; |
| | 11 | 21 | | _to = to; |
| | 11 | 22 | | _via = via; |
| | | 23 | | |
| | 11 | 24 | | if (!manualAddressing && _to != null) |
| | | 25 | | { |
| | | 26 | | Uri toUri; |
| | 11 | 27 | | if (_to.IsAnonymous) |
| | | 28 | | { |
| | 11 | 29 | | toUri = MessageVersion.Addressing.AnonymousUri; |
| | | 30 | | } |
| | 0 | 31 | | else if (_to.IsNone) |
| | | 32 | | { |
| | 0 | 33 | | toUri = MessageVersion.Addressing.NoneUri; |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | 0 | 37 | | toUri = _to.Uri; |
| | | 38 | | } |
| | | 39 | | |
| | 11 | 40 | | if (toUri != null) |
| | | 41 | | { |
| | 11 | 42 | | XmlDictionaryString dictionaryTo = new ToDictionary(toUri.AbsoluteUri).To; |
| | 11 | 43 | | _toHeader = ToHeader.Create(toUri, dictionaryTo, messageVersion.Addressing); |
| | | 44 | | } |
| | | 45 | | |
| | 11 | 46 | | _anyHeadersToAdd = _to.Headers.Count > 0; |
| | | 47 | | } |
| | 11 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | protected bool ManualAddressing { get; } |
| | | 51 | | |
| | 11 | 52 | | public MessageVersion MessageVersion { get; } |
| | | 53 | | |
| | | 54 | | public override EndpointAddress RemoteAddress |
| | | 55 | | { |
| | | 56 | | get |
| | | 57 | | { |
| | 15 | 58 | | return _to; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public override Uri Via |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 0 | 66 | | return _via; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | protected override void AddHeadersTo(Message message) |
| | | 71 | | { |
| | 15 | 72 | | base.AddHeadersTo(message); |
| | | 73 | | |
| | 15 | 74 | | if (_toHeader != null) |
| | | 75 | | { |
| | | 76 | | // TODO: Removed performance enhancement to avoid exposing another internal method. |
| | | 77 | | // Evaluate whether we should do something to bring this back. My thoughts are we |
| | | 78 | | // remove the SetToHeader method as we should be using the same mechanism as third |
| | | 79 | | // parties transports have to use. |
| | 15 | 80 | | message.Headers.To = _toHeader.To; |
| | | 81 | | |
| | | 82 | | // Original comment and code: |
| | | 83 | | // we don't use to.ApplyTo(message) since it's faster to cache and |
| | | 84 | | // use the actual <To> header then to call message.Headers.To = Uri... |
| | | 85 | | //message.Headers.SetToHeader(toHeader); |
| | | 86 | | |
| | 15 | 87 | | if (_anyHeadersToAdd) |
| | | 88 | | { |
| | 0 | 89 | | _to.Headers.AddHeadersTo(message); |
| | | 90 | | } |
| | | 91 | | } |
| | 15 | 92 | | } |
| | | 93 | | |
| | | 94 | | private class ToDictionary : IXmlDictionary |
| | | 95 | | { |
| | 11 | 96 | | public ToDictionary(string to) |
| | | 97 | | { |
| | 11 | 98 | | To = new XmlDictionaryString(this, to, 0); |
| | 11 | 99 | | } |
| | | 100 | | |
| | 22 | 101 | | public XmlDictionaryString To { get; private set; } |
| | | 102 | | |
| | | 103 | | public bool TryLookup(string value, out XmlDictionaryString result) |
| | | 104 | | { |
| | 0 | 105 | | if (value == null) |
| | | 106 | | { |
| | 0 | 107 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | if (value == To.Value) |
| | | 111 | | { |
| | 0 | 112 | | result = To; |
| | 0 | 113 | | return true; |
| | | 114 | | } |
| | 0 | 115 | | result = null; |
| | 0 | 116 | | return false; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public bool TryLookup(int key, out XmlDictionaryString result) |
| | | 120 | | { |
| | 0 | 121 | | if (key == 0) |
| | | 122 | | { |
| | 0 | 123 | | result = To; |
| | 0 | 124 | | return true; |
| | | 125 | | } |
| | 0 | 126 | | result = null; |
| | 0 | 127 | | return false; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result) |
| | | 131 | | { |
| | 0 | 132 | | if (value == null) |
| | | 133 | | { |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | if (value == To) |
| | | 138 | | { |
| | 0 | 139 | | result = To; |
| | 0 | 140 | | return true; |
| | | 141 | | } |
| | 0 | 142 | | result = null; |
| | 0 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |