| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Extensions.DependencyInjection; |
| | 4 | | using IceRpc.Features; |
| | 5 | | using IceRpc.Internal; |
| | 6 | | using System.Buffers; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.IO.Compression; |
| | 9 | | using System.IO.Pipelines; |
| | 10 | | using ZeroC.Slice; |
| | 11 | |
|
| | 12 | | namespace IceRpc.Compressor; |
| | 13 | |
|
| | 14 | | /// <summary>Represents an interceptor that compresses the payloads of outgoing requests and decompresses the payloads |
| | 15 | | /// of incoming responses.</summary> |
| | 16 | | /// <remarks>This interceptor compresses the payload of a request and sets the |
| | 17 | | /// <see cref="RequestFieldKey.CompressionFormat" /> field when this request has the <see cref="ICompressFeature" /> |
| | 18 | | /// feature set and the CompressionFormat field is unset.<br/> |
| | 19 | | /// This interceptor decompresses the payload of a response when this response's status code is |
| | 20 | | /// <see cref="StatusCode.Ok" /> and the response carries a <see cref="ResponseFieldKey.CompressionFormat" /> field |
| | 21 | | /// with a supported compression format (currently <see cref="CompressionFormat.Brotli" /> or |
| | 22 | | /// <see cref="CompressionFormat.Deflate" />).</remarks> |
| | 23 | | /// <seealso cref="CompressorPipelineExtensions"/> |
| | 24 | | /// <seealso cref="CompressorDispatcherBuilderExtensions"/> |
| | 25 | | public class CompressorInterceptor : IInvoker |
| | 26 | | { |
| | 27 | | private readonly CompressionFormat _compressionFormat; |
| | 28 | | private readonly CompressionLevel _compressionLevel; |
| | 29 | | private readonly ReadOnlySequence<byte> _encodedCompressionFormatValue; |
| | 30 | | private readonly IInvoker _next; |
| | 31 | |
|
| | 32 | | /// <summary>Constructs a Compressor interceptor.</summary> |
| | 33 | | /// <param name="next">The next invoker in the invocation pipeline.</param> |
| | 34 | | /// <param name="compressionFormat">The compression format for the compress operation.</param> |
| | 35 | | /// <param name="compressionLevel">The compression level for the compress operation.</param> |
| 9 | 36 | | public CompressorInterceptor( |
| 9 | 37 | | IInvoker next, |
| 9 | 38 | | CompressionFormat compressionFormat, |
| 9 | 39 | | CompressionLevel compressionLevel = CompressionLevel.Fastest) |
| 9 | 40 | | { |
| 9 | 41 | | _next = next; |
| 9 | 42 | | if (compressionFormat != CompressionFormat.Brotli && compressionFormat != CompressionFormat.Deflate) |
| 0 | 43 | | { |
| 0 | 44 | | throw new NotSupportedException($"The compression format '{compressionFormat}' is not supported."); |
| | 45 | | } |
| 9 | 46 | | _compressionFormat = compressionFormat; |
| 9 | 47 | | _compressionLevel = compressionLevel; |
| 9 | 48 | | _encodedCompressionFormatValue = new(new byte[] { (byte)compressionFormat }); |
| 9 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc/> |
| | 52 | | public async Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) |
| 9 | 53 | | { |
| | 54 | | // The ICompressFeature is typically set through the Slice compress attribute. |
| | 55 | |
|
| 9 | 56 | | if (request.Protocol.HasFields && |
| 9 | 57 | | request.Features.Get<ICompressFeature>() is ICompressFeature compress && |
| 9 | 58 | | compress.Value && |
| 9 | 59 | | !request.Fields.ContainsKey(RequestFieldKey.CompressionFormat)) |
| 4 | 60 | | { |
| 4 | 61 | | if (_compressionFormat == CompressionFormat.Brotli) |
| 2 | 62 | | { |
| 4 | 63 | | request.Use(next => PipeWriter.Create(new BrotliStream(next.AsStream(), _compressionLevel))); |
| 2 | 64 | | } |
| | 65 | | else |
| 2 | 66 | | { |
| 2 | 67 | | Debug.Assert(_compressionFormat == CompressionFormat.Deflate); |
| 4 | 68 | | request.Use(next => PipeWriter.Create(new DeflateStream(next.AsStream(), _compressionLevel))); |
| 2 | 69 | | } |
| | 70 | |
|
| 4 | 71 | | request.Fields = request.Fields.With(RequestFieldKey.CompressionFormat, _encodedCompressionFormatValue); |
| 4 | 72 | | } |
| | 73 | |
|
| 9 | 74 | | IncomingResponse response = await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | 75 | |
|
| 9 | 76 | | if (request.Protocol.HasFields && response.StatusCode == StatusCode.Ok) |
| 9 | 77 | | { |
| 9 | 78 | | CompressionFormat compressionFormat = response.Fields.DecodeValue( |
| 9 | 79 | | ResponseFieldKey.CompressionFormat, |
| 14 | 80 | | (ref SliceDecoder decoder) => decoder.DecodeCompressionFormat()); |
| | 81 | |
|
| 9 | 82 | | if (compressionFormat == CompressionFormat.Brotli) |
| 2 | 83 | | { |
| 2 | 84 | | response.Payload = PipeReader.Create( |
| 2 | 85 | | new BrotliStream(response.Payload.AsStream(), CompressionMode.Decompress)); |
| | 86 | |
|
| | 87 | | // Work around bug from StreamPipeReader with the BugFixStreamPipeReaderDecorator |
| 2 | 88 | | response.Payload = new BugFixStreamPipeReaderDecorator(response.Payload); |
| 2 | 89 | | } |
| 7 | 90 | | else if (compressionFormat == CompressionFormat.Deflate) |
| 2 | 91 | | { |
| 2 | 92 | | response.Payload = PipeReader.Create( |
| 2 | 93 | | new DeflateStream(response.Payload.AsStream(), CompressionMode.Decompress)); |
| | 94 | |
|
| | 95 | | // Work around bug from StreamPipeReader with the BugFixStreamPipeReaderDecorator |
| 2 | 96 | | response.Payload = new BugFixStreamPipeReaderDecorator(response.Payload); |
| 2 | 97 | | } |
| | 98 | | // else nothing to do |
| 9 | 99 | | } |
| | 100 | |
|
| 9 | 101 | | return response; |
| 9 | 102 | | } |
| | 103 | | } |