From fa21e93f390390cee5b6e5d440e9439a8b1b81f1 Mon Sep 17 00:00:00 2001 From: Erik O'Leary <969938+onionhammer@users.noreply.github.com> Date: Fri, 29 Mar 2024 23:26:23 -0500 Subject: [PATCH] Add compression-level option (#147) --- src/dotnet-serve/CommandLineOptions.cs | 4 ++++ src/dotnet-serve/Program.cs | 4 ++++ src/dotnet-serve/Startup.cs | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/dotnet-serve/CommandLineOptions.cs b/src/dotnet-serve/CommandLineOptions.cs index 7045b19..14e8168 100644 --- a/src/dotnet-serve/CommandLineOptions.cs +++ b/src/dotnet-serve/CommandLineOptions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.ComponentModel.DataAnnotations; +using System.IO.Compression; using System.Net; using System.Reflection; using McMaster.Extensions.CommandLineUtils; @@ -124,6 +125,9 @@ public virtual bool UseTls [Option("-b|--brotli", Description = "Enable brotli compression")] public bool? UseBrotli { get; internal set; } + [Option("--compression-level", Description = "The level of compression to use, when compression is enabled")] + public CompressionLevel? CompressionLevel { get; internal set; } + [Option("-c|--cors", Description = "Enable CORS (It will enable CORS for all origin and all methods)")] public bool? EnableCors { get; internal set; } diff --git a/src/dotnet-serve/Program.cs b/src/dotnet-serve/Program.cs index 5f77c85..e091a26 100644 --- a/src/dotnet-serve/Program.cs +++ b/src/dotnet-serve/Program.cs @@ -1,6 +1,7 @@ // Copyright (c) Nate McMaster. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.IO.Compression; using DotNetConfig; using McMaster.Extensions.CommandLineUtils; @@ -93,6 +94,9 @@ private static void ReadConfig(ConfigSection config, CommandLineOptions model) model.CertificatePassword ??= config.GetString("pfx-pwd"); model.UseGzip ??= config.GetBoolean("gzip"); model.UseBrotli ??= config.GetBoolean("brotli"); + model.CompressionLevel ??= config.GetString("compression-level") is string compressionLevel + ? Enum.Parse(compressionLevel, ignoreCase: true) + : default; model.EnableCors ??= config.GetBoolean("cors"); model.PathBase ??= config.GetString("path-base"); model.FallbackFile ??= config.GetString("fallback-file"); diff --git a/src/dotnet-serve/Startup.cs b/src/dotnet-serve/Startup.cs index 82658d7..22cdfbe 100644 --- a/src/dotnet-serve/Startup.cs +++ b/src/dotnet-serve/Startup.cs @@ -54,13 +54,23 @@ public void ConfigureServices(IServiceCollection services) if (_options.UseGzip == true) { - options.Providers.Add(); + var gzipCompressionProvider = new GzipCompressionProvider(new GzipCompressionProviderOptions + { + Level = _options.CompressionLevel ?? System.IO.Compression.CompressionLevel.Fastest + }); + + options.Providers.Add(gzipCompressionProvider); } if (_options.UseBrotli == true) { - options.Providers.Add(); + var brotliCompressionProvider = new BrotliCompressionProvider(new BrotliCompressionProviderOptions + { + Level = _options.CompressionLevel ?? System.IO.Compression.CompressionLevel.Fastest + }); + + options.Providers.Add(brotliCompressionProvider); } });