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