|
|
|
@ -9,24 +9,21 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Squidex.Config; |
|
|
|
|
|
|
|
namespace Squidex.Pipeline |
|
|
|
{ |
|
|
|
public sealed class SingleUrlsMiddleware |
|
|
|
public sealed class EnforceHttpsMiddleware |
|
|
|
{ |
|
|
|
private readonly RequestDelegate next; |
|
|
|
private readonly IOptions<MyUrlsOptions> urls; |
|
|
|
private readonly ILogger<SingleUrlsMiddleware> logger; |
|
|
|
|
|
|
|
public SingleUrlsMiddleware(RequestDelegate next, ILoggerFactory factory, IOptions<MyUrlsOptions> urls) |
|
|
|
public EnforceHttpsMiddleware(RequestDelegate next, IOptions<MyUrlsOptions> urls) |
|
|
|
{ |
|
|
|
this.next = next; |
|
|
|
this.urls = urls; |
|
|
|
|
|
|
|
logger = factory.CreateLogger<SingleUrlsMiddleware>(); |
|
|
|
this.urls = urls; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task Invoke(HttpContext context) |
|
|
|
@ -37,19 +34,11 @@ namespace Squidex.Pipeline |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var currentUrl = string.Concat(context.Request.Scheme, "://", context.Request.Host, context.Request.Path); |
|
|
|
|
|
|
|
var hostName = context.Request.Host.ToString().ToLowerInvariant(); |
|
|
|
if (hostName.StartsWith("www.")) |
|
|
|
{ |
|
|
|
hostName = hostName.Substring(4); |
|
|
|
} |
|
|
|
|
|
|
|
var newUrl = string.Concat("https://", hostName, context.Request.Path); |
|
|
|
|
|
|
|
if (!string.Equals(newUrl, currentUrl, StringComparison.OrdinalIgnoreCase)) |
|
|
|
if (!string.Equals(context.Request.Scheme, "https", StringComparison.OrdinalIgnoreCase)) |
|
|
|
{ |
|
|
|
logger.LogError("Invalid url: {0} instead {1}", currentUrl, newUrl); |
|
|
|
var newUrl = string.Concat("https://", hostName, context.Request.Path); |
|
|
|
|
|
|
|
context.Response.Redirect(newUrl + context.Request.QueryString, true); |
|
|
|
} |