|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Headers; |
| 3 | + |
| 4 | +namespace Chats.BE.Services.RequestTracing; |
| 5 | + |
| 6 | +internal sealed class ObservedRequestHttpContent : HttpContent |
| 7 | +{ |
| 8 | + private readonly HttpContent _inner; |
| 9 | + private readonly int _maxCaptureBytes; |
| 10 | + private readonly Action<int, byte[], bool> _onCompleted; |
| 11 | + private int _completedFlag; |
| 12 | + |
| 13 | + public ObservedRequestHttpContent(HttpContent inner, int maxCaptureBytes, Action<int, byte[], bool> onCompleted) |
| 14 | + { |
| 15 | + _inner = inner; |
| 16 | + _maxCaptureBytes = maxCaptureBytes; |
| 17 | + _onCompleted = onCompleted; |
| 18 | + CopyHeaders(_inner.Headers, Headers); |
| 19 | + } |
| 20 | + |
| 21 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context) |
| 22 | + { |
| 23 | + await SerializeToStreamAsync(stream, context, CancellationToken.None); |
| 24 | + } |
| 25 | + |
| 26 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) |
| 27 | + { |
| 28 | + using WriteCaptureStream captureStream = new(stream, _maxCaptureBytes); |
| 29 | + try |
| 30 | + { |
| 31 | + await _inner.CopyToAsync(captureStream, context, cancellationToken); |
| 32 | + CompleteOnce(captureStream.TotalBytesWritten, captureStream.CapturedBytes, captureStream.IsTruncated); |
| 33 | + } |
| 34 | + catch |
| 35 | + { |
| 36 | + CompleteOnce(captureStream.TotalBytesWritten, captureStream.CapturedBytes, true); |
| 37 | + throw; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + protected override bool TryComputeLength(out long length) |
| 42 | + { |
| 43 | + if (_inner.Headers.ContentLength.HasValue) |
| 44 | + { |
| 45 | + length = _inner.Headers.ContentLength.Value; |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + length = 0; |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + protected override void Dispose(bool disposing) |
| 54 | + { |
| 55 | + if (disposing) |
| 56 | + { |
| 57 | + _inner.Dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + base.Dispose(disposing); |
| 61 | + } |
| 62 | + |
| 63 | + private void CompleteOnce(int totalBytes, byte[] capturedBytes, bool truncated) |
| 64 | + { |
| 65 | + if (Interlocked.Exchange(ref _completedFlag, 1) != 0) |
| 66 | + { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + _onCompleted(totalBytes, capturedBytes, truncated); |
| 71 | + } |
| 72 | + |
| 73 | + protected override Task<Stream> CreateContentReadStreamAsync() |
| 74 | + { |
| 75 | + return _inner.ReadAsStreamAsync(); |
| 76 | + } |
| 77 | + |
| 78 | + protected override Stream CreateContentReadStream(CancellationToken cancellationToken) |
| 79 | + { |
| 80 | + return _inner.ReadAsStream(cancellationToken); |
| 81 | + } |
| 82 | + |
| 83 | + private static void CopyHeaders(HttpContentHeaders source, HttpContentHeaders target) |
| 84 | + { |
| 85 | + foreach (KeyValuePair<string, IEnumerable<string>> header in source) |
| 86 | + { |
| 87 | + target.TryAddWithoutValidation(header.Key, header.Value); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +internal sealed class ObservedResponseHttpContent : HttpContent |
| 93 | +{ |
| 94 | + private readonly HttpContent _inner; |
| 95 | + private readonly int _maxCaptureBytes; |
| 96 | + private readonly Action<int, byte[], bool> _onCompleted; |
| 97 | + private int _completedFlag; |
| 98 | + |
| 99 | + public ObservedResponseHttpContent(HttpContent inner, int maxCaptureBytes, Action<int, byte[], bool> onCompleted) |
| 100 | + { |
| 101 | + _inner = inner; |
| 102 | + _maxCaptureBytes = maxCaptureBytes; |
| 103 | + _onCompleted = onCompleted; |
| 104 | + CopyHeaders(_inner.Headers, Headers); |
| 105 | + } |
| 106 | + |
| 107 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context) |
| 108 | + { |
| 109 | + await SerializeToStreamAsync(stream, context, CancellationToken.None); |
| 110 | + } |
| 111 | + |
| 112 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) |
| 113 | + { |
| 114 | + await using Stream source = await _inner.ReadAsStreamAsync(cancellationToken); |
| 115 | + using ReadCaptureStream captureStream = new( |
| 116 | + source, |
| 117 | + _maxCaptureBytes, |
| 118 | + CompleteOnce); |
| 119 | + |
| 120 | + byte[] buffer = new byte[81920]; |
| 121 | + while (true) |
| 122 | + { |
| 123 | + int read = await captureStream.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken); |
| 124 | + if (read <= 0) |
| 125 | + { |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + await stream.WriteAsync(buffer.AsMemory(0, read), cancellationToken); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + protected override bool TryComputeLength(out long length) |
| 134 | + { |
| 135 | + if (_inner.Headers.ContentLength.HasValue) |
| 136 | + { |
| 137 | + length = _inner.Headers.ContentLength.Value; |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + length = 0; |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + protected override void Dispose(bool disposing) |
| 146 | + { |
| 147 | + if (disposing) |
| 148 | + { |
| 149 | + _inner.Dispose(); |
| 150 | + } |
| 151 | + |
| 152 | + base.Dispose(disposing); |
| 153 | + } |
| 154 | + |
| 155 | + protected override async Task<Stream> CreateContentReadStreamAsync() |
| 156 | + { |
| 157 | + Stream source = await _inner.ReadAsStreamAsync(); |
| 158 | + return new ReadCaptureStream(source, _maxCaptureBytes, CompleteOnce); |
| 159 | + } |
| 160 | + |
| 161 | + protected override Stream CreateContentReadStream(CancellationToken cancellationToken) |
| 162 | + { |
| 163 | + Stream source = _inner.ReadAsStream(cancellationToken); |
| 164 | + return new ReadCaptureStream(source, _maxCaptureBytes, CompleteOnce); |
| 165 | + } |
| 166 | + |
| 167 | + private void CompleteOnce(int totalBytes, byte[] capturedBytes, bool truncated) |
| 168 | + { |
| 169 | + if (Interlocked.Exchange(ref _completedFlag, 1) != 0) |
| 170 | + { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + _onCompleted(totalBytes, capturedBytes, truncated); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + private static void CopyHeaders(HttpContentHeaders source, HttpContentHeaders target) |
| 179 | + { |
| 180 | + foreach (KeyValuePair<string, IEnumerable<string>> header in source) |
| 181 | + { |
| 182 | + target.TryAddWithoutValidation(header.Key, header.Value); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments