| | | 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.Globalization; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Xml; |
| | | 10 | | |
| | | 11 | | using CoreWCF.Description; |
| | | 12 | | using CoreWCF.Diagnostics; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | using CoreWCF.Runtime.Diagnostics; |
| | | 15 | | using CoreWCF.Xml; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Channels |
| | | 18 | | { |
| | | 19 | | internal class MtomMessageEncoderFactory : MessageEncoderFactory |
| | | 20 | | { |
| | | 21 | | private MessageVersion _messageVersion; |
| | | 22 | | private Encoding _writeEncoding; |
| | | 23 | | private int _maxReadPoolSize; |
| | | 24 | | private int _maxWritePoolSize; |
| | | 25 | | private int _maxBufferSize; |
| | | 26 | | private XmlDictionaryReaderQuotas _readerQuotas; |
| | | 27 | | |
| | | 28 | | // Pools used by MtomMessageEncoder |
| | | 29 | | private const int MaxPooledXmlReadersPerMessage = 2; |
| | | 30 | | private object _thisLock; |
| | | 31 | | private OnXmlDictionaryReaderClose _onStreamedReaderClose; |
| | | 32 | | // Double-checked locking pattern requires volatile for read/write synchronization |
| | | 33 | | private volatile SynchronizedPool<XmlDictionaryWriter> _streamedWriterPool; |
| | | 34 | | private volatile SynchronizedPool<XmlDictionaryReader> _streamedReaderPool; |
| | | 35 | | private volatile SynchronizedPool<MtomMessageEncoder.MtomBufferedMessageData> _bufferedReaderPool; |
| | | 36 | | private volatile SynchronizedPool<MtomMessageEncoder.MtomBufferedMessageWriter> _bufferedWriterPool; |
| | | 37 | | private volatile SynchronizedPool<RecycledMessageState> _recycledStatePool; |
| | | 38 | | |
| | | 39 | | public MtomMessageEncoderFactory(MessageVersion version, Encoding writeEncoding, int maxReadPoolSize, int maxWri |
| | | 40 | | { |
| | | 41 | | _messageVersion = version; |
| | | 42 | | _writeEncoding = writeEncoding; |
| | | 43 | | _maxReadPoolSize = maxReadPoolSize; |
| | | 44 | | _maxWritePoolSize = maxWritePoolSize; |
| | | 45 | | _maxBufferSize = maxBufferSize; |
| | | 46 | | _readerQuotas = quotas; |
| | | 47 | | _thisLock = new object(); |
| | | 48 | | _onStreamedReaderClose = new OnXmlDictionaryReaderClose(ReturnStreamedReader); |
| | | 49 | | if (version.Envelope == EnvelopeVersion.Soap12) |
| | | 50 | | { |
| | | 51 | | ContentEncodingMap = TextMessageEncoderFactory.Soap12Content; |
| | | 52 | | } |
| | | 53 | | else if (version.Envelope == EnvelopeVersion.Soap11) |
| | | 54 | | { |
| | | 55 | | ContentEncodingMap = TextMessageEncoderFactory.Soap11Content; |
| | | 56 | | } |
| | | 57 | | else |
| | | 58 | | { |
| | | 59 | | Fx.Assert("Invalid MessageVersion"); |
| | | 60 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(string.Format(Cu |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public override MessageEncoder Encoder => new MtomMessageEncoder(_messageVersion, _writeEncoding, _maxReadPoolSi |
| | | 65 | | |
| | | 66 | | public override MessageVersion MessageVersion => _messageVersion; |
| | | 67 | | |
| | | 68 | | public int MaxWritePoolSize => _maxWritePoolSize; |
| | | 69 | | |
| | | 70 | | public int MaxReadPoolSize => _maxReadPoolSize; |
| | | 71 | | |
| | | 72 | | public XmlDictionaryReaderQuotas ReaderQuotas => _readerQuotas; |
| | | 73 | | |
| | | 74 | | public int MaxBufferSize => _maxBufferSize; |
| | | 75 | | |
| | | 76 | | internal ContentEncoding[] ContentEncodingMap { get; } |
| | | 77 | | |
| | | 78 | | internal XmlDictionaryWriter TakeStreamedWriter(Stream stream, string startInfo, string boundary, string startUr |
| | | 79 | | { |
| | | 80 | | if (_streamedWriterPool == null) |
| | | 81 | | { |
| | | 82 | | lock (_thisLock) |
| | | 83 | | { |
| | | 84 | | if (_streamedWriterPool == null) |
| | | 85 | | { |
| | | 86 | | _streamedWriterPool = new SynchronizedPool<XmlDictionaryWriter>(MaxWritePoolSize); |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | XmlDictionaryWriter xmlWriter = _streamedWriterPool.Take(); |
| | | 91 | | if (xmlWriter == null) |
| | | 92 | | { |
| | | 93 | | xmlWriter = XmlMtomWriter.Create(stream, _writeEncoding, int.MaxValue, startInfo, boundary, startUri, wr |
| | | 94 | | // if (WcfEventSource.Instance.WritePoolMissIsEnabled()) |
| | | 95 | | // { |
| | | 96 | | // WcfEventSource.Instance.WritePoolMiss(xmlWriter.GetType().Name); |
| | | 97 | | // } |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | | 101 | | ((IXmlMtomWriterInitializer)xmlWriter).SetOutput(stream, _writeEncoding, int.MaxValue, startInfo, bounda |
| | | 102 | | } |
| | | 103 | | return xmlWriter; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | internal void ReturnStreamedWriter(XmlDictionaryWriter xmlWriter) |
| | | 107 | | { |
| | | 108 | | xmlWriter.Close(); |
| | | 109 | | _streamedWriterPool.Return(xmlWriter); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | internal MtomMessageEncoder.MtomBufferedMessageWriter TakeBufferedWriter(MtomMessageEncoder messageEncoder) |
| | | 113 | | { |
| | | 114 | | if (_bufferedWriterPool == null) |
| | | 115 | | { |
| | | 116 | | lock (_thisLock) |
| | | 117 | | { |
| | | 118 | | if (_bufferedWriterPool == null) |
| | | 119 | | { |
| | | 120 | | _bufferedWriterPool = new SynchronizedPool<MtomMessageEncoder.MtomBufferedMessageWriter>(MaxWrit |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | MtomMessageEncoder.MtomBufferedMessageWriter messageWriter = _bufferedWriterPool.Take(); |
| | | 126 | | if (messageWriter == null) |
| | | 127 | | { |
| | | 128 | | messageWriter = new MtomMessageEncoder.MtomBufferedMessageWriter(messageEncoder); |
| | | 129 | | // if (WcfEventSource.Instance.WritePoolMissIsEnabled()) |
| | | 130 | | // { |
| | | 131 | | // WcfEventSource.Instance.WritePoolMiss(messageWriter.GetType().Name); |
| | | 132 | | // } |
| | | 133 | | } |
| | | 134 | | return messageWriter; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | internal void ReturnMessageWriter(MtomMessageEncoder.MtomBufferedMessageWriter messageWriter) |
| | | 138 | | { |
| | | 139 | | _bufferedWriterPool.Return(messageWriter); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | internal MtomMessageEncoder.MtomBufferedMessageData TakeBufferedReader(MtomMessageEncoder messageEncoder) |
| | | 143 | | { |
| | | 144 | | if (_bufferedReaderPool == null) |
| | | 145 | | { |
| | | 146 | | lock (_thisLock) |
| | | 147 | | { |
| | | 148 | | if (_bufferedReaderPool == null) |
| | | 149 | | { |
| | | 150 | | _bufferedReaderPool = new SynchronizedPool<MtomMessageEncoder.MtomBufferedMessageData>(MaxReadPo |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | MtomMessageEncoder.MtomBufferedMessageData messageData = _bufferedReaderPool.Take(); |
| | | 155 | | if (messageData == null) |
| | | 156 | | { |
| | | 157 | | messageData = new MtomMessageEncoder.MtomBufferedMessageData(messageEncoder, MaxPooledXmlReadersPerMessa |
| | | 158 | | // if (WcfEventSource.Instance.ReadPoolMissIsEnabled()) |
| | | 159 | | // { |
| | | 160 | | // WcfEventSource.Instance.ReadPoolMiss(messageData.GetType().Name); |
| | | 161 | | // } |
| | | 162 | | } |
| | | 163 | | return messageData; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | internal void ReturnBufferedData(MtomMessageEncoder.MtomBufferedMessageData messageData) |
| | | 167 | | { |
| | | 168 | | _bufferedReaderPool.Return(messageData); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal XmlReader TakeStreamedReader(Stream stream, string contentType, bool isMtomContentType) |
| | | 172 | | { |
| | | 173 | | if (_streamedReaderPool == null) |
| | | 174 | | { |
| | | 175 | | lock (_thisLock) |
| | | 176 | | { |
| | | 177 | | if (_streamedReaderPool == null) |
| | | 178 | | { |
| | | 179 | | _streamedReaderPool = new SynchronizedPool<XmlDictionaryReader>(MaxReadPoolSize); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | } |
| | | 183 | | XmlDictionaryReader xmlReader = _streamedReaderPool.Take(); |
| | | 184 | | try |
| | | 185 | | { |
| | | 186 | | if (contentType == null || isMtomContentType) |
| | | 187 | | { |
| | | 188 | | if (xmlReader != null && xmlReader is IXmlMtomReaderInitializer) |
| | | 189 | | { |
| | | 190 | | ((IXmlMtomReaderInitializer)xmlReader).SetInput(stream, MtomMessageEncoderFactory.GetSupportedEn |
| | | 191 | | } |
| | | 192 | | else |
| | | 193 | | { |
| | | 194 | | xmlReader = XmlMtomReader.Create(stream, MtomMessageEncoderFactory.GetSupportedEncodings(), cont |
| | | 195 | | // if (WcfEventSource.Instance.ReadPoolMissIsEnabled()) |
| | | 196 | | // { |
| | | 197 | | // WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name); |
| | | 198 | | // } |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | else |
| | | 202 | | { |
| | | 203 | | if (xmlReader != null && xmlReader is IXmlTextReaderInitializer) |
| | | 204 | | { |
| | | 205 | | ((IXmlTextReaderInitializer)xmlReader).SetInput(stream, TextMessageEncoderFactory.GetEncodingFro |
| | | 206 | | } |
| | | 207 | | else |
| | | 208 | | { |
| | | 209 | | xmlReader = XmlDictionaryReader.CreateTextReader(stream, TextMessageEncoderFactory.GetEncodingFr |
| | | 210 | | // if (WcfEventSource.Instance.ReadPoolMissIsEnabled()) |
| | | 211 | | // { |
| | | 212 | | // WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name); |
| | | 213 | | // } |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | catch (FormatException fe) |
| | | 218 | | { |
| | | 219 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | | 220 | | SR.SFxErrorCreatingMtomReader, fe)); |
| | | 221 | | } |
| | | 222 | | catch (XmlException xe) |
| | | 223 | | { |
| | | 224 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | | 225 | | SR.SFxErrorCreatingMtomReader, xe)); |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | return xmlReader; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | internal void ReturnStreamedReader(XmlDictionaryReader xmlReader) |
| | | 232 | | { |
| | | 233 | | _streamedReaderPool.Return(xmlReader); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | internal SynchronizedPool<RecycledMessageState> RecycledStatePool |
| | | 237 | | { |
| | | 238 | | get |
| | | 239 | | { |
| | | 240 | | if (_recycledStatePool == null) |
| | | 241 | | { |
| | | 242 | | lock (_thisLock) |
| | | 243 | | { |
| | | 244 | | if (_recycledStatePool == null) |
| | | 245 | | { |
| | | 246 | | _recycledStatePool = new SynchronizedPool<RecycledMessageState>(MaxReadPoolSize); |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | return _recycledStatePool; |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | public static Encoding[] GetSupportedEncodings() |
| | | 255 | | { |
| | | 256 | | Encoding[] supported = TextEncoderDefaults.SupportedEncodings; |
| | | 257 | | Encoding[] enc = new Encoding[supported.Length]; |
| | | 258 | | Array.Copy(supported, enc, supported.Length); |
| | | 259 | | return enc; |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | // Some notes: |
| | | 265 | | // The Encoding passed in is used for the SOAP envelope |
| | | 266 | | internal class MtomMessageEncoder : MessageEncoder, ITraceSourceStringProvider |
| | | 267 | | { |
| | | 268 | | private Encoding _writeEncoding; |
| | | 269 | | private string _contentType; |
| | | 270 | | private string _boundary; |
| | | 271 | | private MessageVersion _version; |
| | | 272 | | private static UriGenerator s_mimeBoundaryGenerator; |
| | | 273 | | private XmlDictionaryReaderQuotas _bufferedReadReaderQuotas; |
| | | 274 | | |
| | | 275 | | private MtomMessageEncoderFactory _factory; |
| | | 276 | | private const string MtomMediaType = "multipart/related"; |
| | | 277 | | private const string MtomContentType = MtomMediaType + "; type=\"application/xop+xml\""; |
| | | 278 | | private const string MtomStartUri = NamingHelper.DefaultNamespace + "0"; |
| | | 279 | | |
| | 29 | 280 | | public MtomMessageEncoder(MessageVersion version, Encoding writeEncoding, int maxReadPoolSize, int maxWritePoolS |
| | | 281 | | { |
| | 29 | 282 | | if (version == null) |
| | 0 | 283 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(version)); |
| | 29 | 284 | | if (writeEncoding == null) |
| | 0 | 285 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writeEncoding)); |
| | | 286 | | |
| | 29 | 287 | | _factory = factory; |
| | 29 | 288 | | TextEncoderDefaults.ValidateEncoding(writeEncoding); |
| | 29 | 289 | | _writeEncoding = writeEncoding; |
| | | 290 | | |
| | 29 | 291 | | MaxReadPoolSize = maxReadPoolSize; |
| | 29 | 292 | | MaxWritePoolSize = maxWritePoolSize; |
| | | 293 | | |
| | 29 | 294 | | ReaderQuotas = new XmlDictionaryReaderQuotas(); |
| | 29 | 295 | | quotas.CopyTo(ReaderQuotas); |
| | | 296 | | |
| | 29 | 297 | | _bufferedReadReaderQuotas = EncoderHelpers.GetBufferedReadQuotas(ReaderQuotas); |
| | 29 | 298 | | MaxBufferSize = maxBufferSize; |
| | 29 | 299 | | _version = version; |
| | 29 | 300 | | _contentType = GetContentType(out _boundary); |
| | 29 | 301 | | } |
| | | 302 | | |
| | | 303 | | private static UriGenerator MimeBoundaryGenerator |
| | | 304 | | { |
| | | 305 | | get |
| | | 306 | | { |
| | 29 | 307 | | if (s_mimeBoundaryGenerator == null) |
| | 1 | 308 | | s_mimeBoundaryGenerator = new UriGenerator("uuid", "+"); |
| | 29 | 309 | | return s_mimeBoundaryGenerator; |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | |
| | 38 | 313 | | public override string ContentType => _contentType; |
| | | 314 | | |
| | 0 | 315 | | public int MaxWritePoolSize { get; } |
| | | 316 | | |
| | 0 | 317 | | public int MaxReadPoolSize { get; } |
| | | 318 | | |
| | 58 | 319 | | public XmlDictionaryReaderQuotas ReaderQuotas { get; } |
| | | 320 | | |
| | 10 | 321 | | public int MaxBufferSize { get; } |
| | | 322 | | |
| | 18 | 323 | | public override string MediaType => MtomMediaType; |
| | | 324 | | |
| | 21 | 325 | | public override MessageVersion MessageVersion => _version; |
| | | 326 | | |
| | | 327 | | internal bool IsMTOMContentType(string contentType) |
| | | 328 | | { |
| | | 329 | | // check for MTOM contentType: multipart/related; type=\"application/xop+xml\" |
| | 18 | 330 | | return IsContentTypeSupported(contentType, ContentType, MediaType); |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | internal bool IsTextContentType(string contentType) |
| | | 334 | | { |
| | | 335 | | // check for Text contentType: text/xml or application/soap+xml |
| | 0 | 336 | | string textMediaType = TextMessageEncoderFactory.GetMediaType(_version); |
| | 0 | 337 | | string textContentType = TextMessageEncoderFactory.GetContentType(textMediaType, _writeEncoding); |
| | 0 | 338 | | return IsContentTypeSupported(contentType, textContentType, textMediaType); |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | public override bool IsContentTypeSupported(string contentType) |
| | | 342 | | { |
| | 9 | 343 | | if (contentType == null) |
| | 0 | 344 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(contentType)) |
| | 9 | 345 | | return (IsMTOMContentType(contentType) || IsTextContentType(contentType)); |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | protected override bool IsCharSetSupported(string charSet) |
| | | 349 | | { |
| | 18 | 350 | | if (charSet == null || charSet.Length == 0) |
| | 18 | 351 | | return true; |
| | | 352 | | |
| | | 353 | | Encoding tmp; |
| | 0 | 354 | | return TextEncoderDefaults.TryGetEncoding(charSet, out tmp); |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | private string GenerateStartInfoString() |
| | | 358 | | { |
| | 40 | 359 | | return (_version.Envelope == EnvelopeVersion.Soap12) ? TextMessageEncoderFactory.Soap12MediaType : TextMessa |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) |
| | | 363 | | { |
| | 10 | 364 | | if (bufferManager == null) |
| | 0 | 365 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bufferManager)); |
| | | 366 | | |
| | 10 | 367 | | if (contentType == ContentType) |
| | 0 | 368 | | contentType = null; |
| | | 369 | | |
| | | 370 | | // if (WcfEventSource.Instance.MtomMessageDecodingStartIsEnabled()) |
| | | 371 | | // { |
| | | 372 | | // WcfEventSource.Instance.MtomMessageDecodingStart(); |
| | | 373 | | // } |
| | | 374 | | |
| | 10 | 375 | | MtomBufferedMessageData messageData = _factory.TakeBufferedReader(this); |
| | 10 | 376 | | messageData._contentType = contentType; |
| | 10 | 377 | | messageData.Open(buffer, bufferManager); |
| | 10 | 378 | | RecycledMessageState messageState = messageData.TakeMessageState(); |
| | 10 | 379 | | if (messageState == null) |
| | 10 | 380 | | messageState = new RecycledMessageState(); |
| | 10 | 381 | | Message message = new BufferedMessage(messageData, messageState); |
| | 10 | 382 | | message.Properties.Encoder = this; |
| | | 383 | | // if (MessageLogger.LogMessagesAtTransportLevel) |
| | | 384 | | // MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportReceive); |
| | | 385 | | // |
| | | 386 | | // if (WcfEventSource.Instance.MessageReadByEncoderIsEnabled() && buffer != null) |
| | | 387 | | // { |
| | | 388 | | // WcfEventSource.Instance.MessageReadByEncoder( |
| | | 389 | | // EventTraceActivityHelper.TryExtractActivity(message, true), |
| | | 390 | | // buffer.Count, |
| | | 391 | | // this); |
| | | 392 | | // } |
| | | 393 | | |
| | 10 | 394 | | return message; |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | public override Task<Message> ReadMessageAsync(Stream stream, int maxSizeOfHeaders, string contentType) |
| | | 398 | | { |
| | 1 | 399 | | if (stream == null) |
| | 0 | 400 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(stream))); |
| | | 401 | | |
| | 1 | 402 | | if (contentType == ContentType) |
| | 0 | 403 | | contentType = null; |
| | | 404 | | |
| | | 405 | | // if (WcfEventSource.Instance.MtomMessageDecodingStartIsEnabled()) |
| | | 406 | | // { |
| | | 407 | | // WcfEventSource.Instance.MtomMessageDecodingStart(); |
| | | 408 | | // } |
| | | 409 | | |
| | 1 | 410 | | XmlReader reader = _factory.TakeStreamedReader(stream, contentType, contentType == null || IsMTOMContentType |
| | 1 | 411 | | Message message = Message.CreateMessage(reader, maxSizeOfHeaders, _version); |
| | 1 | 412 | | message.Properties.Encoder = this; |
| | | 413 | | |
| | | 414 | | // if (WcfEventSource.Instance.StreamedMessageReadByEncoderIsEnabled()) |
| | | 415 | | // { |
| | | 416 | | // WcfEventSource.Instance.StreamedMessageReadByEncoder(EventTraceActivityHelper.TryExtractActivity(mess |
| | | 417 | | // } |
| | | 418 | | // |
| | | 419 | | // if (MessageLogger.LogMessagesAtTransportLevel) |
| | | 420 | | // MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportReceive); |
| | 1 | 421 | | return Task.FromResult(message); |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager |
| | | 425 | | { |
| | 10 | 426 | | return WriteMessageInternal(message, maxMessageSize, bufferManager, messageOffset, GenerateStartInfoString() |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | // public override Task<ArraySegment<byte>> WriteMessageAsync(Message message, int maxMessageSize, BufferManager |
| | | 430 | | // { |
| | | 431 | | // return Task.FromResult(WriteMessageInternal(message, maxMessageSize, bufferManager, messageOffset, Genera |
| | | 432 | | // } |
| | | 433 | | |
| | | 434 | | private string GetContentType(out string boundary) |
| | | 435 | | { |
| | 29 | 436 | | string startInfo = GenerateStartInfoString(); |
| | 29 | 437 | | boundary = MimeBoundaryGenerator.Next(); |
| | | 438 | | |
| | 29 | 439 | | return FormatContentType(boundary, startInfo); |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | internal string FormatContentType(string boundary, string startInfo) |
| | | 443 | | { |
| | 29 | 444 | | return string.Format(CultureInfo.InvariantCulture, |
| | 29 | 445 | | "{0};start=\"<{1}>\";boundary=\"{2}\";start-info=\"{3}\"", |
| | 29 | 446 | | MtomContentType, MtomStartUri, boundary, startInfo); |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | private ArraySegment<byte> WriteMessageInternal(Message message, int maxMessageSize, BufferManager bufferManager |
| | | 450 | | { |
| | 10 | 451 | | bool writeMessageHeaders = true; |
| | 10 | 452 | | if (message.Properties.TryGetValue("CoreWCF.Channel.MtomMessageEncoder.WriteMessageHeaders", out object bool |
| | | 453 | | { |
| | 8 | 454 | | writeMessageHeaders = (bool)boolAsObject; |
| | | 455 | | } |
| | | 456 | | |
| | 10 | 457 | | if (message == null) |
| | 0 | 458 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | 10 | 459 | | if (bufferManager == null) |
| | 0 | 460 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bufferManager)); |
| | 10 | 461 | | if (maxMessageSize < 0) |
| | 0 | 462 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maxMess |
| | 0 | 463 | | SRCommon.ValueMustBeNonNegative)); |
| | 10 | 464 | | if (messageOffset < 0 || messageOffset > maxMessageSize) |
| | 0 | 465 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(message |
| | 0 | 466 | | SR.Format(SR.ValueMustBeInRange, 0, maxMessageSize))); |
| | 10 | 467 | | ThrowIfMismatchedMessageVersion(message); |
| | | 468 | | |
| | | 469 | | // EventTraceActivity eventTraceActivity = null; |
| | | 470 | | // if (WcfEventSource.Instance.MtomMessageEncodingStartIsEnabled()) |
| | | 471 | | // { |
| | | 472 | | // eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); |
| | | 473 | | // WcfEventSource.Instance.MtomMessageEncodingStart(eventTraceActivity); |
| | | 474 | | // } |
| | | 475 | | |
| | 10 | 476 | | message.Properties.Encoder = this; |
| | | 477 | | |
| | 10 | 478 | | MtomBufferedMessageWriter messageWriter = _factory.TakeBufferedWriter(this); |
| | 10 | 479 | | messageWriter._startInfo = startInfo; |
| | 10 | 480 | | messageWriter._boundary = boundary; |
| | 10 | 481 | | messageWriter._startUri = startUri; |
| | 10 | 482 | | messageWriter._writeMessageHeaders = writeMessageHeaders; |
| | 10 | 483 | | messageWriter._maxSizeInBytes = maxMessageSize; |
| | 10 | 484 | | ArraySegment<byte> messageData = messageWriter.WriteMessage(message, bufferManager, messageOffset, maxMessag |
| | 10 | 485 | | _factory.ReturnMessageWriter(messageWriter); |
| | | 486 | | |
| | | 487 | | // if (WcfEventSource.Instance.MessageWrittenByEncoderIsEnabled() && messageData != null) |
| | | 488 | | // { |
| | | 489 | | // WcfEventSource.Instance.MessageWrittenByEncoder( |
| | | 490 | | // eventTraceActivity ?? EventTraceActivityHelper.TryExtractActivity(message), |
| | | 491 | | // messageData.Count, |
| | | 492 | | // this); |
| | | 493 | | // } |
| | | 494 | | // |
| | | 495 | | // if (MessageLogger.LogMessagesAtTransportLevel) |
| | | 496 | | // { |
| | | 497 | | // string contentType = null; |
| | | 498 | | // if (boundary != null) |
| | | 499 | | // contentType = FormatContentType(boundary, startInfo ?? GenerateStartInfoString()); |
| | | 500 | | // |
| | | 501 | | // XmlDictionaryReader xmlDictionaryReader = XmlMtomReader.Create(messageData.Array, messageData.Offset, |
| | | 502 | | // MessageLogger.LogMessage(ref message, xmlDictionaryReader, MessageLoggingSource.TransportSend); |
| | | 503 | | // } |
| | | 504 | | |
| | 10 | 505 | | return messageData; |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | // public override void WriteMessage(Message message, Stream stream) |
| | | 509 | | // { |
| | | 510 | | // WriteMessageAsync(message, stream).WaitForCompletionNoSpin(); |
| | | 511 | | // } |
| | | 512 | | |
| | | 513 | | public override Task WriteMessageAsync(Message message, Stream stream) |
| | | 514 | | { |
| | 1 | 515 | | return WriteMessageInternalAsync(message, stream, GenerateStartInfoString(), _boundary, MtomStartUri); |
| | | 516 | | } |
| | | 517 | | |
| | | 518 | | // public override IAsyncResult BeginWriteMessage(Message message, Stream stream, AsyncCallback callback, object |
| | | 519 | | // { |
| | | 520 | | // return WriteMessageAsync(message, stream).ToApm(callback, state); |
| | | 521 | | // } |
| | | 522 | | // |
| | | 523 | | // public override void EndWriteMessage(IAsyncResult result) |
| | | 524 | | // { |
| | | 525 | | // result.ToApmEnd(); |
| | | 526 | | // } |
| | | 527 | | |
| | | 528 | | private async Task WriteMessageInternalAsync(Message message, Stream stream, string startInfo, string boundary, |
| | | 529 | | { |
| | 1 | 530 | | bool writeMessageHeaders = true; |
| | 1 | 531 | | if (message.Properties.TryGetValue("CoreWCF.Channel.MtomMessageEncoder.WriteMessageHeaders", out object bool |
| | | 532 | | { |
| | 1 | 533 | | writeMessageHeaders = (bool)boolAsObject; |
| | | 534 | | } |
| | | 535 | | |
| | 1 | 536 | | if (message == null) |
| | 0 | 537 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(message))); |
| | 1 | 538 | | if (stream == null) |
| | 0 | 539 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(stream)), message); |
| | 1 | 540 | | ThrowIfMismatchedMessageVersion(message); |
| | | 541 | | |
| | | 542 | | // EventTraceActivity eventTraceActivity = null; |
| | | 543 | | // if (WcfEventSource.Instance.MtomMessageEncodingStartIsEnabled()) |
| | | 544 | | // { |
| | | 545 | | // eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); |
| | | 546 | | // WcfEventSource.Instance.MtomMessageEncodingStart(eventTraceActivity); |
| | | 547 | | // } |
| | | 548 | | |
| | 1 | 549 | | message.Properties.Encoder = this; |
| | | 550 | | // if (MessageLogger.LogMessagesAtTransportLevel) |
| | | 551 | | // MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); |
| | 1 | 552 | | XmlDictionaryWriter xmlWriter = _factory.TakeStreamedWriter(stream, startInfo, boundary, startUri, writeMess |
| | 1 | 553 | | if (_writeEncoding.WebName == "utf-8") |
| | | 554 | | { |
| | 1 | 555 | | await message.WriteMessageAsync(xmlWriter); |
| | | 556 | | } |
| | | 557 | | else |
| | | 558 | | { |
| | 0 | 559 | | await xmlWriter.WriteStartDocumentAsync(); |
| | 0 | 560 | | await message.WriteMessageAsync(xmlWriter); |
| | 0 | 561 | | await xmlWriter.WriteEndDocumentAsync(); |
| | | 562 | | } |
| | | 563 | | |
| | 1 | 564 | | if (xmlWriter.SupportsAsync()) |
| | | 565 | | { |
| | 1 | 566 | | await xmlWriter.FlushAsync(); |
| | | 567 | | } |
| | | 568 | | else |
| | | 569 | | { |
| | 0 | 570 | | xmlWriter.Flush(); |
| | | 571 | | } |
| | 1 | 572 | | _factory.ReturnStreamedWriter(xmlWriter); |
| | | 573 | | |
| | | 574 | | // if (WcfEventSource.Instance.StreamedMessageWrittenByEncoderIsEnabled()) |
| | | 575 | | // { |
| | | 576 | | // WcfEventSource.Instance.StreamedMessageWrittenByEncoder(eventTraceActivity ?? EventTraceActivityHelpe |
| | | 577 | | // } |
| | 1 | 578 | | } |
| | | 579 | | |
| | | 580 | | string ITraceSourceStringProvider.GetSourceString() |
| | | 581 | | { |
| | 0 | 582 | | return GetTraceSourceString(); |
| | | 583 | | } |
| | | 584 | | |
| | | 585 | | internal class MtomBufferedMessageData : BufferedMessageData |
| | | 586 | | { |
| | | 587 | | private MtomMessageEncoder _messageEncoder; |
| | | 588 | | private Pool<XmlDictionaryReader> _readerPool; |
| | | 589 | | internal string _contentType; |
| | | 590 | | private OnXmlDictionaryReaderClose _onClose; |
| | | 591 | | |
| | | 592 | | public MtomBufferedMessageData(MtomMessageEncoder messageEncoder, int maxReaderPoolSize) |
| | 10 | 593 | | : base(messageEncoder._factory.RecycledStatePool) |
| | | 594 | | { |
| | 10 | 595 | | _messageEncoder = messageEncoder; |
| | 10 | 596 | | _readerPool = new Pool<XmlDictionaryReader>(maxReaderPoolSize); |
| | 10 | 597 | | _onClose = new OnXmlDictionaryReaderClose(OnXmlReaderClosed); |
| | 10 | 598 | | } |
| | | 599 | | |
| | 10 | 600 | | public override MessageEncoder MessageEncoder => _messageEncoder; |
| | | 601 | | |
| | 10 | 602 | | public override XmlDictionaryReaderQuotas Quotas => _messageEncoder._bufferedReadReaderQuotas; |
| | | 603 | | |
| | | 604 | | protected override void OnClosed() |
| | | 605 | | { |
| | 10 | 606 | | _messageEncoder._factory.ReturnBufferedData(this); |
| | 10 | 607 | | } |
| | | 608 | | |
| | | 609 | | protected override XmlDictionaryReader TakeXmlReader() |
| | | 610 | | { |
| | | 611 | | try |
| | | 612 | | { |
| | 10 | 613 | | ArraySegment<byte> buffer = Buffer; |
| | | 614 | | |
| | 10 | 615 | | XmlDictionaryReader xmlReader = _readerPool.Take(); |
| | 10 | 616 | | if (_contentType == null || _messageEncoder.IsMTOMContentType(_contentType)) |
| | | 617 | | { |
| | 10 | 618 | | if (xmlReader != null && xmlReader is IXmlMtomReaderInitializer) |
| | | 619 | | { |
| | 0 | 620 | | ((IXmlMtomReaderInitializer)xmlReader).SetInput(buffer.Array, buffer.Offset, buffer.Count, M |
| | | 621 | | } |
| | | 622 | | else |
| | | 623 | | { |
| | 10 | 624 | | xmlReader = XmlMtomReader.Create(buffer.Array, buffer.Offset, buffer.Count, MtomMessageEncod |
| | | 625 | | // if (WcfEventSource.Instance.ReadPoolMissIsEnabled()) |
| | | 626 | | // { |
| | | 627 | | // WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name); |
| | | 628 | | // } |
| | | 629 | | } |
| | | 630 | | } |
| | | 631 | | else |
| | | 632 | | { |
| | 0 | 633 | | if (xmlReader != null && xmlReader is IXmlTextReaderInitializer) |
| | | 634 | | { |
| | 0 | 635 | | ((IXmlTextReaderInitializer)xmlReader).SetInput(buffer.Array, buffer.Offset, buffer.Count, T |
| | | 636 | | } |
| | | 637 | | else |
| | | 638 | | { |
| | 0 | 639 | | xmlReader = XmlDictionaryReader.CreateTextReader(buffer.Array, buffer.Offset, buffer.Count, |
| | | 640 | | // if (WcfEventSource.Instance.ReadPoolMissIsEnabled()) |
| | | 641 | | // { |
| | | 642 | | // WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name); |
| | | 643 | | // } |
| | | 644 | | } |
| | | 645 | | } |
| | 10 | 646 | | return xmlReader; |
| | | 647 | | } |
| | 0 | 648 | | catch (FormatException fe) |
| | | 649 | | { |
| | 0 | 650 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 651 | | SR.SFxErrorCreatingMtomReader, fe)); |
| | | 652 | | } |
| | 0 | 653 | | catch (XmlException xe) |
| | | 654 | | { |
| | 0 | 655 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 656 | | SR.SFxErrorCreatingMtomReader, xe)); |
| | | 657 | | } |
| | 10 | 658 | | } |
| | | 659 | | |
| | | 660 | | protected override void ReturnXmlReader(XmlDictionaryReader xmlReader) |
| | | 661 | | { |
| | 10 | 662 | | if (xmlReader != null) |
| | 10 | 663 | | _readerPool.Return(xmlReader); |
| | 10 | 664 | | } |
| | | 665 | | } |
| | | 666 | | |
| | | 667 | | internal class MtomBufferedMessageWriter : BufferedMessageWriter |
| | | 668 | | { |
| | | 669 | | private MtomMessageEncoder _messageEncoder; |
| | | 670 | | internal bool _writeMessageHeaders; |
| | | 671 | | internal string _startInfo; |
| | | 672 | | internal string _startUri; |
| | | 673 | | internal string _boundary; |
| | 10 | 674 | | internal int _maxSizeInBytes = int.MaxValue; |
| | | 675 | | private XmlDictionaryWriter _writer; |
| | | 676 | | |
| | 10 | 677 | | public MtomBufferedMessageWriter(MtomMessageEncoder messageEncoder) |
| | | 678 | | { |
| | 10 | 679 | | _messageEncoder = messageEncoder; |
| | 10 | 680 | | } |
| | | 681 | | |
| | | 682 | | protected override XmlDictionaryWriter TakeXmlWriter(Stream stream) |
| | | 683 | | { |
| | 10 | 684 | | XmlDictionaryWriter returnedWriter = _writer; |
| | 10 | 685 | | if (returnedWriter == null) |
| | | 686 | | { |
| | 10 | 687 | | returnedWriter = XmlMtomWriter.Create(stream, _messageEncoder._writeEncoding, _maxSizeInBytes, _star |
| | | 688 | | } |
| | | 689 | | else |
| | | 690 | | { |
| | 0 | 691 | | _writer = null; |
| | 0 | 692 | | ((IXmlMtomWriterInitializer)returnedWriter).SetOutput(stream, _messageEncoder._writeEncoding, _maxSi |
| | | 693 | | } |
| | 10 | 694 | | if (_messageEncoder._writeEncoding.WebName != "utf-8") |
| | 0 | 695 | | returnedWriter.WriteStartDocument(); |
| | 10 | 696 | | return returnedWriter; |
| | | 697 | | } |
| | | 698 | | |
| | | 699 | | protected override void ReturnXmlWriter(XmlDictionaryWriter writer) |
| | | 700 | | { |
| | 10 | 701 | | writer.Close(); |
| | | 702 | | |
| | 10 | 703 | | if (_writer == null) |
| | 10 | 704 | | _writer = writer; |
| | 10 | 705 | | } |
| | | 706 | | } |
| | | 707 | | } |
| | | 708 | | } |