Merge remote-tracking branch 'origin/release/10.3' into release/10.3

This commit is contained in:
Zeegaan
2022-10-13 08:26:34 +02:00
3 changed files with 51 additions and 11 deletions

View File

@@ -0,0 +1,26 @@
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Web;
namespace Umbraco.Cms.Web.Common.AspNetCore;
/// <summary>
/// Notification handler which will listen to the <see cref="UmbracoRequestBeginNotification"/>, and ensure that
/// the applicationUrl is set on the first request.
/// </summary>
internal class ApplicationUrlRequestBeginNotificationHandler : INotificationHandler<UmbracoRequestBeginNotification>
{
private readonly IRequestAccessor _requestAccessor;
public ApplicationUrlRequestBeginNotificationHandler(IRequestAccessor requestAccessor) =>
_requestAccessor = requestAccessor;
public void Handle(UmbracoRequestBeginNotification notification)
{
// If someone has replaced the AspNetCoreRequestAccessor we'll do nothing and assume they handle it themselves.
if (_requestAccessor is AspNetCoreRequestAccessor accessor)
{
accessor.EnsureApplicationUrl();
}
}
}

View File

@@ -9,7 +9,7 @@ using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.AspNetCore;
public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<UmbracoRequestBeginNotification>
public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<UmbracoRequestBeginNotification>, IDisposable
{
private readonly ISet<string> _applicationUrls = new HashSet<string>();
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -18,6 +18,7 @@ public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<
private object _initLocker = new();
private bool _isInit;
private WebRoutingSettings _webRoutingSettings;
private readonly IDisposable? _onChangeDisposable;
/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreRequestAccessor" /> class.
@@ -28,20 +29,19 @@ public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<
{
_httpContextAccessor = httpContextAccessor;
_webRoutingSettings = webRoutingSettings.CurrentValue;
webRoutingSettings.OnChange(x => _webRoutingSettings = x);
_onChangeDisposable = webRoutingSettings.OnChange(x => _webRoutingSettings = x);
}
/// <summary>
/// This just initializes the application URL on first request attempt
/// TODO: This doesn't belong here, the GetApplicationUrl doesn't belong to IRequestAccessor
/// this should be part of middleware not a lazy init based on an INotification
/// <para>
/// This is now a NoOp, and is no longer used, instead ApplicationUrlRequestBeginNotificationHandler is used
/// </para>
/// </summary>
[Obsolete("This is no longer used, AspNetCoreRequestAccessor will no longer implement INotificationHandler in V12, see ApplicationUrlRequestBeginNotificationHandler instead.")]
public void Handle(UmbracoRequestBeginNotification notification)
=> LazyInitializer.EnsureInitialized(ref _hasAppUrl, ref _isInit, ref _initLocker, () =>
{
GetApplicationUrl();
return true;
});
{
// NoOP
}
/// <inheritdoc />
public string GetRequestValue(string name) => GetFormValue(name) ?? GetQueryStringValue(name);
@@ -54,6 +54,17 @@ public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<
? new Uri(_httpContextAccessor.HttpContext.Request.GetEncodedUrl())
: null;
/// <summary>
/// Ensure that the ApplicationUrl is set on the first request, this is using a LazyInitializer, so the code will only be run the first time
/// </summary>
/// TODO: This doesn't belong here, the GetApplicationUrl doesn't belong to IRequestAccessor
internal void EnsureApplicationUrl() =>
LazyInitializer.EnsureInitialized(ref _hasAppUrl, ref _isInit, ref _initLocker, () =>
{
GetApplicationUrl();
return true;
});
public Uri? GetApplicationUrl()
{
// Fixme: This causes problems with site swap on azure because azure pre-warms a site by calling into `localhost` and when it does that
@@ -63,7 +74,7 @@ public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<
// see U4-10626 - in some cases we want to reset the application url
// (this is a simplified version of what was in 7.x)
// note: should this be optional? is it expensive?
if (!(_webRoutingSettings.UmbracoApplicationUrl is null))
if (_webRoutingSettings.UmbracoApplicationUrl is not null)
{
return new Uri(_webRoutingSettings.UmbracoApplicationUrl);
}
@@ -96,4 +107,6 @@ public class AspNetCoreRequestAccessor : IRequestAccessor, INotificationHandler<
return request.Form[name];
}
public void Dispose() => _onChangeDisposable?.Dispose();
}

View File

@@ -298,6 +298,7 @@ public static partial class UmbracoBuilderExtensions
// AspNetCore specific services
builder.Services.AddUnique<IRequestAccessor, AspNetCoreRequestAccessor>();
builder.AddNotificationHandler<UmbracoRequestBeginNotification, AspNetCoreRequestAccessor>();
builder.AddNotificationHandler<UmbracoRequestBeginNotification, ApplicationUrlRequestBeginNotificationHandler>();
// Password hasher
builder.Services.AddUnique<IPasswordHasher, AspNetCorePasswordHasher>();