From df54b2417f74d410657ccc8e1326a444e39d2e24 Mon Sep 17 00:00:00 2001 From: Matthew Sigley Date: Wed, 9 Aug 2023 10:56:55 -0400 Subject: [PATCH 1/2] Added ApplyDefaultCommandsToQuerylessUrls route option. --- src/Imageflow.Server.Configuration/Executor.cs | 5 ++++- .../MinimalParseStructures.cs | 1 + src/Imageflow.Server.Configuration/minimal.toml | 1 + src/Imageflow.Server/ImageJobInfo.cs | 2 +- src/Imageflow.Server/ImageflowMiddlewareOptions.cs | 14 +++++++++++++- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Imageflow.Server.Configuration/Executor.cs b/src/Imageflow.Server.Configuration/Executor.cs index 1d17d3d8..3f944546 100644 --- a/src/Imageflow.Server.Configuration/Executor.cs +++ b/src/Imageflow.Server.Configuration/Executor.cs @@ -121,7 +121,10 @@ public ImageflowMiddlewareOptions GetImageflowMiddlewareOptions(){ } } - + if (config.RouteDefaults?.ApplyDefaultCommandsToQuerylessUrls ?? false){ + options.SetApplyDefaultCommandsToQuerylessUrls(true); + } + // set security options var securityOptions = new SecurityOptions(); if (config.Security?.MaxDecodeResolution != null){ diff --git a/src/Imageflow.Server.Configuration/MinimalParseStructures.cs b/src/Imageflow.Server.Configuration/MinimalParseStructures.cs index 85b91bf4..8117720b 100644 --- a/src/Imageflow.Server.Configuration/MinimalParseStructures.cs +++ b/src/Imageflow.Server.Configuration/MinimalParseStructures.cs @@ -101,6 +101,7 @@ internal class RouteBase : ITomlMetadataProvider, IValidationCapable // public RouteSource? Source { get; set; } public string? CacheControl { get; set; } public string? ApplyDefaultCommands { get; set; } + public bool? ApplyDefaultCommandsToQuerylessUrls { get; set; } // public string? ApplyOverrideCommands { get; set; } // public string? RequireSignature { get; set; } diff --git a/src/Imageflow.Server.Configuration/minimal.toml b/src/Imageflow.Server.Configuration/minimal.toml index 84f3cc73..8a44dec2 100644 --- a/src/Imageflow.Server.Configuration/minimal.toml +++ b/src/Imageflow.Server.Configuration/minimal.toml @@ -11,6 +11,7 @@ lowercase_path_remainder = false allow_extensionless_urls = false cache_control = "public, max-age=2592000" apply_default_commands = "quality=76&webp.quality=70&f.sharpen=23&down.filter=mitchell" +apply_default_commands_to_queryless_urls = false [[routes]] prefix = '/images/' diff --git a/src/Imageflow.Server/ImageJobInfo.cs b/src/Imageflow.Server/ImageJobInfo.cs index c6728ef7..ff4ae2b5 100644 --- a/src/Imageflow.Server/ImageJobInfo.cs +++ b/src/Imageflow.Server/ImageJobInfo.cs @@ -219,7 +219,7 @@ private bool ProcessRewritesAndAuthorization(HttpContext context, ImageflowMiddl } // Set defaults if keys are missing, but at least 1 supported key is present - if (PathHelpers.SupportedQuerystringKeys.Any(args.Query.ContainsKey)) + if (middlewareOptions.ApplyDefaultCommandsToQuerylessUrls || PathHelpers.SupportedQuerystringKeys.Any(args.Query.ContainsKey)) { foreach (var pair in middlewareOptions.CommandDefaults) { diff --git a/src/Imageflow.Server/ImageflowMiddlewareOptions.cs b/src/Imageflow.Server/ImageflowMiddlewareOptions.cs index cf488261..c8fb7420 100644 --- a/src/Imageflow.Server/ImageflowMiddlewareOptions.cs +++ b/src/Imageflow.Server/ImageflowMiddlewareOptions.cs @@ -55,7 +55,8 @@ public class ImageflowMiddlewareOptions internal readonly Dictionary CommandDefaults = new Dictionary(StringComparer.OrdinalIgnoreCase); - + public bool ApplyDefaultCommandsToQuerylessUrls { get; set; } = false; + internal readonly Dictionary Presets = new Dictionary(StringComparer.OrdinalIgnoreCase); internal readonly List ExtensionlessPaths = new List(); @@ -242,5 +243,16 @@ public ImageflowMiddlewareOptions SetDefaultCacheControlString(string cacheContr DefaultCacheControlString = cacheControlString; return this; } + + /// + /// Set to true have the command defaults to all urls, not just ones containing a valid query command. + /// + /// + /// + public ImageflowMiddlewareOptions SetApplyDefaultCommandsToQuerylessUrls(bool value) + { + this.ApplyDefaultCommandsToQuerylessUrls = value; + return this; + } } } \ No newline at end of file From 72bb560ff554a35795ced7783ae7693de92580ad Mon Sep 17 00:00:00 2001 From: Matthew Sigley Date: Fri, 11 Aug 2023 11:47:20 -0400 Subject: [PATCH 2/2] Updated TOML unittest. --- tests/Imageflow.Server.Configuration.Tests/TestMinimal.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Imageflow.Server.Configuration.Tests/TestMinimal.cs b/tests/Imageflow.Server.Configuration.Tests/TestMinimal.cs index 0cb1224d..a46f7e12 100644 --- a/tests/Imageflow.Server.Configuration.Tests/TestMinimal.cs +++ b/tests/Imageflow.Server.Configuration.Tests/TestMinimal.cs @@ -87,6 +87,7 @@ public void TestComputedOptions(DeploymentEnvironment environment) {"ImageflowMiddlewareOptions.MapWebRoot", "False"}, {"ImageflowMiddlewareOptions.UsePresetsExclusively", "False"}, {"ImageflowMiddlewareOptions.DefaultCacheControlString", "public, max-age=20"}, + {"ImageflowMiddlewareOptions.ApplyDefaultCommandsToQuerylessUrls", "True"}, {"ImageflowMiddlewareOptions.RequestSignatureOptions", "null"}, {"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxWidth", "12000"}, {"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxHeight", "12000"}, @@ -135,6 +136,7 @@ public void TestComputedOptions(DeploymentEnvironment environment) {"ImageflowMiddlewareOptions.MapWebRoot", "False"}, {"ImageflowMiddlewareOptions.UsePresetsExclusively", "False"}, {"ImageflowMiddlewareOptions.DefaultCacheControlString", "public, max-age=20"}, + {"ImageflowMiddlewareOptions.ApplyDefaultCommandsToQuerylessUrls", "True"}, {"ImageflowMiddlewareOptions.RequestSignatureOptions", "null"}, {"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxWidth", "12000"}, {"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxHeight", "12000"}, @@ -183,6 +185,7 @@ public void TestComputedOptions(DeploymentEnvironment environment) {"ImageflowMiddlewareOptions.MapWebRoot", "False"}, {"ImageflowMiddlewareOptions.UsePresetsExclusively", "False"}, {"ImageflowMiddlewareOptions.DefaultCacheControlString", "public, max-age=20"}, + {"ImageflowMiddlewareOptions.ApplyDefaultCommandsToQuerylessUrls", "True"}, {"ImageflowMiddlewareOptions.RequestSignatureOptions", "null"}, {"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxWidth", "12000"}, {"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxHeight", "12000"}, @@ -232,6 +235,7 @@ public void TestComputedOptions(DeploymentEnvironment environment) lowercase_path_remainder = true cache_control = "public, max-age=20" apply_default_commands = "quality=76" +apply_default_commands_to_queryless_urls = true [[routes]] prefix = '/images/'