| | | 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.IO; |
| | | 6 | | using System.Net.Http; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | public static class ByteStreamMessage |
| | | 13 | | { |
| | | 14 | | public static Message CreateMessage(Stream stream) |
| | | 15 | | { |
| | 6 | 16 | | if (stream == null) |
| | | 17 | | { |
| | 1 | 18 | | throw Fx.Exception.ArgumentNull(nameof(stream)); |
| | | 19 | | } |
| | | 20 | | |
| | 5 | 21 | | return CreateMessage(stream, XmlDictionaryReaderQuotas.Max, true); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public static Message CreateMessage(ArraySegment<byte> buffer) |
| | | 25 | | { |
| | 7 | 26 | | return CreateMessage(buffer, null); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public static Message CreateMessage(ArraySegment<byte> buffer, BufferManager bufferManager) |
| | | 30 | | { |
| | 9 | 31 | | if (buffer.Array == null) |
| | | 32 | | { |
| | 1 | 33 | | throw Fx.Exception.ArgumentNull("buffer.Array", SR.Format(SR.ArgumentPropertyShouldNotBeNullError, "buff |
| | | 34 | | } |
| | | 35 | | |
| | 8 | 36 | | ByteStreamBufferedMessageData data = new ByteStreamBufferedMessageData(buffer, bufferManager); |
| | | 37 | | |
| | | 38 | | // moveBodyReaderToContent is true, for consistency with the other implementations of Message (including the |
| | 8 | 39 | | return CreateMessage(data, XmlDictionaryReaderQuotas.Max, true); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | internal static Message CreateMessage(Stream stream, XmlDictionaryReaderQuotas quotas, bool moveBodyReaderToCont |
| | | 43 | | { |
| | 7 | 44 | | return new InternalByteStreamMessage(stream, quotas, moveBodyReaderToContent); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static Message CreateMessage(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuotas quotas, b |
| | | 48 | | { |
| | | 49 | | // moveBodyReaderToContent is true, for consistency with the other implementations of Message (including the |
| | 0 | 50 | | return new InternalByteStreamMessage(httpRequestMessage, quotas, true); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | internal static Message CreateMessage(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuotas quotas) |
| | | 54 | | { |
| | | 55 | | // moveBodyReaderToContent is true, for consistency with the other implementations of Message (including the |
| | 0 | 56 | | return new InternalByteStreamMessage(httpResponseMessage, quotas, true); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | internal static Message CreateMessage(ByteStreamBufferedMessageData bufferedMessageData, XmlDictionaryReaderQuot |
| | | 60 | | { |
| | 11 | 61 | | return new InternalByteStreamMessage(bufferedMessageData, quotas, moveBodyReaderToContent); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal static bool IsInternalByteStreamMessage(Message message) |
| | | 65 | | { |
| | | 66 | | Fx.Assert(message != null, "message should not be null"); |
| | 0 | 67 | | return message is InternalByteStreamMessage; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | internal class InternalByteStreamMessage : Message |
| | | 71 | | { |
| | | 72 | | private BodyWriter _bodyWriter; |
| | | 73 | | private readonly MessageHeaders _headers; |
| | | 74 | | private readonly MessageProperties _properties; |
| | | 75 | | private XmlByteStreamReader _reader; |
| | | 76 | | private bool _moveBodyReaderToContent; |
| | | 77 | | |
| | 11 | 78 | | public InternalByteStreamMessage(ByteStreamBufferedMessageData bufferedMessageData, XmlDictionaryReaderQuota |
| | | 79 | | { |
| | | 80 | | // Assign both writer and reader here so that we can CreateBufferedCopy without the need to |
| | | 81 | | // abstract between a streamed or buffered message. We're protected here by the state on Message |
| | | 82 | | // preventing both a read/write. |
| | | 83 | | |
| | 11 | 84 | | quotas = ByteStreamMessageUtility.EnsureQuotas(quotas); |
| | | 85 | | |
| | 11 | 86 | | _bodyWriter = new BufferedBodyWriter(bufferedMessageData); |
| | 11 | 87 | | _headers = new MessageHeaders(MessageVersion.None); |
| | 11 | 88 | | _properties = new MessageProperties(); |
| | 11 | 89 | | _reader = new XmlBufferedByteStreamReader(bufferedMessageData, quotas); |
| | 11 | 90 | | _moveBodyReaderToContent = moveBodyReaderToContent; |
| | 11 | 91 | | } |
| | | 92 | | |
| | 7 | 93 | | public InternalByteStreamMessage(Stream stream, XmlDictionaryReaderQuotas quotas, bool moveBodyReaderToConte |
| | | 94 | | { |
| | | 95 | | // Assign both writer and reader here so that we can CreateBufferedCopy without the need to |
| | | 96 | | // abstract between a streamed or buffered message. We're protected here by the state on Message |
| | | 97 | | // preventing both a read/write on the same stream. |
| | | 98 | | |
| | 7 | 99 | | quotas = ByteStreamMessageUtility.EnsureQuotas(quotas); |
| | | 100 | | |
| | 7 | 101 | | _bodyWriter = StreamedBodyWriter.Create(stream); |
| | 7 | 102 | | _headers = new MessageHeaders(MessageVersion.None); |
| | 7 | 103 | | _properties = new MessageProperties(); |
| | 7 | 104 | | _reader = XmlStreamedByteStreamReader.Create(stream, quotas); |
| | 7 | 105 | | _moveBodyReaderToContent = moveBodyReaderToContent; |
| | 7 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | public InternalByteStreamMessage(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuotas quotas, bo |
| | | 109 | | { |
| | | 110 | | Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should not be null."); |
| | | 111 | | |
| | | 112 | | // Assign both writer and reader here so that we can CreateBufferedCopy without the need to |
| | | 113 | | // abstract between a streamed or buffered message. We're protected here by the state on Message |
| | | 114 | | // preventing both a read/write on the same stream. |
| | | 115 | | |
| | 0 | 116 | | quotas = ByteStreamMessageUtility.EnsureQuotas(quotas); |
| | | 117 | | |
| | 0 | 118 | | _bodyWriter = StreamedBodyWriter.Create(httpRequestMessage); |
| | 0 | 119 | | _headers = new MessageHeaders(MessageVersion.None); |
| | 0 | 120 | | _properties = new MessageProperties(); |
| | 0 | 121 | | _reader = XmlStreamedByteStreamReader.Create(httpRequestMessage, quotas); |
| | 0 | 122 | | _moveBodyReaderToContent = moveBodyReaderToContent; |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | public InternalByteStreamMessage(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuotas quotas, |
| | | 126 | | { |
| | | 127 | | Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should not be null."); |
| | | 128 | | |
| | | 129 | | // Assign both writer and reader here so that we can CreateBufferedCopy without the need to |
| | | 130 | | // abstract between a streamed or buffered message. We're protected here by the state on Message |
| | | 131 | | // preventing both a read/write on the same stream. |
| | | 132 | | |
| | 0 | 133 | | quotas = ByteStreamMessageUtility.EnsureQuotas(quotas); |
| | | 134 | | |
| | 0 | 135 | | _bodyWriter = StreamedBodyWriter.Create(httpResponseMessage); |
| | 0 | 136 | | _headers = new MessageHeaders(MessageVersion.None); |
| | 0 | 137 | | _properties = new MessageProperties(); |
| | 0 | 138 | | _reader = XmlStreamedByteStreamReader.Create(httpResponseMessage, quotas); |
| | 0 | 139 | | _moveBodyReaderToContent = moveBodyReaderToContent; |
| | 0 | 140 | | } |
| | | 141 | | |
| | 4 | 142 | | private InternalByteStreamMessage(ByteStreamBufferedMessageData messageData, MessageHeaders headers, Message |
| | | 143 | | { |
| | 4 | 144 | | _headers = new MessageHeaders(headers); |
| | 4 | 145 | | _properties = new MessageProperties(properties); |
| | 4 | 146 | | _bodyWriter = new BufferedBodyWriter(messageData); |
| | 4 | 147 | | _reader = new XmlBufferedByteStreamReader(messageData, quotas); |
| | 4 | 148 | | _moveBodyReaderToContent = moveBodyReaderToContent; |
| | 4 | 149 | | } |
| | | 150 | | |
| | | 151 | | public override MessageHeaders Headers |
| | | 152 | | { |
| | | 153 | | get |
| | | 154 | | { |
| | 10 | 155 | | if (IsDisposed) |
| | | 156 | | { |
| | 0 | 157 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 158 | | } |
| | | 159 | | |
| | 10 | 160 | | return _headers; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | public override bool IsEmpty |
| | | 165 | | { |
| | | 166 | | get |
| | | 167 | | { |
| | 17 | 168 | | if (IsDisposed) |
| | | 169 | | { |
| | 0 | 170 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 171 | | } |
| | | 172 | | |
| | 17 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public override bool IsFault |
| | | 178 | | { |
| | | 179 | | get |
| | | 180 | | { |
| | 1 | 181 | | if (IsDisposed) |
| | | 182 | | { |
| | 0 | 183 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 184 | | } |
| | | 185 | | |
| | 1 | 186 | | return false; |
| | | 187 | | } |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | public override MessageProperties Properties |
| | | 191 | | { |
| | | 192 | | get |
| | | 193 | | { |
| | 33 | 194 | | if (IsDisposed) |
| | | 195 | | { |
| | 0 | 196 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 197 | | } |
| | | 198 | | |
| | 33 | 199 | | return _properties; |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | public override MessageVersion Version |
| | | 204 | | { |
| | | 205 | | get |
| | | 206 | | { |
| | 11 | 207 | | if (IsDisposed) |
| | | 208 | | { |
| | 0 | 209 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 210 | | } |
| | | 211 | | |
| | 11 | 212 | | return MessageVersion.None; |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | protected override void OnBodyToString(XmlDictionaryWriter writer) |
| | | 217 | | { |
| | 0 | 218 | | if (_bodyWriter.IsBuffered) |
| | | 219 | | { |
| | 0 | 220 | | _bodyWriter.WriteBodyContents(writer); |
| | | 221 | | } |
| | | 222 | | else |
| | | 223 | | { |
| | 0 | 224 | | writer.WriteString(SR.MessageBodyIsStream); |
| | | 225 | | } |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | protected override void OnClose() |
| | | 229 | | { |
| | 6 | 230 | | Exception ex = null; |
| | | 231 | | try |
| | | 232 | | { |
| | 6 | 233 | | base.OnClose(); |
| | 6 | 234 | | } |
| | | 235 | | catch (Exception e) |
| | | 236 | | { |
| | 0 | 237 | | if (Fx.IsFatal(e)) |
| | | 238 | | { |
| | 0 | 239 | | throw; |
| | | 240 | | } |
| | 0 | 241 | | ex = e; |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | try |
| | | 245 | | { |
| | 6 | 246 | | if (_properties != null) |
| | | 247 | | { |
| | 6 | 248 | | _properties.Dispose(); |
| | | 249 | | } |
| | 6 | 250 | | } |
| | 0 | 251 | | catch (Exception e) |
| | | 252 | | { |
| | 0 | 253 | | if (Fx.IsFatal(e)) |
| | | 254 | | { |
| | 0 | 255 | | throw; |
| | | 256 | | } |
| | 0 | 257 | | if (ex == null) |
| | | 258 | | { |
| | 0 | 259 | | ex = e; |
| | | 260 | | } |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | try |
| | | 264 | | { |
| | 6 | 265 | | if (_reader != null) |
| | | 266 | | { |
| | 5 | 267 | | _reader.Close(); |
| | | 268 | | } |
| | 6 | 269 | | } |
| | 0 | 270 | | catch (Exception e) |
| | | 271 | | { |
| | 0 | 272 | | if (Fx.IsFatal(e)) |
| | | 273 | | { |
| | 0 | 274 | | throw; |
| | | 275 | | } |
| | 0 | 276 | | if (ex == null) |
| | | 277 | | { |
| | 0 | 278 | | ex = e; |
| | | 279 | | } |
| | 0 | 280 | | } |
| | | 281 | | |
| | 6 | 282 | | if (ex != null) |
| | | 283 | | { |
| | 0 | 284 | | throw Fx.Exception.AsError(ex); |
| | | 285 | | } |
| | | 286 | | |
| | 6 | 287 | | _bodyWriter = null; |
| | 6 | 288 | | } |
| | | 289 | | |
| | | 290 | | protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize) |
| | | 291 | | { |
| | | 292 | | BufferedBodyWriter bufferedBodyWriter; |
| | 2 | 293 | | if (_bodyWriter.IsBuffered) |
| | | 294 | | { |
| | | 295 | | // Can hand this off in buffered case without making a new one. |
| | 1 | 296 | | bufferedBodyWriter = (BufferedBodyWriter)_bodyWriter; |
| | | 297 | | } |
| | | 298 | | else |
| | | 299 | | { |
| | 1 | 300 | | bufferedBodyWriter = (BufferedBodyWriter)_bodyWriter.CreateBufferedCopy(maxBufferSize); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | // Protected by Message state to be called only once. |
| | 2 | 304 | | _bodyWriter = null; |
| | 2 | 305 | | return new ByteStreamMessageBuffer(bufferedBodyWriter.MessageData, _headers, _properties, _reader.Quotas |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | protected override T OnGetBody<T>(XmlDictionaryReader reader) |
| | | 309 | | { |
| | | 310 | | Fx.Assert(reader is XmlByteStreamReader, "reader should be XmlByteStreamReader"); |
| | 9 | 311 | | if (IsDisposed) |
| | | 312 | | { |
| | 0 | 313 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 314 | | } |
| | | 315 | | |
| | 9 | 316 | | Type typeT = typeof(T); |
| | 9 | 317 | | if (typeof(Stream) == typeT) |
| | | 318 | | { |
| | 3 | 319 | | Stream stream = (reader as XmlByteStreamReader).ToStream(); |
| | 3 | 320 | | reader.Close(); |
| | 3 | 321 | | return (T)(object)stream; |
| | | 322 | | } |
| | 6 | 323 | | else if (typeof(byte[]) == typeT) |
| | | 324 | | { |
| | 5 | 325 | | byte[] buffer = (reader as XmlByteStreamReader).ToByteArray(); |
| | 4 | 326 | | reader.Close(); |
| | 4 | 327 | | return (T)(object)buffer; |
| | | 328 | | } |
| | | 329 | | |
| | 1 | 330 | | throw Fx.Exception.AsError( |
| | 1 | 331 | | new NotSupportedException(SR.Format(SR.ByteStreamMessageGetTypeNotSupported, typeT.FullName))); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | protected override XmlDictionaryReader OnGetReaderAtBodyContents() |
| | | 335 | | { |
| | 17 | 336 | | XmlDictionaryReader r = _reader; |
| | 17 | 337 | | _reader = null; |
| | | 338 | | |
| | 17 | 339 | | if ((r != null) && _moveBodyReaderToContent) |
| | | 340 | | { |
| | 15 | 341 | | r.MoveToContent(); |
| | | 342 | | } |
| | | 343 | | |
| | 17 | 344 | | return r; |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 348 | | { |
| | 1 | 349 | | _bodyWriter.WriteBodyContents(writer); |
| | 1 | 350 | | } |
| | | 351 | | |
| | | 352 | | internal class BufferedBodyWriter : BodyWriter |
| | | 353 | | { |
| | | 354 | | public BufferedBodyWriter(ByteStreamBufferedMessageData bufferedMessageData) |
| | 16 | 355 | | : base(true) |
| | | 356 | | { |
| | 16 | 357 | | MessageData = bufferedMessageData; |
| | 16 | 358 | | } |
| | | 359 | | |
| | 2 | 360 | | internal ByteStreamBufferedMessageData MessageData { get; } |
| | | 361 | | |
| | | 362 | | protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) |
| | | 363 | | { |
| | | 364 | | // Never called because when copying a Buffered message, we simply hand off the existing BodyWriter |
| | | 365 | | // to the new message. |
| | | 366 | | Fx.Assert(false, "This is never called"); |
| | 0 | 367 | | return null; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 371 | | { |
| | 0 | 372 | | writer.WriteStartElement(ByteStreamMessageUtility.StreamElementName, string.Empty); |
| | 0 | 373 | | writer.WriteBase64(MessageData.Buffer.Array, MessageData.Buffer.Offset, MessageData.Buffer.Count); |
| | 0 | 374 | | writer.WriteEndElement(); |
| | 0 | 375 | | } |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | internal abstract class StreamedBodyWriter : BodyWriter |
| | | 379 | | { |
| | | 380 | | private StreamedBodyWriter() |
| | 7 | 381 | | : base(false) |
| | | 382 | | { |
| | 7 | 383 | | } |
| | | 384 | | |
| | 7 | 385 | | public static StreamedBodyWriter Create(Stream stream) => new StreamBasedStreamedBodyWriter(stream); |
| | | 386 | | |
| | 0 | 387 | | public static StreamedBodyWriter Create(HttpRequestMessage httpRequestMessage) => new HttpRequestMessage |
| | | 388 | | |
| | 0 | 389 | | public static StreamedBodyWriter Create(HttpResponseMessage httpResponseMessage) => new HttpResponseMess |
| | | 390 | | |
| | | 391 | | // OnCreateBufferedCopy / OnWriteBodyContents can only be called once - protected by state on Message (e |
| | | 392 | | protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) |
| | | 393 | | { |
| | 1 | 394 | | using (BufferManagerOutputStream bufferedStream = new BufferManagerOutputStream(SR.MaxReceivedMessag |
| | | 395 | | { |
| | 1 | 396 | | using (XmlDictionaryWriter writer = new XmlByteStreamWriter(bufferedStream, true)) |
| | | 397 | | { |
| | 1 | 398 | | OnWriteBodyContents(writer); |
| | 1 | 399 | | writer.Flush(); |
| | 1 | 400 | | byte[] bytesArray = bufferedStream.ToArray(out int size); |
| | 1 | 401 | | ByteStreamBufferedMessageData bufferedMessageData = new ByteStreamBufferedMessageData(new Ar |
| | 1 | 402 | | return new BufferedBodyWriter(bufferedMessageData); |
| | | 403 | | } |
| | | 404 | | } |
| | 1 | 405 | | } |
| | | 406 | | |
| | | 407 | | // OnCreateBufferedCopy / OnWriteBodyContents can only be called once - protected by state on Message (e |
| | | 408 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 409 | | { |
| | 2 | 410 | | writer.WriteStartElement(ByteStreamMessageUtility.StreamElementName, string.Empty); |
| | 2 | 411 | | writer.WriteValue(new ByteStreamStreamProvider(GetStream())); |
| | 2 | 412 | | writer.WriteEndElement(); |
| | 2 | 413 | | } |
| | | 414 | | |
| | | 415 | | protected abstract Stream GetStream(); |
| | | 416 | | |
| | | 417 | | internal class ByteStreamStreamProvider : IStreamProvider |
| | | 418 | | { |
| | | 419 | | private readonly Stream _stream; |
| | | 420 | | |
| | 2 | 421 | | internal ByteStreamStreamProvider(Stream stream) |
| | | 422 | | { |
| | 2 | 423 | | _stream = stream; |
| | 2 | 424 | | } |
| | | 425 | | |
| | 2 | 426 | | public Stream GetStream() => _stream; |
| | | 427 | | |
| | | 428 | | public void ReleaseStream(Stream stream) |
| | | 429 | | { |
| | | 430 | | //Noop |
| | 2 | 431 | | } |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | internal class StreamBasedStreamedBodyWriter : StreamedBodyWriter |
| | | 435 | | { |
| | | 436 | | private Stream _stream; |
| | | 437 | | |
| | 7 | 438 | | public StreamBasedStreamedBodyWriter(Stream stream) |
| | | 439 | | { |
| | 7 | 440 | | _stream = stream; |
| | 7 | 441 | | } |
| | | 442 | | |
| | 2 | 443 | | protected override Stream GetStream() => _stream; |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | internal class HttpRequestMessageStreamedBodyWriter : StreamedBodyWriter |
| | | 447 | | { |
| | | 448 | | private readonly HttpRequestMessage _httpRequestMessage; |
| | | 449 | | |
| | 0 | 450 | | public HttpRequestMessageStreamedBodyWriter(HttpRequestMessage httpRequestMessage) |
| | | 451 | | { |
| | | 452 | | Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should not be null."); |
| | | 453 | | |
| | 0 | 454 | | _httpRequestMessage = httpRequestMessage; |
| | 0 | 455 | | } |
| | | 456 | | |
| | | 457 | | protected override Stream GetStream() |
| | | 458 | | { |
| | 0 | 459 | | HttpContent content = _httpRequestMessage.Content; |
| | 0 | 460 | | if (content != null) |
| | | 461 | | { |
| | 0 | 462 | | return content.ReadAsStreamAsync().Result; |
| | | 463 | | } |
| | | 464 | | |
| | 0 | 465 | | return new MemoryStream(Array.Empty<byte>()); |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) |
| | | 469 | | { |
| | 0 | 470 | | HttpContent content = _httpRequestMessage.Content; |
| | 0 | 471 | | if (content != null) |
| | | 472 | | { |
| | 0 | 473 | | content.LoadIntoBufferAsync(maxBufferSize).Wait(); |
| | | 474 | | } |
| | | 475 | | |
| | 0 | 476 | | return base.OnCreateBufferedCopy(maxBufferSize); |
| | | 477 | | } |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | internal class HttpResponseMessageStreamedBodyWriter : StreamedBodyWriter |
| | | 481 | | { |
| | | 482 | | private readonly HttpResponseMessage _httpResponseMessage; |
| | | 483 | | |
| | 0 | 484 | | public HttpResponseMessageStreamedBodyWriter(HttpResponseMessage httpResponseMessage) |
| | | 485 | | { |
| | | 486 | | Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should not be null." |
| | | 487 | | |
| | 0 | 488 | | _httpResponseMessage = httpResponseMessage; |
| | 0 | 489 | | } |
| | | 490 | | |
| | | 491 | | protected override Stream GetStream() |
| | | 492 | | { |
| | 0 | 493 | | HttpContent content = _httpResponseMessage.Content; |
| | 0 | 494 | | if (content != null) |
| | | 495 | | { |
| | 0 | 496 | | return content.ReadAsStreamAsync().Result; |
| | | 497 | | } |
| | | 498 | | |
| | 0 | 499 | | return new MemoryStream(Array.Empty<byte>()); |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) |
| | | 503 | | { |
| | 0 | 504 | | HttpContent content = _httpResponseMessage.Content; |
| | 0 | 505 | | if (content != null) |
| | | 506 | | { |
| | 0 | 507 | | content.LoadIntoBufferAsync(maxBufferSize).Wait(); |
| | | 508 | | } |
| | | 509 | | |
| | 0 | 510 | | return base.OnCreateBufferedCopy(maxBufferSize); |
| | | 511 | | } |
| | | 512 | | } |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | internal class ByteStreamMessageBuffer : MessageBuffer |
| | | 516 | | { |
| | | 517 | | private bool _closed; |
| | | 518 | | private MessageHeaders _headers; |
| | | 519 | | private ByteStreamBufferedMessageData _messageData; |
| | | 520 | | private MessageProperties _properties; |
| | | 521 | | private XmlDictionaryReaderQuotas _quotas; |
| | | 522 | | private bool _moveBodyReaderToContent; |
| | | 523 | | |
| | | 524 | | public ByteStreamMessageBuffer(ByteStreamBufferedMessageData messageData, MessageHeaders headers, Messag |
| | 2 | 525 | | : base() |
| | | 526 | | { |
| | 2 | 527 | | _messageData = messageData; |
| | 2 | 528 | | _headers = new MessageHeaders(headers); |
| | 2 | 529 | | _properties = new MessageProperties(properties); |
| | 2 | 530 | | _quotas = new XmlDictionaryReaderQuotas(); |
| | 2 | 531 | | quotas.CopyTo(_quotas); |
| | 2 | 532 | | _moveBodyReaderToContent = moveBodyReaderToContent; |
| | | 533 | | |
| | 2 | 534 | | _messageData.Open(); |
| | 2 | 535 | | } |
| | | 536 | | |
| | 0 | 537 | | public override int BufferSize => _messageData.Buffer.Count; |
| | | 538 | | |
| | 8 | 539 | | private object ThisLock { get; } = new object(); |
| | | 540 | | |
| | | 541 | | public override void Close() |
| | | 542 | | { |
| | 2 | 543 | | lock (ThisLock) |
| | | 544 | | { |
| | 2 | 545 | | if (!_closed) |
| | | 546 | | { |
| | 2 | 547 | | _closed = true; |
| | 2 | 548 | | _headers = null; |
| | | 549 | | |
| | 2 | 550 | | if (_properties != null) |
| | | 551 | | { |
| | 2 | 552 | | _properties.Dispose(); |
| | 2 | 553 | | _properties = null; |
| | | 554 | | } |
| | | 555 | | |
| | 2 | 556 | | _messageData.Close(); |
| | 2 | 557 | | _messageData = null; |
| | 2 | 558 | | _quotas = null; |
| | | 559 | | } |
| | 2 | 560 | | } |
| | 2 | 561 | | } |
| | | 562 | | |
| | | 563 | | public override Message CreateMessage() |
| | | 564 | | { |
| | 4 | 565 | | lock (ThisLock) |
| | | 566 | | { |
| | 4 | 567 | | if (_closed) |
| | | 568 | | { |
| | 0 | 569 | | throw Fx.Exception.ObjectDisposed(SR.Format(SR.ObjectDisposed, "message")); |
| | | 570 | | } |
| | | 571 | | |
| | 4 | 572 | | return new InternalByteStreamMessage(_messageData, _headers, _properties, _quotas, _moveBodyRead |
| | | 573 | | } |
| | 4 | 574 | | } |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | } |
| | | 578 | | } |