From e9a054d743fbe37de538c14451f4b97ba13f9418 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Wed, 5 Jul 2023 20:44:41 -0400 Subject: [PATCH] More work, but still WIP --- .../CanonicalDomainMiddleware.cs | 10 +-- .../IApplicationBuilderExtensions.cs | 63 +++++++++++-------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/BitBadger.AspNetCore.CanonicalDomains/CanonicalDomainMiddleware.cs b/src/BitBadger.AspNetCore.CanonicalDomains/CanonicalDomainMiddleware.cs index ca875be..853e8aa 100644 --- a/src/BitBadger.AspNetCore.CanonicalDomains/CanonicalDomainMiddleware.cs +++ b/src/BitBadger.AspNetCore.CanonicalDomains/CanonicalDomainMiddleware.cs @@ -3,6 +3,9 @@ using Microsoft.AspNetCore.Http.Extensions; namespace BitBadger.AspNetCore.CanonicalDomains; +/// +/// Middleware to enforce canonical domains +/// public class CanonicalDomainMiddleware { /// @@ -26,12 +29,11 @@ public class CanonicalDomainMiddleware public async Task InvokeAsync(HttpContext ctx) { - var host = ctx.Request.Host.Host; - if (CanonicalDomains.ContainsKey(host)) + if (CanonicalDomains.ContainsKey(ctx.Request.Host.Host)) { UriBuilder uri = new(ctx.Request.GetDisplayUrl()); - uri.Host = CanonicalDomains[host]; - ctx.Response.Redirect(uri.Uri.ToString ()); + uri.Host = CanonicalDomains[ctx.Request.Host.Host]; + ctx.Response.Redirect(uri.Uri.ToString(), permanent: true); } else { diff --git a/src/BitBadger.AspNetCore.CanonicalDomains/IApplicationBuilderExtensions.cs b/src/BitBadger.AspNetCore.CanonicalDomains/IApplicationBuilderExtensions.cs index 7610e9d..cf43cc0 100644 --- a/src/BitBadger.AspNetCore.CanonicalDomains/IApplicationBuilderExtensions.cs +++ b/src/BitBadger.AspNetCore.CanonicalDomains/IApplicationBuilderExtensions.cs @@ -14,18 +14,31 @@ public static class IApplicationBuilderExtensions /// public static IApplicationBuilder UseCanonicalDomains(this IApplicationBuilder app) { - void warnForMissingConfig() { - var logger = (ILogger?)app.ApplicationServices - .GetService(typeof(ILogger)); - if (logger is not null) - { - logger.LogWarning("No canonical domain configuration was found; no domains will be redirected"); - } + ParseConfiguration(GetService(app)!.GetSection("CanonicalDomains")); + + if (CanonicalDomainMiddleware.CanonicalDomains.Count > 0) + { + return app.UseMiddleware(); } + + WarnForMissingConfig(app); + return app; + } - var config = (IConfiguration)app.ApplicationServices.GetService(typeof(IConfiguration))!; - - var section = config.GetSection("CanonicalDomains"); + /// + /// Shorthand for retrieving typed services from the application's service provider + /// + /// The application builder + /// The requested service, or null if it was not able to be found + private static T? GetService(IApplicationBuilder app) => + (T?)app.ApplicationServices.GetService(typeof(T)); + + /// + /// Extract the from/to domain paris from the configuration + /// + /// The CanonicalDomains configuration section + private static void ParseConfiguration(IConfigurationSection? section) + { if (section is not null) { foreach (var item in section.GetChildren()) @@ -37,21 +50,19 @@ public static class IApplicationBuilderExtensions CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical); } } - - if (CanonicalDomainMiddleware.CanonicalDomains.Count > 0) - { - app.UseMiddleware (); - } - else - { - warnForMissingConfig(); - } } - else - { - warnForMissingConfig(); - } - - return app; } -} \ No newline at end of file + + /// + /// Generate a warning if no configured domains were found + /// + /// The application builder + private static void WarnForMissingConfig(IApplicationBuilder app) + { + var logger = GetService>(app); + if (logger is not null) + { + logger.LogWarning("No canonical domain configuration was found; no domains will be redirected"); + } + } +}