2022-04-25 15:27:53 +01:00
|
|
|
using Microsoft.AspNetCore.DataProtection.Infrastructure;
|
2020-02-18 08:32:06 +01:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2022-04-25 15:27:53 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-08-21 14:52:47 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2021-08-03 14:04:19 +02:00
|
|
|
using Umbraco.Cms.Core.Collections;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Configuration;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2022-04-25 15:27:53 +01:00
|
|
|
using Umbraco.Cms.Core.Extensions;
|
2021-08-12 12:08:39 +03:00
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
|
2020-02-18 08:32:06 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.AspNetCore;
|
|
|
|
|
|
|
|
|
|
public class AspNetCoreHostingEnvironment : IHostingEnvironment
|
2020-02-18 08:32:06 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
private readonly IApplicationDiscriminator? _applicationDiscriminator;
|
|
|
|
|
private readonly ConcurrentHashSet<Uri> _applicationUrls = new();
|
|
|
|
|
private readonly IOptionsMonitor<HostingSettings> _hostingSettings;
|
|
|
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
|
|
|
private readonly IOptionsMonitor<WebRoutingSettings> _webRoutingSettings;
|
|
|
|
|
|
|
|
|
|
private readonly UrlMode _urlProviderMode;
|
|
|
|
|
|
|
|
|
|
private string? _applicationId;
|
|
|
|
|
private string? _localTempPath;
|
|
|
|
|
|
|
|
|
|
[Obsolete("Please use an alternative constructor.")]
|
|
|
|
|
public AspNetCoreHostingEnvironment(
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
IOptionsMonitor<HostingSettings> hostingSettings,
|
|
|
|
|
IOptionsMonitor<WebRoutingSettings> webRoutingSettings,
|
|
|
|
|
IWebHostEnvironment webHostEnvironment)
|
2022-04-29 15:02:36 +02:00
|
|
|
: this(hostingSettings, webRoutingSettings, webHostEnvironment, serviceProvider.GetService<IApplicationDiscriminator>()!)
|
2022-05-09 09:39:46 +02:00
|
|
|
{
|
|
|
|
|
}
|
2022-04-25 15:27:53 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
public AspNetCoreHostingEnvironment(
|
|
|
|
|
IOptionsMonitor<HostingSettings> hostingSettings,
|
|
|
|
|
IOptionsMonitor<WebRoutingSettings> webRoutingSettings,
|
|
|
|
|
IWebHostEnvironment webHostEnvironment,
|
|
|
|
|
IApplicationDiscriminator applicationDiscriminator)
|
|
|
|
|
: this(hostingSettings, webRoutingSettings, webHostEnvironment) =>
|
|
|
|
|
_applicationDiscriminator = applicationDiscriminator;
|
|
|
|
|
|
|
|
|
|
public AspNetCoreHostingEnvironment(
|
|
|
|
|
IOptionsMonitor<HostingSettings> hostingSettings,
|
|
|
|
|
IOptionsMonitor<WebRoutingSettings> webRoutingSettings,
|
|
|
|
|
IWebHostEnvironment webHostEnvironment)
|
|
|
|
|
{
|
|
|
|
|
_hostingSettings = hostingSettings ?? throw new ArgumentNullException(nameof(hostingSettings));
|
|
|
|
|
_webRoutingSettings = webRoutingSettings ?? throw new ArgumentNullException(nameof(webRoutingSettings));
|
|
|
|
|
_webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
|
|
|
|
|
_urlProviderMode = _webRoutingSettings.CurrentValue.UrlProviderMode;
|
2020-02-20 07:55:24 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
SetSiteName(hostingSettings.CurrentValue.SiteName);
|
2022-01-31 10:12:02 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// We have to ensure that the OptionsMonitor is an actual options monitor since we have a hack
|
|
|
|
|
// where we initially use an OptionsMonitorAdapter, which doesn't implement OnChange.
|
|
|
|
|
// See summery of OptionsMonitorAdapter for more information.
|
|
|
|
|
if (hostingSettings is OptionsMonitor<HostingSettings>)
|
|
|
|
|
{
|
|
|
|
|
hostingSettings.OnChange(settings => SetSiteName(settings.SiteName));
|
|
|
|
|
}
|
2022-01-31 10:12:02 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
ApplicationPhysicalPath = webHostEnvironment.ContentRootPath;
|
2021-09-09 11:18:12 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
if (_webRoutingSettings.CurrentValue.UmbracoApplicationUrl is not null)
|
|
|
|
|
{
|
|
|
|
|
ApplicationMainUrl = new Uri(_webRoutingSettings.CurrentValue.UmbracoApplicationUrl);
|
2020-02-18 08:32:06 +01:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scheduled for removal in v12
|
|
|
|
|
[Obsolete("This will never have a value")]
|
|
|
|
|
public Version? IISVersion { get; }
|
2020-03-25 15:06:22 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool IsHosted { get; } = true;
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Uri ApplicationMainUrl { get; private set; } = null!;
|
2021-02-08 11:00:15 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string SiteName { get; private set; } = null!;
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string ApplicationId
|
|
|
|
|
{
|
|
|
|
|
get
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
if (_applicationId != null)
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
{
|
|
|
|
|
return _applicationId;
|
|
|
|
|
}
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
_applicationId = _applicationDiscriminator?.GetApplicationId() ??
|
|
|
|
|
_webHostEnvironment.GetTemporaryApplicationId();
|
|
|
|
|
|
|
|
|
|
return _applicationId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string ApplicationPhysicalPath { get; }
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// TODO how to find this, This is a server thing, not application thing.
|
|
|
|
|
public string ApplicationVirtualPath =>
|
|
|
|
|
_hostingSettings.CurrentValue.ApplicationVirtualPath?.EnsureStartsWith('/') ?? "/";
|
2020-02-27 11:10:43 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool IsDebugMode => _hostingSettings.CurrentValue.Debug;
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
public string LocalTempPath
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-02-25 09:45:56 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
if (_localTempPath != null)
|
2020-02-25 09:45:56 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
return _localTempPath;
|
2020-02-25 09:45:56 +01:00
|
|
|
}
|
2020-02-27 11:10:43 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
switch (_hostingSettings.CurrentValue.LocalTempStorageLocation)
|
|
|
|
|
{
|
|
|
|
|
case LocalTempStorage.EnvironmentTemp:
|
2020-12-08 10:42:26 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// environment temp is unique, we need a folder per site
|
2020-05-04 14:40:11 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// use a hash
|
|
|
|
|
// combine site name and application id
|
|
|
|
|
// site name is a Guid on Cloud
|
|
|
|
|
// application id is eg /LM/W3SVC/123456/ROOT
|
|
|
|
|
// the combination is unique on one server
|
|
|
|
|
// and, if a site moves from worker A to B and then back to A...
|
|
|
|
|
// hopefully it gets a new Guid or new application id?
|
|
|
|
|
var hashString = SiteName + "::" + ApplicationId;
|
|
|
|
|
var hash = hashString.GenerateHash();
|
|
|
|
|
var siteTemp = Path.Combine(Path.GetTempPath(), "UmbracoData", hash);
|
2020-04-03 09:13:55 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
return _localTempPath = siteTemp;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
return _localTempPath = MapPathContentRoot(Core.Constants.SystemDirectories.TempData);
|
2020-12-08 10:42:26 +11:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string MapPathWebRoot(string path) => _webHostEnvironment.MapPathWebRoot(path);
|
2020-02-27 11:48:38 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string MapPathContentRoot(string path) => _webHostEnvironment.MapPathContentRoot(path);
|
2020-02-27 11:48:38 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string ToAbsolute(string virtualPath)
|
|
|
|
|
{
|
|
|
|
|
if (!virtualPath.StartsWith("~/") && !virtualPath.StartsWith("/") && _urlProviderMode != UrlMode.Absolute)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"The value {virtualPath} for parameter {nameof(virtualPath)} must start with ~/ or /");
|
2020-02-18 08:32:06 +01:00
|
|
|
}
|
2021-02-08 11:00:15 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// will occur if it starts with "/"
|
|
|
|
|
if (Uri.IsWellFormedUriString(virtualPath, UriKind.Absolute))
|
2021-02-08 11:00:15 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
return virtualPath;
|
|
|
|
|
}
|
2021-02-08 11:00:15 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
var fullPath = ApplicationVirtualPath.EnsureEndsWith('/') +
|
|
|
|
|
virtualPath.TrimStart(Core.Constants.CharArrays.TildeForwardSlash);
|
2021-02-08 11:00:15 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
return fullPath;
|
|
|
|
|
}
|
2021-09-09 11:18:12 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
public void EnsureApplicationMainUrl(Uri? currentApplicationUrl)
|
|
|
|
|
{
|
|
|
|
|
// 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 (currentApplicationUrl is null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-08 11:00:15 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
if (_webRoutingSettings.CurrentValue.UmbracoApplicationUrl is not null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-08 11:00:15 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
var change = !_applicationUrls.Contains(currentApplicationUrl);
|
|
|
|
|
if (change)
|
|
|
|
|
{
|
|
|
|
|
if (_applicationUrls.TryAdd(currentApplicationUrl))
|
2021-02-08 11:00:15 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
ApplicationMainUrl = currentApplicationUrl;
|
2021-02-08 11:00:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-31 10:12:02 +01:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
|
|
|
|
|
private void SetSiteName(string? siteName) =>
|
|
|
|
|
SiteName = string.IsNullOrWhiteSpace(siteName)
|
|
|
|
|
? _webHostEnvironment.ApplicationName
|
|
|
|
|
: siteName;
|
2020-02-18 08:32:06 +01:00
|
|
|
}
|