AB#6233 - Handle ApplicationUrl

This commit is contained in:
Bjarke Berg
2020-05-07 09:34:16 +02:00
parent 269c75e26b
commit 8f7c022f2d
34 changed files with 169 additions and 141 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Web.Common.Extensions;
using Umbraco.Web.Common.Lifetime;
using Umbraco.Web.Routing;
@@ -11,11 +13,18 @@ namespace Umbraco.Web.Common.AspNetCore
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebRoutingSettings _webRoutingSettings;
private readonly ISet<string> _applicationUrls = new HashSet<string>();
private Uri _currentApplicationUrl;
public AspNetCoreRequestAccessor(IHttpContextAccessor httpContextAccessor, IUmbracoRequestLifetime umbracoRequestLifetime, IUmbracoContextAccessor umbracoContextAccessor)
public AspNetCoreRequestAccessor(IHttpContextAccessor httpContextAccessor,
IUmbracoRequestLifetime umbracoRequestLifetime,
IUmbracoContextAccessor umbracoContextAccessor,
IWebRoutingSettings webRoutingSettings)
{
_httpContextAccessor = httpContextAccessor;
_umbracoContextAccessor = umbracoContextAccessor;
_webRoutingSettings = webRoutingSettings;
umbracoRequestLifetime.RequestStart += RequestStart;
umbracoRequestLifetime.RequestEnd += RequestEnd;
@@ -43,5 +52,36 @@ namespace Umbraco.Web.Common.AspNetCore
public event EventHandler<RoutableAttemptEventArgs> RouteAttempt;
public Uri GetRequestUrl() => _httpContextAccessor.HttpContext != null ? new Uri(_httpContextAccessor.HttpContext.Request.GetEncodedUrl()) : null;
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
// it changes the URL to `localhost:80` which actually doesn't work for pinging itself, it only works internally in Azure. The ironic part
// about this is that this is here specifically for the slot swap scenario https://issues.umbraco.org/issue/U4-10626
// 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))
{
return new Uri(_webRoutingSettings.UmbracoApplicationUrl);
}
var request = _httpContextAccessor.HttpContext?.Request;
if (request is null) return _currentApplicationUrl;
var url = UriHelper.BuildAbsolute(request.Scheme, request.Host);
var change = url != null && !_applicationUrls.Contains(url);
if (change)
{
_applicationUrls.Add(url);
_currentApplicationUrl ??= new Uri(url);
}
return _currentApplicationUrl;
}
}
}