| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Azure.Core.Buffers; |
| | | 10 | | |
| | | 11 | | namespace Azure.Core.Pipeline |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Pipeline policy to buffer response content or add a timeout to response content managed by the client |
| | | 15 | | /// </summary> |
| | | 16 | | internal class ResponseBodyPolicy : HttpPipelinePolicy |
| | | 17 | | { |
| | | 18 | | // Same value as Stream.CopyTo uses by default |
| | | 19 | | private const int DefaultCopyBufferSize = 81920; |
| | | 20 | | |
| | | 21 | | private readonly TimeSpan _networkTimeout; |
| | | 22 | | |
| | 64 | 23 | | public ResponseBodyPolicy(TimeSpan networkTimeout) |
| | | 24 | | { |
| | 64 | 25 | | _networkTimeout = networkTimeout; |
| | 64 | 26 | | } |
| | | 27 | | |
| | | 28 | | public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) |
| | | 29 | | { |
| | 1052 | 30 | | await ProcessAsync(message, pipeline, true).ConfigureAwait(false); |
| | 1040 | 31 | | } |
| | | 32 | | |
| | | 33 | | public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) |
| | | 34 | | { |
| | 1034 | 35 | | ProcessAsync(message, pipeline, false).EnsureCompleted(); |
| | 1022 | 36 | | } |
| | | 37 | | |
| | | 38 | | private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool asyn |
| | | 39 | | { |
| | 2086 | 40 | | CancellationToken oldToken = message.CancellationToken; |
| | 2086 | 41 | | using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(oldToken); |
| | | 42 | | |
| | 2086 | 43 | | cts.CancelAfter(_networkTimeout); |
| | | 44 | | try |
| | | 45 | | { |
| | 2086 | 46 | | message.CancellationToken = cts.Token; |
| | 2086 | 47 | | if (async) |
| | | 48 | | { |
| | 1052 | 49 | | await ProcessNextAsync(message, pipeline).ConfigureAwait(false); |
| | | 50 | | } |
| | | 51 | | else |
| | | 52 | | { |
| | 1034 | 53 | | ProcessNext(message, pipeline); |
| | | 54 | | } |
| | 2074 | 55 | | } |
| | | 56 | | finally |
| | | 57 | | { |
| | 2086 | 58 | | message.CancellationToken = oldToken; |
| | 2086 | 59 | | cts.CancelAfter(Timeout.Infinite); |
| | | 60 | | } |
| | | 61 | | |
| | 2074 | 62 | | Stream? responseContentStream = message.Response.ContentStream; |
| | 2074 | 63 | | if (responseContentStream == null || responseContentStream.CanSeek) |
| | | 64 | | { |
| | 22 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | 2052 | 68 | | if (message.BufferResponse) |
| | | 69 | | { |
| | 428 | 70 | | if (_networkTimeout != Timeout.InfiniteTimeSpan) |
| | | 71 | | { |
| | 416 | 72 | | cts.Token.Register(state => ((Stream)state)?.Dispose(), responseContentStream); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | try |
| | | 76 | | { |
| | 428 | 77 | | var bufferedStream = new MemoryStream(); |
| | 428 | 78 | | if (async) |
| | | 79 | | { |
| | 214 | 80 | | await CopyToAsync(responseContentStream, bufferedStream, cts).ConfigureAwait(false); |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | 214 | 84 | | CopyTo(responseContentStream, bufferedStream, cts); |
| | | 85 | | } |
| | | 86 | | |
| | 416 | 87 | | responseContentStream.Dispose(); |
| | 416 | 88 | | bufferedStream.Position = 0; |
| | 416 | 89 | | message.Response.ContentStream = bufferedStream; |
| | 416 | 90 | | } |
| | | 91 | | // We dispose stream on timeout so catch and check if cancellation token was cancelled |
| | 0 | 92 | | catch (ObjectDisposedException) |
| | | 93 | | { |
| | 0 | 94 | | cts.Token.ThrowIfCancellationRequested(); |
| | 0 | 95 | | throw; |
| | | 96 | | } |
| | | 97 | | } |
| | 1624 | 98 | | else if (_networkTimeout != Timeout.InfiniteTimeSpan) |
| | | 99 | | { |
| | 1620 | 100 | | message.Response.ContentStream = new ReadTimeoutStream(responseContentStream, _networkTimeout); |
| | | 101 | | } |
| | 2062 | 102 | | } |
| | | 103 | | |
| | | 104 | | private async Task CopyToAsync(Stream source, Stream destination, CancellationTokenSource cancellationTokenSourc |
| | | 105 | | { |
| | 214 | 106 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(DefaultCopyBufferSize); |
| | | 107 | | try |
| | | 108 | | { |
| | 2474 | 109 | | while (true) |
| | | 110 | | { |
| | 2688 | 111 | | cancellationTokenSource.CancelAfter(_networkTimeout); |
| | 2688 | 112 | | int bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationTokenSource.Token).Conf |
| | 2682 | 113 | | if (bytesRead == 0) break; |
| | 2474 | 114 | | await destination.WriteAsync(new ReadOnlyMemory<byte>(buffer, 0, bytesRead), cancellationTokenSource |
| | | 115 | | } |
| | 208 | 116 | | } |
| | | 117 | | finally |
| | | 118 | | { |
| | 214 | 119 | | cancellationTokenSource.CancelAfter(Timeout.InfiniteTimeSpan); |
| | 214 | 120 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 121 | | } |
| | 208 | 122 | | } |
| | | 123 | | |
| | | 124 | | private void CopyTo(Stream source, Stream destination, CancellationTokenSource cancellationTokenSource) |
| | | 125 | | { |
| | 214 | 126 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(DefaultCopyBufferSize); |
| | | 127 | | try |
| | | 128 | | { |
| | | 129 | | int read; |
| | 1054 | 130 | | while ((read = source.Read(buffer, 0, buffer.Length)) != 0) |
| | | 131 | | { |
| | 842 | 132 | | cancellationTokenSource.Token.ThrowIfCancellationRequested(); |
| | 840 | 133 | | cancellationTokenSource.CancelAfter(_networkTimeout); |
| | 840 | 134 | | destination.Write(buffer, 0, read); |
| | | 135 | | } |
| | 208 | 136 | | } |
| | | 137 | | finally |
| | | 138 | | { |
| | 214 | 139 | | cancellationTokenSource.CancelAfter(Timeout.InfiniteTimeSpan); |
| | 214 | 140 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 214 | 141 | | } |
| | 208 | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |