| | | 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.Net.Http.Headers; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Shared helpers for the per-algorithm compressed content types. Provides header initialization, |
| | | 14 | | /// argument validation, and serialization that copies the inner content through a compression stream. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class CompressedContentCore |
| | | 17 | | { |
| | | 18 | | public static void ValidateCompressionLevel(CompressionLevel compressionLevel, string paramName) |
| | 0 | 19 | | { |
| | | 20 | | // Validate up front so an invalid enum value fails fast at construction time rather than |
| | | 21 | | // deferring the exception to the serialization path when the request is being sent. |
| | 0 | 22 | | if (compressionLevel is not (CompressionLevel.Optimal or CompressionLevel.Fastest or CompressionLevel.NoComp |
| | 0 | 23 | | { |
| | 0 | 24 | | throw new ArgumentOutOfRangeException(paramName); |
| | | 25 | | } |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public static void InitializeHeaders(HttpContent target, HttpContent source, string encoding) |
| | 0 | 29 | | { |
| | | 30 | | // Copy headers from the original content. |
| | 0 | 31 | | target.Headers.AddHeaders(source.Headers); |
| | | 32 | | |
| | | 33 | | // Append our encoding to the Content-Encoding header (supports stacking per HTTP spec). |
| | | 34 | | // Always use TryAddWithoutValidation so we append the new encoding without re-parsing or |
| | | 35 | | // validating any existing Content-Encoding values the inner content may have added. |
| | 0 | 36 | | target.Headers.TryAddWithoutValidation(KnownHeaders.ContentEncoding.Descriptor, encoding); |
| | | 37 | | |
| | | 38 | | // Remove Content-Length since we don't know the compressed size upfront. |
| | 0 | 39 | | target.Headers.ContentLength = null; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public static void SerializeToStream(HttpContent originalContent, Stream compressionStream, TransportContext? co |
| | 0 | 43 | | { |
| | 0 | 44 | | using (compressionStream) |
| | 0 | 45 | | { |
| | 0 | 46 | | originalContent.CopyTo(compressionStream, context, cancellationToken); |
| | 0 | 47 | | } |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | public static async Task SerializeToStreamAsync(HttpContent originalContent, Stream compressionStream, Transport |
| | 0 | 51 | | { |
| | 0 | 52 | | await using (compressionStream.ConfigureAwait(false)) |
| | 0 | 53 | | { |
| | 0 | 54 | | await originalContent.CopyToAsync(compressionStream, context, cancellationToken).ConfigureAwait(false); |
| | 0 | 55 | | } |
| | 0 | 56 | | } |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |