| | | 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.IO; |
| | | 5 | | using System.IO.Compression; |
| | | 6 | | using System.Runtime.Versioning; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides HTTP content that compresses the inner content using the Zstandard content coding. |
| | | 14 | | /// </summary> |
| | | 15 | | [UnsupportedOSPlatform("browser")] |
| | | 16 | | [UnsupportedOSPlatform("wasi")] |
| | | 17 | | public sealed class ZstandardCompressedContent : HttpContent |
| | | 18 | | { |
| | | 19 | | private const string Encoding = "zstd"; |
| | | 20 | | |
| | | 21 | | // RFC 9659 requires zstd decoders for the "zstd" content coding to support a window size of at |
| | | 22 | | // least 8 MB (2^23) and recommends that encoders not produce frames requiring a larger window. |
| | | 23 | | // Some compression levels (notably CompressionLevel.SmallestSize) would otherwise select a larger |
| | | 24 | | // window, producing payloads that a conformant server would reject. See RFC 9659, Section 3. |
| | | 25 | | private const int RfcMaxWindowLog2 = 23; |
| | 0 | 26 | | private static readonly ZstandardCompressionOptions s_smallestSizeRfcOptions = new ZstandardCompressionOptions |
| | 0 | 27 | | { |
| | 0 | 28 | | Quality = ZstandardCompressionOptions.MaxQuality, |
| | 0 | 29 | | WindowLog2 = RfcMaxWindowLog2 |
| | 0 | 30 | | }; |
| | | 31 | | |
| | | 32 | | private readonly HttpContent _content; |
| | | 33 | | private readonly ZstandardCompressionOptions? _compressionOptions; |
| | | 34 | | private readonly CompressionLevel _compressionLevel; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="ZstandardCompressedContent"/> class that compresses the |
| | | 38 | | /// provided content using the Zstandard content coding at the specified compression level. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="content">The HTTP content to compress.</param> |
| | | 41 | | /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co |
| | | 42 | | /// <remarks> |
| | | 43 | | /// RFC 9659 requires that the "zstd" content coding be decodable with a window size of 8 MB (2^23) and |
| | | 44 | | /// recommends that encoders not produce frames requiring a larger window. When setting <paramref name="compress |
| | | 45 | | /// to <see cref="CompressionLevel.SmallestSize"/>, this class applies RFC-compliant options to limit the window |
| | | 46 | | /// so that the produced content is accepted by servers that enforce this limit. |
| | | 47 | | /// </remarks> |
| | 0 | 48 | | public ZstandardCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Opti |
| | 0 | 49 | | { |
| | 0 | 50 | | ArgumentNullException.ThrowIfNull(content); |
| | 0 | 51 | | CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel)); |
| | | 52 | | |
| | 0 | 53 | | if (compressionLevel == CompressionLevel.SmallestSize) |
| | 0 | 54 | | { |
| | | 55 | | // use RFC-compliant options for SmallestSize to avoid producing frames that a conformant server would r |
| | 0 | 56 | | _compressionOptions = s_smallestSizeRfcOptions; |
| | 0 | 57 | | } |
| | | 58 | | else |
| | 0 | 59 | | { |
| | 0 | 60 | | _compressionLevel = compressionLevel; |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | _content = content; |
| | 0 | 64 | | CompressedContentCore.InitializeHeaders(this, content, Encoding); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Initializes a new instance of the <see cref="ZstandardCompressedContent"/> class that compresses the |
| | | 69 | | /// provided content using the Zstandard content coding and the specified compression options. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="content">The HTTP content to compress.</param> |
| | | 72 | | /// <param name="compressionOptions">The options used to fine-tune the compression.</param> |
| | | 73 | | /// <remarks> |
| | | 74 | | /// RFC 9659 requires that the "zstd" content coding be decodable with a window size of 8 MB (2^23) and |
| | | 75 | | /// recommends that encoders not produce frames requiring a larger window. When supplying custom options, |
| | | 76 | | /// consider limiting <see cref="ZstandardCompressionOptions.WindowLog2"/> to 23 or less so that the |
| | | 77 | | /// produced content is accepted by servers that enforce this limit. |
| | | 78 | | /// </remarks> |
| | 0 | 79 | | public ZstandardCompressedContent(HttpContent content, ZstandardCompressionOptions compressionOptions) |
| | 0 | 80 | | { |
| | 0 | 81 | | ArgumentNullException.ThrowIfNull(content); |
| | 0 | 82 | | ArgumentNullException.ThrowIfNull(compressionOptions); |
| | | 83 | | |
| | 0 | 84 | | _compressionOptions = compressionOptions; |
| | 0 | 85 | | _content = content; |
| | 0 | 86 | | CompressedContentCore.InitializeHeaders(this, content, Encoding); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati |
| | 0 | 90 | | CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToke |
| | | 91 | | |
| | | 92 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => |
| | 0 | 93 | | CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, Cancellatio |
| | | 94 | | |
| | | 95 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance |
| | 0 | 96 | | CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellatio |
| | | 97 | | |
| | | 98 | | protected internal override bool TryComputeLength(out long length) |
| | 0 | 99 | | { |
| | | 100 | | // Compressed size is not known without actually compressing. |
| | 0 | 101 | | length = 0; |
| | 0 | 102 | | return false; |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | internal override bool AllowDuplex => false; |
| | | 106 | | |
| | | 107 | | protected override void Dispose(bool disposing) |
| | 0 | 108 | | { |
| | 0 | 109 | | if (disposing) |
| | 0 | 110 | | { |
| | 0 | 111 | | _content.Dispose(); |
| | 0 | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | base.Dispose(disposing); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | private ZstandardStream CreateCompressionStream(Stream outputStream) => |
| | 0 | 118 | | _compressionOptions is null |
| | 0 | 119 | | ? new ZstandardStream(outputStream, _compressionLevel, leaveOpen: true) |
| | 0 | 120 | | : new ZstandardStream(outputStream, _compressionOptions, leaveOpen: true); |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |