More work, but still WIP

This commit is contained in:
Daniel J. Summers 2023-07-05 20:44:41 -04:00
parent e39ee05314
commit e9a054d743
2 changed files with 43 additions and 30 deletions

View File

@ -3,6 +3,9 @@ using Microsoft.AspNetCore.Http.Extensions;
namespace BitBadger.AspNetCore.CanonicalDomains; namespace BitBadger.AspNetCore.CanonicalDomains;
/// <summary>
/// Middleware to enforce canonical domains
/// </summary>
public class CanonicalDomainMiddleware public class CanonicalDomainMiddleware
{ {
/// <summary> /// <summary>
@ -26,12 +29,11 @@ public class CanonicalDomainMiddleware
public async Task InvokeAsync(HttpContext ctx) public async Task InvokeAsync(HttpContext ctx)
{ {
var host = ctx.Request.Host.Host; if (CanonicalDomains.ContainsKey(ctx.Request.Host.Host))
if (CanonicalDomains.ContainsKey(host))
{ {
UriBuilder uri = new(ctx.Request.GetDisplayUrl()); UriBuilder uri = new(ctx.Request.GetDisplayUrl());
uri.Host = CanonicalDomains[host]; uri.Host = CanonicalDomains[ctx.Request.Host.Host];
ctx.Response.Redirect(uri.Uri.ToString ()); ctx.Response.Redirect(uri.Uri.ToString(), permanent: true);
} }
else else
{ {

View File

@ -14,18 +14,31 @@ public static class IApplicationBuilderExtensions
/// </summary> /// </summary>
public static IApplicationBuilder UseCanonicalDomains(this IApplicationBuilder app) public static IApplicationBuilder UseCanonicalDomains(this IApplicationBuilder app)
{ {
void warnForMissingConfig() { ParseConfiguration(GetService<IConfiguration>(app)!.GetSection("CanonicalDomains"));
var logger = (ILogger<CanonicalDomainMiddleware>?)app.ApplicationServices
.GetService(typeof(ILogger<CanonicalDomainMiddleware>)); if (CanonicalDomainMiddleware.CanonicalDomains.Count > 0)
if (logger is not null) {
{ return app.UseMiddleware<CanonicalDomainMiddleware>();
logger.LogWarning("No canonical domain configuration was found; no domains will be redirected");
}
} }
WarnForMissingConfig(app);
return app;
}
var config = (IConfiguration)app.ApplicationServices.GetService(typeof(IConfiguration))!; /// <summary>
/// Shorthand for retrieving typed services from the application's service provider
var section = config.GetSection("CanonicalDomains"); /// </summary>
/// <param name="app">The application builder</param>
/// <returns>The requested service, or null if it was not able to be found</returns>
private static T? GetService<T>(IApplicationBuilder app) =>
(T?)app.ApplicationServices.GetService(typeof(T));
/// <summary>
/// Extract the from/to domain paris from the configuration
/// </summary>
/// <param name="section">The <tt>CanonicalDomains</tt> configuration section</param>
private static void ParseConfiguration(IConfigurationSection? section)
{
if (section is not null) if (section is not null)
{ {
foreach (var item in section.GetChildren()) foreach (var item in section.GetChildren())
@ -37,21 +50,19 @@ public static class IApplicationBuilderExtensions
CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical); CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical);
} }
} }
if (CanonicalDomainMiddleware.CanonicalDomains.Count > 0)
{
app.UseMiddleware<CanonicalDomainMiddleware> ();
}
else
{
warnForMissingConfig();
}
} }
else
{
warnForMissingConfig();
}
return app;
} }
}
/// <summary>
/// Generate a warning if no configured domains were found
/// </summary>
/// <param name="app">The application builder</param>
private static void WarnForMissingConfig(IApplicationBuilder app)
{
var logger = GetService<ILogger<CanonicalDomainMiddleware>>(app);
if (logger is not null)
{
logger.LogWarning("No canonical domain configuration was found; no domains will be redirected");
}
}
}