| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.IO.Compression; |
| | | 8 | | using System.Net.Http.Headers; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace System.Net.Http |
| | | 13 | | { |
| | | 14 | | internal sealed class DecompressionHandler : HttpMessageHandlerStage |
| | | 15 | | { |
| | | 16 | | private readonly HttpMessageHandlerStage _innerHandler; |
| | | 17 | | private readonly DecompressionMethods _decompressionMethods; |
| | | 18 | | |
| | | 19 | | private const string Gzip = "gzip"; |
| | | 20 | | private const string Deflate = "deflate"; |
| | | 21 | | private const string Brotli = "br"; |
| | | 22 | | private const string Zstd = "zstd"; |
| | 0 | 23 | | private static readonly StringWithQualityHeaderValue s_gzipHeaderValue = new(Gzip); |
| | 0 | 24 | | private static readonly StringWithQualityHeaderValue s_deflateHeaderValue = new(Deflate); |
| | 0 | 25 | | private static readonly StringWithQualityHeaderValue s_brotliHeaderValue = new(Brotli); |
| | 0 | 26 | | private static readonly StringWithQualityHeaderValue s_zstdHeaderValue = new(Zstd); |
| | | 27 | | |
| | | 28 | | /// <summary>Header value for all enabled decompression methods, e.g. "gzip, deflate".</summary> |
| | | 29 | | private readonly string _acceptEncodingHeaderValue; |
| | | 30 | | |
| | 0 | 31 | | public DecompressionHandler(DecompressionMethods decompressionMethods, HttpMessageHandlerStage innerHandler) |
| | 0 | 32 | | { |
| | 0 | 33 | | Debug.Assert(decompressionMethods != DecompressionMethods.None); |
| | 0 | 34 | | Debug.Assert(innerHandler != null); |
| | | 35 | | |
| | 0 | 36 | | _decompressionMethods = decompressionMethods; |
| | 0 | 37 | | _innerHandler = innerHandler; |
| | | 38 | | |
| | 0 | 39 | | Span<string?> methods = [null, null, null, null]; |
| | 0 | 40 | | int count = 0; |
| | 0 | 41 | | if (GZipEnabled) methods[count++] = Gzip; |
| | 0 | 42 | | if (DeflateEnabled) methods[count++] = Deflate; |
| | 0 | 43 | | if (BrotliEnabled) methods[count++] = Brotli; |
| | 0 | 44 | | if (ZstandardEnabled) methods[count++] = Zstd; |
| | 0 | 45 | | _acceptEncodingHeaderValue = string.Join(", ", methods.Slice(0, count)); |
| | 0 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | internal bool GZipEnabled => (_decompressionMethods & DecompressionMethods.GZip) != 0; |
| | 0 | 49 | | internal bool DeflateEnabled => (_decompressionMethods & DecompressionMethods.Deflate) != 0; |
| | 0 | 50 | | internal bool BrotliEnabled => (_decompressionMethods & DecompressionMethods.Brotli) != 0; |
| | 0 | 51 | | internal bool ZstandardEnabled => (_decompressionMethods & DecompressionMethods.Zstandard) != 0; |
| | | 52 | | |
| | | 53 | | private static bool EncodingExists(HttpHeaderValueCollection<StringWithQualityHeaderValue> acceptEncodingHeader, |
| | 0 | 54 | | { |
| | 0 | 55 | | foreach (StringWithQualityHeaderValue existingEncoding in acceptEncodingHeader) |
| | 0 | 56 | | { |
| | 0 | 57 | | if (string.Equals(existingEncoding.Value, encoding, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 58 | | { |
| | 0 | 59 | | return true; |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return false; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | internal override async ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, Cancell |
| | 0 | 67 | | { |
| | 0 | 68 | | if (!request.Headers.Contains(KnownHeaders.AcceptEncoding.Descriptor)) |
| | 0 | 69 | | { |
| | | 70 | | // Very common case: no Accept-Encoding header yet, so just add one with all supported encodings. |
| | 0 | 71 | | request.Headers.TryAddWithoutValidation(KnownHeaders.AcceptEncoding.Descriptor, _acceptEncodingHeaderVal |
| | 0 | 72 | | } |
| | | 73 | | else |
| | 0 | 74 | | { |
| | 0 | 75 | | HttpHeaderValueCollection<StringWithQualityHeaderValue> acceptEncoding = request.Headers.AcceptEncoding; |
| | | 76 | | |
| | 0 | 77 | | if (GZipEnabled && !EncodingExists(acceptEncoding, Gzip)) |
| | 0 | 78 | | { |
| | 0 | 79 | | acceptEncoding.Add(s_gzipHeaderValue); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | if (DeflateEnabled && !EncodingExists(acceptEncoding, Deflate)) |
| | 0 | 83 | | { |
| | 0 | 84 | | acceptEncoding.Add(s_deflateHeaderValue); |
| | 0 | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | if (BrotliEnabled && !EncodingExists(acceptEncoding, Brotli)) |
| | 0 | 88 | | { |
| | 0 | 89 | | acceptEncoding.Add(s_brotliHeaderValue); |
| | 0 | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (ZstandardEnabled && !EncodingExists(acceptEncoding, Zstd)) |
| | 0 | 93 | | { |
| | 0 | 94 | | acceptEncoding.Add(s_zstdHeaderValue); |
| | 0 | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | HttpResponseMessage response = await _innerHandler.SendAsync(request, async, cancellationToken).ConfigureAwa |
| | | 99 | | |
| | 0 | 100 | | Debug.Assert(response.Content != null); |
| | 0 | 101 | | if (response.Content.Headers.TryGetValues(KnownHeaders.ContentEncoding.Descriptor, out IEnumerable<string>? |
| | 0 | 102 | | { |
| | 0 | 103 | | Debug.Assert(contentEncodings is string[] { Length: > 0 }); |
| | | 104 | | |
| | 0 | 105 | | string[] encodings = (string[])contentEncodings; |
| | 0 | 106 | | string? last = encodings[^1]; |
| | | 107 | | |
| | 0 | 108 | | if (GZipEnabled && string.Equals(last, Gzip, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 109 | | { |
| | 0 | 110 | | response.Content = new GZipDecompressedContent(response.Content, encodings); |
| | 0 | 111 | | } |
| | 0 | 112 | | else if (DeflateEnabled && string.Equals(last, Deflate, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 113 | | { |
| | 0 | 114 | | response.Content = new DeflateDecompressedContent(response.Content, encodings); |
| | 0 | 115 | | } |
| | 0 | 116 | | else if (BrotliEnabled && string.Equals(last, Brotli, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 117 | | { |
| | 0 | 118 | | response.Content = new BrotliDecompressedContent(response.Content, encodings); |
| | 0 | 119 | | } |
| | 0 | 120 | | else if (ZstandardEnabled && string.Equals(last, Zstd, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 121 | | { |
| | 0 | 122 | | response.Content = new ZstandardDecompressedContent(response.Content, encodings); |
| | 0 | 123 | | } |
| | 0 | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | return response; |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | protected override void Dispose(bool disposing) |
| | 0 | 130 | | { |
| | 0 | 131 | | if (disposing) |
| | 0 | 132 | | { |
| | 0 | 133 | | _innerHandler.Dispose(); |
| | 0 | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | base.Dispose(disposing); |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | private abstract class DecompressedContent : HttpContent |
| | | 140 | | { |
| | | 141 | | private readonly HttpContent _originalContent; |
| | | 142 | | private bool _contentConsumed; |
| | | 143 | | |
| | 0 | 144 | | public DecompressedContent(HttpContent originalContent, string[] contentEncodings) |
| | 0 | 145 | | { |
| | 0 | 146 | | _originalContent = originalContent; |
| | 0 | 147 | | _contentConsumed = false; |
| | | 148 | | |
| | | 149 | | // Copy original response headers, but with the following changes: |
| | | 150 | | // Content-Length is removed, since it no longer applies to the decompressed content |
| | | 151 | | // The last Content-Encoding is removed, since we are processing that here. |
| | 0 | 152 | | SetHeaders(originalContent.Headers); |
| | 0 | 153 | | Headers.ContentLength = null; |
| | 0 | 154 | | Headers.Remove(KnownHeaders.ContentEncoding.Descriptor); |
| | | 155 | | |
| | 0 | 156 | | if (contentEncodings.Length > 1) |
| | 0 | 157 | | { |
| | 0 | 158 | | Headers.TryAddWithoutValidation(KnownHeaders.ContentEncoding.Descriptor, contentEncodings[..^1]); |
| | 0 | 159 | | } |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | protected abstract Stream GetDecompressedStream(Stream originalStream); |
| | | 163 | | |
| | | 164 | | protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancel |
| | 0 | 165 | | { |
| | 0 | 166 | | using Stream decompressedStream = CreateContentReadStream(cancellationToken); |
| | 0 | 167 | | decompressedStream.CopyTo(stream); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => |
| | 0 | 171 | | SerializeToStreamAsync(stream, context, CancellationToken.None); |
| | | 172 | | |
| | | 173 | | protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationT |
| | 0 | 174 | | { |
| | 0 | 175 | | using (Stream decompressedStream = TryCreateContentReadStream() ?? await CreateContentReadStreamAsync(ca |
| | 0 | 176 | | { |
| | 0 | 177 | | await decompressedStream.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); |
| | 0 | 178 | | } |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | protected override Stream CreateContentReadStream(CancellationToken cancellationToken) |
| | 0 | 182 | | { |
| | 0 | 183 | | ValueTask<Stream> task = CreateContentReadStreamAsyncCore(async: false, cancellationToken); |
| | 0 | 184 | | Debug.Assert(task.IsCompleted); |
| | 0 | 185 | | return task.GetAwaiter().GetResult(); |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | protected override Task<Stream> CreateContentReadStreamAsync(CancellationToken cancellationToken) => |
| | 0 | 189 | | CreateContentReadStreamAsyncCore(async: true, cancellationToken).AsTask(); |
| | | 190 | | |
| | | 191 | | private async ValueTask<Stream> CreateContentReadStreamAsyncCore(bool async, CancellationToken cancellationT |
| | 0 | 192 | | { |
| | 0 | 193 | | if (_contentConsumed) |
| | 0 | 194 | | { |
| | 0 | 195 | | throw new InvalidOperationException(SR.net_http_content_stream_already_read); |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | _contentConsumed = true; |
| | | 199 | | |
| | | 200 | | Stream originalStream; |
| | 0 | 201 | | if (async) |
| | 0 | 202 | | { |
| | 0 | 203 | | originalStream = _originalContent.TryReadAsStream() ?? await _originalContent.ReadAsStreamAsync(canc |
| | 0 | 204 | | } |
| | | 205 | | else |
| | 0 | 206 | | { |
| | 0 | 207 | | originalStream = _originalContent.ReadAsStream(cancellationToken); |
| | 0 | 208 | | } |
| | 0 | 209 | | return GetDecompressedStream(originalStream); |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | internal override Stream? TryCreateContentReadStream() |
| | 0 | 213 | | { |
| | 0 | 214 | | Stream? originalStream = _originalContent.TryReadAsStream(); |
| | 0 | 215 | | return originalStream is null ? null : GetDecompressedStream(originalStream); |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | protected internal override bool TryComputeLength(out long length) |
| | 0 | 219 | | { |
| | 0 | 220 | | length = 0; |
| | 0 | 221 | | return false; |
| | 0 | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | internal override bool AllowDuplex => false; |
| | | 225 | | |
| | | 226 | | protected override void Dispose(bool disposing) |
| | 0 | 227 | | { |
| | 0 | 228 | | if (disposing) |
| | 0 | 229 | | { |
| | 0 | 230 | | _originalContent.Dispose(); |
| | 0 | 231 | | } |
| | 0 | 232 | | base.Dispose(disposing); |
| | 0 | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | private sealed class GZipDecompressedContent(HttpContent originalContent, string[] contentEncodings) : Decompres |
| | | 237 | | { |
| | | 238 | | protected override Stream GetDecompressedStream(Stream originalStream) => |
| | 0 | 239 | | new GZipStream(originalStream, CompressionMode.Decompress); |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | private sealed class DeflateDecompressedContent(HttpContent originalContent, string[] contentEncodings) : Decomp |
| | | 243 | | { |
| | | 244 | | protected override Stream GetDecompressedStream(Stream originalStream) => |
| | 0 | 245 | | new ZLibOrDeflateStream(originalStream); |
| | | 246 | | |
| | | 247 | | /// <summary>Stream that wraps either <see cref="ZLibStream"/> or <see cref="DeflateStream"/> for decompress |
| | | 248 | | private sealed class ZLibOrDeflateStream : HttpBaseStream |
| | | 249 | | { |
| | | 250 | | // As described in RFC 2616, the deflate content-coding is the "zlib" format (RFC 1950) in combination w |
| | | 251 | | // the "deflate" compression algorithm (RFC 1951). Thus, the right stream to use here is ZLibStream. Ho |
| | | 252 | | // some servers incorrectly interpret "deflate" to mean the raw, unwrapped deflate protocol. To account |
| | | 253 | | // that, this switches between using ZLibStream (correct) and DeflateStream (incorrect) in order to maxi |
| | | 254 | | // compatibility with servers. |
| | | 255 | | |
| | | 256 | | private readonly PeekFirstByteReadStream _stream; |
| | | 257 | | private Stream? _decompressionStream; |
| | | 258 | | |
| | 0 | 259 | | public ZLibOrDeflateStream(Stream stream) => _stream = new PeekFirstByteReadStream(stream); |
| | | 260 | | |
| | | 261 | | protected override void Dispose(bool disposing) |
| | 0 | 262 | | { |
| | 0 | 263 | | if (disposing) |
| | 0 | 264 | | { |
| | 0 | 265 | | _decompressionStream?.Dispose(); |
| | 0 | 266 | | _stream.Dispose(); |
| | 0 | 267 | | } |
| | 0 | 268 | | base.Dispose(disposing); |
| | 0 | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | public override bool CanRead => true; |
| | 0 | 272 | | public override bool CanWrite => false; |
| | 0 | 273 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken) = |
| | | 274 | | |
| | | 275 | | // On the first read request, peek at the first nibble of the response. If it's an 8, use ZLibStream, ot |
| | | 276 | | // use DeflateStream. This heuristic works because we're deciding only between raw deflate and zlib wrap |
| | | 277 | | // deflate, in which case the first nibble will always be 8 for zlib and never be 8 for deflate. |
| | | 278 | | // https://stackoverflow.com/a/37528114 provides an explanation for why. |
| | | 279 | | |
| | | 280 | | public override int Read(Span<byte> buffer) |
| | 0 | 281 | | { |
| | 0 | 282 | | if (_decompressionStream is null) |
| | 0 | 283 | | { |
| | 0 | 284 | | int firstByte = _stream.PeekFirstByte(); |
| | 0 | 285 | | _decompressionStream = CreateDecompressionStream(firstByte, _stream); |
| | 0 | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | return _decompressionStream.Read(buffer); |
| | 0 | 289 | | } |
| | | 290 | | |
| | | 291 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) |
| | 0 | 292 | | { |
| | 0 | 293 | | if (_decompressionStream is null) |
| | 0 | 294 | | { |
| | 0 | 295 | | return CreateAndReadAsync(this, buffer, cancellationToken); |
| | | 296 | | |
| | | 297 | | static async ValueTask<int> CreateAndReadAsync(ZLibOrDeflateStream thisRef, Memory<byte> buffer, |
| | 0 | 298 | | { |
| | 0 | 299 | | int firstByte = await thisRef._stream.PeekFirstByteAsync(cancellationToken).ConfigureAwait(f |
| | 0 | 300 | | thisRef._decompressionStream = CreateDecompressionStream(firstByte, thisRef._stream); |
| | 0 | 301 | | return await thisRef._decompressionStream.ReadAsync(buffer, cancellationToken).ConfigureAwai |
| | 0 | 302 | | } |
| | | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | return _decompressionStream.ReadAsync(buffer, cancellationToken); |
| | 0 | 306 | | } |
| | | 307 | | |
| | | 308 | | public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken |
| | 0 | 309 | | { |
| | 0 | 310 | | ValidateCopyToArguments(destination, bufferSize); |
| | 0 | 311 | | return Core(destination, bufferSize, cancellationToken); |
| | | 312 | | async Task Core(Stream destination, int bufferSize, CancellationToken cancellationToken) |
| | 0 | 313 | | { |
| | 0 | 314 | | if (_decompressionStream is null) |
| | 0 | 315 | | { |
| | 0 | 316 | | int firstByte = await _stream.PeekFirstByteAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 317 | | _decompressionStream = CreateDecompressionStream(firstByte, _stream); |
| | 0 | 318 | | } |
| | | 319 | | |
| | 0 | 320 | | await _decompressionStream.CopyToAsync(destination, bufferSize, cancellationToken).ConfigureAwai |
| | 0 | 321 | | } |
| | 0 | 322 | | } |
| | | 323 | | |
| | | 324 | | private static Stream CreateDecompressionStream(int firstByte, Stream stream) => |
| | 0 | 325 | | (firstByte & 0xF) == 8 ? |
| | 0 | 326 | | new ZLibStream(stream, CompressionMode.Decompress) : |
| | 0 | 327 | | new DeflateStream(stream, CompressionMode.Decompress); |
| | | 328 | | |
| | | 329 | | private sealed class PeekFirstByteReadStream : HttpBaseStream |
| | | 330 | | { |
| | | 331 | | private readonly Stream _stream; |
| | | 332 | | private byte _firstByte; |
| | | 333 | | private FirstByteStatus _firstByteStatus; |
| | | 334 | | |
| | 0 | 335 | | public PeekFirstByteReadStream(Stream stream) => _stream = stream; |
| | | 336 | | |
| | | 337 | | protected override void Dispose(bool disposing) |
| | 0 | 338 | | { |
| | 0 | 339 | | if (disposing) |
| | 0 | 340 | | { |
| | 0 | 341 | | _stream.Dispose(); |
| | 0 | 342 | | } |
| | 0 | 343 | | base.Dispose(disposing); |
| | 0 | 344 | | } |
| | | 345 | | |
| | 0 | 346 | | public override bool CanRead => true; |
| | 0 | 347 | | public override bool CanWrite => false; |
| | 0 | 348 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToke |
| | | 349 | | |
| | | 350 | | public int PeekFirstByte() |
| | 0 | 351 | | { |
| | 0 | 352 | | Debug.Assert(_firstByteStatus == FirstByteStatus.None); |
| | | 353 | | |
| | 0 | 354 | | int value = _stream.ReadByte(); |
| | 0 | 355 | | if (value == -1) |
| | 0 | 356 | | { |
| | 0 | 357 | | _firstByteStatus = FirstByteStatus.Consumed; |
| | 0 | 358 | | return -1; |
| | | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | _firstByte = (byte)value; |
| | 0 | 362 | | _firstByteStatus = FirstByteStatus.Available; |
| | 0 | 363 | | return value; |
| | 0 | 364 | | } |
| | | 365 | | |
| | | 366 | | public async ValueTask<int> PeekFirstByteAsync(CancellationToken cancellationToken) |
| | 0 | 367 | | { |
| | 0 | 368 | | Debug.Assert(_firstByteStatus == FirstByteStatus.None); |
| | | 369 | | |
| | 0 | 370 | | var buffer = new byte[1]; |
| | | 371 | | |
| | 0 | 372 | | int bytesRead = await _stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); |
| | 0 | 373 | | if (bytesRead == 0) |
| | 0 | 374 | | { |
| | 0 | 375 | | _firstByteStatus = FirstByteStatus.Consumed; |
| | 0 | 376 | | return -1; |
| | | 377 | | } |
| | | 378 | | |
| | 0 | 379 | | _firstByte = buffer[0]; |
| | 0 | 380 | | _firstByteStatus = FirstByteStatus.Available; |
| | 0 | 381 | | return buffer[0]; |
| | 0 | 382 | | } |
| | | 383 | | |
| | | 384 | | public override int Read(Span<byte> buffer) |
| | 0 | 385 | | { |
| | 0 | 386 | | if (_firstByteStatus == FirstByteStatus.Available) |
| | 0 | 387 | | { |
| | 0 | 388 | | if (buffer.Length != 0) |
| | 0 | 389 | | { |
| | 0 | 390 | | buffer[0] = _firstByte; |
| | 0 | 391 | | _firstByteStatus = FirstByteStatus.Consumed; |
| | 0 | 392 | | return 1; |
| | | 393 | | } |
| | | 394 | | |
| | 0 | 395 | | return 0; |
| | | 396 | | } |
| | | 397 | | |
| | 0 | 398 | | Debug.Assert(_firstByteStatus == FirstByteStatus.Consumed); |
| | 0 | 399 | | return _stream.Read(buffer); |
| | 0 | 400 | | } |
| | | 401 | | |
| | | 402 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) |
| | 0 | 403 | | { |
| | 0 | 404 | | if (_firstByteStatus == FirstByteStatus.Available) |
| | 0 | 405 | | { |
| | 0 | 406 | | if (buffer.Length != 0) |
| | 0 | 407 | | { |
| | 0 | 408 | | buffer.Span[0] = _firstByte; |
| | 0 | 409 | | _firstByteStatus = FirstByteStatus.Consumed; |
| | 0 | 410 | | return new ValueTask<int>(1); |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | return new ValueTask<int>(0); |
| | | 414 | | } |
| | | 415 | | |
| | 0 | 416 | | Debug.Assert(_firstByteStatus == FirstByteStatus.Consumed); |
| | 0 | 417 | | return _stream.ReadAsync(buffer, cancellationToken); |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancell |
| | 0 | 421 | | { |
| | 0 | 422 | | Debug.Assert(_firstByteStatus != FirstByteStatus.None); |
| | | 423 | | |
| | 0 | 424 | | ValidateCopyToArguments(destination, bufferSize); |
| | 0 | 425 | | if (_firstByteStatus == FirstByteStatus.Available) |
| | 0 | 426 | | { |
| | 0 | 427 | | await destination.WriteAsync(new byte[] { _firstByte }, cancellationToken).ConfigureAwait(fa |
| | 0 | 428 | | _firstByteStatus = FirstByteStatus.Consumed; |
| | 0 | 429 | | } |
| | | 430 | | |
| | 0 | 431 | | await _stream.CopyToAsync(destination, bufferSize, cancellationToken).ConfigureAwait(false); |
| | 0 | 432 | | } |
| | | 433 | | |
| | | 434 | | private enum FirstByteStatus : byte |
| | | 435 | | { |
| | | 436 | | None = 0, |
| | | 437 | | Available = 1, |
| | | 438 | | Consumed = 2 |
| | | 439 | | } |
| | | 440 | | } |
| | | 441 | | } |
| | | 442 | | } |
| | | 443 | | |
| | 0 | 444 | | private sealed class BrotliDecompressedContent(HttpContent originalContent, string[] contentEncodings) : Decompr |
| | | 445 | | { |
| | | 446 | | protected override Stream GetDecompressedStream(Stream originalStream) => |
| | 0 | 447 | | new BrotliStream(originalStream, CompressionMode.Decompress); |
| | | 448 | | } |
| | | 449 | | |
| | 0 | 450 | | private sealed class ZstandardDecompressedContent(HttpContent originalContent, string[] contentEncodings) : Deco |
| | | 451 | | { |
| | | 452 | | // RFC 9659 §3.2 mandates HTTP implementations cap decompression window size at 8 MB (WindowLog=23). |
| | 0 | 453 | | private static readonly ZstandardDecompressionOptions s_decompressionOptions = new ZstandardDecompressionOpt |
| | | 454 | | |
| | | 455 | | protected override Stream GetDecompressedStream(Stream originalStream) => |
| | 0 | 456 | | new ZstandardStream(originalStream, s_decompressionOptions); |
| | | 457 | | } |
| | | 458 | | } |
| | | 459 | | } |
| | | 460 | | |