| | | 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 Brotli content coding. |
| | | 14 | | /// </summary> |
| | | 15 | | [UnsupportedOSPlatform("browser")] |
| | | 16 | | [UnsupportedOSPlatform("wasi")] |
| | | 17 | | public sealed class BrotliCompressedContent : HttpContent |
| | | 18 | | { |
| | | 19 | | private const string Encoding = "br"; |
| | | 20 | | |
| | | 21 | | private readonly HttpContent _content; |
| | | 22 | | private readonly BrotliCompressionOptions? _compressionOptions; |
| | | 23 | | private readonly CompressionLevel _compressionLevel; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the |
| | | 27 | | /// provided content using the Brotli content coding at the specified compression level. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="content">The HTTP content to compress.</param> |
| | | 30 | | /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co |
| | 0 | 31 | | public BrotliCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Optimal |
| | 0 | 32 | | { |
| | 0 | 33 | | ArgumentNullException.ThrowIfNull(content); |
| | 0 | 34 | | CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel)); |
| | | 35 | | |
| | 0 | 36 | | _compressionLevel = compressionLevel; |
| | 0 | 37 | | _content = content; |
| | 0 | 38 | | CompressedContentCore.InitializeHeaders(this, content, Encoding); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the |
| | | 43 | | /// provided content using the Brotli content coding and the specified compression options. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="content">The HTTP content to compress.</param> |
| | | 46 | | /// <param name="compressionOptions">The options used to fine-tune the compression.</param> |
| | 0 | 47 | | public BrotliCompressedContent(HttpContent content, BrotliCompressionOptions compressionOptions) |
| | 0 | 48 | | { |
| | 0 | 49 | | ArgumentNullException.ThrowIfNull(content); |
| | 0 | 50 | | ArgumentNullException.ThrowIfNull(compressionOptions); |
| | | 51 | | |
| | 0 | 52 | | _compressionOptions = compressionOptions; |
| | 0 | 53 | | _content = content; |
| | 0 | 54 | | CompressedContentCore.InitializeHeaders(this, content, Encoding); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati |
| | 0 | 58 | | CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToke |
| | | 59 | | |
| | | 60 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => |
| | 0 | 61 | | CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, Cancellatio |
| | | 62 | | |
| | | 63 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance |
| | 0 | 64 | | CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellatio |
| | | 65 | | |
| | | 66 | | protected internal override bool TryComputeLength(out long length) |
| | 0 | 67 | | { |
| | | 68 | | // Compressed size is not known without actually compressing. |
| | 0 | 69 | | length = 0; |
| | 0 | 70 | | return false; |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | internal override bool AllowDuplex => false; |
| | | 74 | | |
| | | 75 | | protected override void Dispose(bool disposing) |
| | 0 | 76 | | { |
| | 0 | 77 | | if (disposing) |
| | 0 | 78 | | { |
| | 0 | 79 | | _content.Dispose(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | base.Dispose(disposing); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | private BrotliStream CreateCompressionStream(Stream outputStream) => |
| | 0 | 86 | | _compressionOptions is null |
| | 0 | 87 | | ? new BrotliStream(outputStream, _compressionLevel, leaveOpen: true) |
| | 0 | 88 | | : new BrotliStream(outputStream, _compressionOptions, leaveOpen: true); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |