< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 123
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
.ctor(...)0%220%
.ctor(...)100%110%
SerializeToStream(...)100%110%
SerializeToStreamAsync(...)100%110%
SerializeToStreamAsync(...)100%110%
TryComputeLength(...)100%110%
Dispose(...)0%220%
CreateCompressionStream(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/ZstandardCompressedContent.cs

#LineLine coverage
 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
 4using System.IO;
 5using System.IO.Compression;
 6using System.Runtime.Versioning;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10namespace 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;
 026        private static readonly ZstandardCompressionOptions s_smallestSizeRfcOptions = new ZstandardCompressionOptions
 027        {
 028            Quality = ZstandardCompressionOptions.MaxQuality,
 029            WindowLog2 = RfcMaxWindowLog2
 030        };
 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>
 048        public ZstandardCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Opti
 049        {
 050            ArgumentNullException.ThrowIfNull(content);
 051            CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel));
 52
 053            if (compressionLevel == CompressionLevel.SmallestSize)
 054            {
 55                // use RFC-compliant options for SmallestSize to avoid producing frames that a conformant server would r
 056                _compressionOptions = s_smallestSizeRfcOptions;
 057            }
 58            else
 059            {
 060                _compressionLevel = compressionLevel;
 061            }
 62
 063            _content = content;
 064            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 065        }
 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>
 079        public ZstandardCompressedContent(HttpContent content, ZstandardCompressionOptions compressionOptions)
 080        {
 081            ArgumentNullException.ThrowIfNull(content);
 082            ArgumentNullException.ThrowIfNull(compressionOptions);
 83
 084            _compressionOptions = compressionOptions;
 085            _content = content;
 086            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 087        }
 88
 89        protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati
 090            CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToke
 91
 92        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
 093            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, Cancellatio
 94
 95        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance
 096            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellatio
 97
 98        protected internal override bool TryComputeLength(out long length)
 099        {
 100            // Compressed size is not known without actually compressing.
 0101            length = 0;
 0102            return false;
 0103        }
 104
 0105        internal override bool AllowDuplex => false;
 106
 107        protected override void Dispose(bool disposing)
 0108        {
 0109            if (disposing)
 0110            {
 0111                _content.Dispose();
 0112            }
 113
 0114            base.Dispose(disposing);
 0115        }
 116
 117        private ZstandardStream CreateCompressionStream(Stream outputStream) =>
 0118            _compressionOptions is null
 0119                ? new ZstandardStream(outputStream, _compressionLevel, leaveOpen: true)
 0120                : new ZstandardStream(outputStream, _compressionOptions, leaveOpen: true);
 121    }
 122}
 123