| | | 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.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http |
| | | 10 | | { |
| | | 11 | | internal sealed class HttpConnectionResponseContent : HttpContent |
| | | 12 | | { |
| | | 13 | | private Stream? _stream; |
| | | 14 | | private bool _consumedStream; // separate from _stream so that Dispose can drain _stream |
| | | 15 | | |
| | | 16 | | public void SetStream(Stream stream) |
| | 0 | 17 | | { |
| | 0 | 18 | | Debug.Assert(stream != null); |
| | 0 | 19 | | Debug.Assert(stream.CanRead); |
| | 0 | 20 | | Debug.Assert(!_consumedStream); |
| | | 21 | | |
| | 0 | 22 | | _stream = stream; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | private Stream ConsumeStream() |
| | 0 | 26 | | { |
| | 0 | 27 | | if (_consumedStream || _stream == null) |
| | 0 | 28 | | { |
| | 0 | 29 | | throw new InvalidOperationException(SR.net_http_content_stream_already_read); |
| | | 30 | | } |
| | 0 | 31 | | _consumedStream = true; |
| | | 32 | | |
| | 0 | 33 | | return _stream; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | protected override void SerializeToStream(Stream stream, TransportContext? context, |
| | | 37 | | CancellationToken cancellationToken) |
| | 0 | 38 | | { |
| | 0 | 39 | | ArgumentNullException.ThrowIfNull(stream); |
| | | 40 | | |
| | 0 | 41 | | using (Stream contentStream = ConsumeStream()) |
| | 0 | 42 | | { |
| | | 43 | | const int BufferSize = 8192; |
| | 0 | 44 | | contentStream.CopyTo(stream, BufferSize); |
| | 0 | 45 | | } |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | protected sealed override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => |
| | 0 | 49 | | SerializeToStreamAsync(stream, context, CancellationToken.None); |
| | | 50 | | |
| | | 51 | | protected sealed override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToke |
| | 0 | 52 | | { |
| | 0 | 53 | | ArgumentNullException.ThrowIfNull(stream); |
| | 0 | 54 | | return Impl(stream, cancellationToken); |
| | | 55 | | |
| | | 56 | | async Task Impl(Stream stream, CancellationToken cancellationToken) |
| | 0 | 57 | | { |
| | 0 | 58 | | using (Stream contentStream = ConsumeStream()) |
| | 0 | 59 | | { |
| | | 60 | | const int BufferSize = 8192; |
| | 0 | 61 | | await contentStream.CopyToAsync(stream, BufferSize, cancellationToken).ConfigureAwait(false); |
| | 0 | 62 | | } |
| | 0 | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | protected internal sealed override bool TryComputeLength(out long length) |
| | 0 | 67 | | { |
| | 0 | 68 | | length = 0; |
| | 0 | 69 | | return false; |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | protected sealed override Stream CreateContentReadStream(CancellationToken cancellationToken) => |
| | 0 | 73 | | ConsumeStream(); |
| | | 74 | | |
| | | 75 | | protected sealed override Task<Stream> CreateContentReadStreamAsync() => |
| | 0 | 76 | | Task.FromResult<Stream>(ConsumeStream()); |
| | | 77 | | |
| | | 78 | | internal sealed override Stream TryCreateContentReadStream() => |
| | 0 | 79 | | ConsumeStream(); |
| | | 80 | | |
| | 0 | 81 | | internal override bool AllowDuplex => false; |
| | | 82 | | |
| | | 83 | | protected sealed override void Dispose(bool disposing) |
| | 0 | 84 | | { |
| | 0 | 85 | | if (disposing) |
| | 0 | 86 | | { |
| | 0 | 87 | | if (_stream != null) |
| | 0 | 88 | | { |
| | 0 | 89 | | _stream.Dispose(); |
| | 0 | 90 | | _stream = null; |
| | 0 | 91 | | } |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | base.Dispose(disposing); |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |