| | | 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.Text; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.Description; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | public sealed class TextMessageEncodingBindingElement : MessageEncodingBindingElement, IWsdlExportExtension, IPolicy |
| | | 12 | | { |
| | | 13 | | private int _maxReadPoolSize; |
| | | 14 | | private int _maxWritePoolSize; |
| | | 15 | | private readonly XmlDictionaryReaderQuotas _readerQuotas; |
| | | 16 | | private MessageVersion _messageVersion; |
| | | 17 | | private Encoding _writeEncoding; |
| | | 18 | | |
| | | 19 | | public TextMessageEncodingBindingElement() |
| | 1016 | 20 | | : this(MessageVersion.Default, TextEncoderDefaults.Encoding) |
| | | 21 | | { |
| | 1016 | 22 | | } |
| | | 23 | | |
| | 1206 | 24 | | public TextMessageEncodingBindingElement(MessageVersion messageVersion, Encoding writeEncoding) |
| | | 25 | | { |
| | 1206 | 26 | | if (writeEncoding == null) |
| | | 27 | | { |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writeEncoding)); |
| | | 29 | | } |
| | | 30 | | |
| | 1206 | 31 | | TextEncoderDefaults.ValidateEncoding(writeEncoding); |
| | | 32 | | |
| | 1206 | 33 | | _maxReadPoolSize = EncoderDefaults.MaxReadPoolSize; |
| | 1206 | 34 | | _maxWritePoolSize = EncoderDefaults.MaxWritePoolSize; |
| | 1206 | 35 | | _readerQuotas = new XmlDictionaryReaderQuotas(); |
| | 1206 | 36 | | EncoderDefaults.ReaderQuotas.CopyTo(_readerQuotas); |
| | 1206 | 37 | | _messageVersion = messageVersion ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof( |
| | 1206 | 38 | | _writeEncoding = writeEncoding; |
| | 1206 | 39 | | } |
| | | 40 | | |
| | | 41 | | private TextMessageEncodingBindingElement(TextMessageEncodingBindingElement elementToBeCloned) |
| | 7471 | 42 | | : base(elementToBeCloned) |
| | | 43 | | { |
| | 7471 | 44 | | _maxReadPoolSize = elementToBeCloned._maxReadPoolSize; |
| | 7471 | 45 | | _maxWritePoolSize = elementToBeCloned._maxWritePoolSize; |
| | 7471 | 46 | | _readerQuotas = new XmlDictionaryReaderQuotas(); |
| | 7471 | 47 | | elementToBeCloned._readerQuotas.CopyTo(_readerQuotas); |
| | 7471 | 48 | | _writeEncoding = elementToBeCloned._writeEncoding; |
| | 7471 | 49 | | _messageVersion = elementToBeCloned._messageVersion; |
| | 7471 | 50 | | } |
| | | 51 | | |
| | | 52 | | public int MaxReadPoolSize |
| | | 53 | | { |
| | | 54 | | get |
| | | 55 | | { |
| | 774 | 56 | | return _maxReadPoolSize; |
| | | 57 | | } |
| | | 58 | | set |
| | | 59 | | { |
| | 8 | 60 | | if (value <= 0) |
| | | 61 | | { |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 63 | | SRCommon.ValueMustBePositive)); |
| | | 64 | | } |
| | 8 | 65 | | _maxReadPoolSize = value; |
| | 8 | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public int MaxWritePoolSize |
| | | 70 | | { |
| | | 71 | | get |
| | | 72 | | { |
| | 774 | 73 | | return _maxWritePoolSize; |
| | | 74 | | } |
| | | 75 | | set |
| | | 76 | | { |
| | 8 | 77 | | if (value <= 0) |
| | | 78 | | { |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 80 | | SRCommon.ValueMustBePositive)); |
| | | 81 | | } |
| | 8 | 82 | | _maxWritePoolSize = value; |
| | 8 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public XmlDictionaryReaderQuotas ReaderQuotas |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 4646 | 90 | | return _readerQuotas; |
| | | 91 | | } |
| | | 92 | | set |
| | | 93 | | { |
| | 184 | 94 | | if (value == null) |
| | | 95 | | { |
| | 0 | 96 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 97 | | } |
| | | 98 | | |
| | 184 | 99 | | value.CopyTo(_readerQuotas); |
| | 184 | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public override MessageVersion MessageVersion |
| | | 104 | | { |
| | | 105 | | get |
| | | 106 | | { |
| | 4647 | 107 | | return _messageVersion; |
| | | 108 | | } |
| | | 109 | | set |
| | | 110 | | { |
| | 593 | 111 | | _messageVersion = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value |
| | 593 | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public Encoding WriteEncoding |
| | | 116 | | { |
| | | 117 | | get |
| | | 118 | | { |
| | 4628 | 119 | | return _writeEncoding; |
| | | 120 | | } |
| | | 121 | | set |
| | | 122 | | { |
| | 10 | 123 | | if (value == null) |
| | | 124 | | { |
| | 0 | 125 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 126 | | } |
| | | 127 | | |
| | 10 | 128 | | TextEncoderDefaults.ValidateEncoding(value); |
| | 10 | 129 | | _writeEncoding = value; |
| | 10 | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | public override BindingElement Clone() |
| | | 134 | | { |
| | 7471 | 135 | | return new TextMessageEncodingBindingElement(this); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | public override MessageEncoderFactory CreateMessageEncoderFactory() |
| | | 139 | | { |
| | 770 | 140 | | return new TextMessageEncoderFactory(MessageVersion, WriteEncoding, MaxReadPoolSize, MaxWritePoolSize, Reade |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public override T GetProperty<T>(BindingContext context) |
| | | 144 | | { |
| | 5352 | 145 | | if (context == null) |
| | | 146 | | { |
| | 0 | 147 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 148 | | } |
| | 5352 | 149 | | if (typeof(T) == typeof(XmlDictionaryReaderQuotas)) |
| | | 150 | | { |
| | 49 | 151 | | return (T)(object)_readerQuotas; |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 5303 | 155 | | return base.GetProperty<T>(context); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context) |
| | | 160 | | { |
| | 40 | 161 | | if (context == null) |
| | | 162 | | { |
| | 0 | 163 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 164 | | } |
| | 40 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { } |
| | | 168 | | |
| | | 169 | | void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context) |
| | | 170 | | { |
| | 31 | 171 | | if (context == null) |
| | | 172 | | { |
| | 0 | 173 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 174 | | } |
| | | 175 | | |
| | 31 | 176 | | SoapHelper.SetSoapVersion(context, exporter, MessageVersion.Envelope); |
| | 31 | 177 | | } |
| | | 178 | | |
| | | 179 | | protected override bool CheckEncodingVersion(EnvelopeVersion version) |
| | | 180 | | { |
| | 0 | 181 | | return _messageVersion.Envelope == version; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | protected override bool IsMatch(BindingElement b) |
| | | 185 | | { |
| | 0 | 186 | | if (!base.IsMatch(b)) |
| | | 187 | | { |
| | 0 | 188 | | return false; |
| | | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | if (!(b is TextMessageEncodingBindingElement text)) |
| | | 192 | | { |
| | 0 | 193 | | return false; |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | if (_maxReadPoolSize != text.MaxReadPoolSize) |
| | | 197 | | { |
| | 0 | 198 | | return false; |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | if (_maxWritePoolSize != text.MaxWritePoolSize) |
| | | 202 | | { |
| | 0 | 203 | | return false; |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | // compare XmlDictionaryReaderQuotas |
| | 0 | 207 | | if (_readerQuotas.MaxStringContentLength != text.ReaderQuotas.MaxStringContentLength) |
| | | 208 | | { |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | if (_readerQuotas.MaxArrayLength != text.ReaderQuotas.MaxArrayLength) |
| | | 213 | | { |
| | 0 | 214 | | return false; |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (_readerQuotas.MaxBytesPerRead != text.ReaderQuotas.MaxBytesPerRead) |
| | | 218 | | { |
| | 0 | 219 | | return false; |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | if (_readerQuotas.MaxDepth != text.ReaderQuotas.MaxDepth) |
| | | 223 | | { |
| | 0 | 224 | | return false; |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | if (_readerQuotas.MaxNameTableCharCount != text.ReaderQuotas.MaxNameTableCharCount) |
| | | 228 | | { |
| | 0 | 229 | | return false; |
| | | 230 | | } |
| | | 231 | | |
| | 0 | 232 | | if (WriteEncoding.EncodingName != text.WriteEncoding.EncodingName) |
| | | 233 | | { |
| | 0 | 234 | | return false; |
| | | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | if (!MessageVersion.IsMatch(text.MessageVersion)) |
| | | 238 | | { |
| | 0 | 239 | | return false; |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | return true; |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | } |