436 lines
21 KiB
C#
436 lines
21 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Configuration;
|
|
using System.Web.Http;
|
|
using System.Web.Http.Dispatcher;
|
|
using System.Web.Mvc;
|
|
using System.Web.Routing;
|
|
using System.Web.Security;
|
|
using ClientDependency.Core.CompositeFiles.Providers;
|
|
using ClientDependency.Core.Config;
|
|
using Examine;
|
|
using Microsoft.AspNet.SignalR;
|
|
using Umbraco.Core;
|
|
using Umbraco.Core.Components;
|
|
using Umbraco.Core.Composing;
|
|
using Umbraco.Core.Configuration;
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
|
using Umbraco.Core.Dictionary;
|
|
using Umbraco.Core.Events;
|
|
using Umbraco.Core.Logging;
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
using Umbraco.Core.Profiling;
|
|
using Umbraco.Core.PropertyEditors;
|
|
using Umbraco.Core.PropertyEditors.ValueConverters;
|
|
using Umbraco.Core.Runtime;
|
|
using Umbraco.Core.Services;
|
|
using Umbraco.Web.Actions;
|
|
using Umbraco.Web.Cache;
|
|
using Umbraco.Web.Composing.Composers;
|
|
using Umbraco.Web.ContentApps;
|
|
using Umbraco.Web.Dictionary;
|
|
using Umbraco.Web.Editors;
|
|
using Umbraco.Web.Features;
|
|
using Umbraco.Web.HealthCheck;
|
|
using Umbraco.Web.Install;
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
using Umbraco.Web.Models.PublishedContent;
|
|
using Umbraco.Web.Mvc;
|
|
using Umbraco.Web.PublishedCache;
|
|
using Umbraco.Web.Routing;
|
|
using Umbraco.Web.Search;
|
|
using Umbraco.Web.Security;
|
|
using Umbraco.Web.Security.Providers;
|
|
using Umbraco.Web.Services;
|
|
using Umbraco.Web.SignalR;
|
|
using Umbraco.Web.Tour;
|
|
using Umbraco.Web.Trees;
|
|
using Umbraco.Web.UI.JavaScript;
|
|
using Umbraco.Web.WebApi;
|
|
|
|
using Current = Umbraco.Web.Composing.Current;
|
|
|
|
namespace Umbraco.Web.Runtime
|
|
{
|
|
[RequireComponent(typeof(CoreRuntimeComponent))]
|
|
public class WebRuntimeComponent : UmbracoComponentBase, IRuntimeComponent
|
|
{
|
|
public override void Compose(Composition composition)
|
|
{
|
|
base.Compose(composition);
|
|
|
|
composition.Register<UmbracoInjectedModule>();
|
|
|
|
composition.RegisterUnique<IHttpContextAccessor, AspNetHttpContextAccessor>(); // required for hybrid accessors
|
|
|
|
composition.ComposeWebMappingProfiles();
|
|
|
|
//register the install components
|
|
//NOTE: i tried to not have these registered if we weren't installing or upgrading but post install when the site restarts
|
|
//it still needs to use the install controller so we can't do that
|
|
composition.ComposeInstaller();
|
|
|
|
// register membership stuff
|
|
composition.Register(factory => Core.Security.MembershipProviderExtensions.GetMembersMembershipProvider());
|
|
composition.Register(factory => Roles.Enabled ? Roles.Provider : new MembersRoleProvider(factory.GetInstance<IMemberService>()));
|
|
composition.Register<MembershipHelper>();
|
|
|
|
// register accessors for cultures
|
|
composition.RegisterUnique<IDefaultCultureAccessor, DefaultCultureAccessor>();
|
|
composition.RegisterUnique<IVariationContextAccessor, HttpContextVariationContextAccessor>();
|
|
|
|
// register the http context and umbraco context accessors
|
|
// we *should* use the HttpContextUmbracoContextAccessor, however there are cases when
|
|
// we have no http context, eg when booting Umbraco or in background threads, so instead
|
|
// let's use an hybrid accessor that can fall back to a ThreadStatic context.
|
|
composition.RegisterUnique<IUmbracoContextAccessor, HybridUmbracoContextAccessor>();
|
|
|
|
// register a per-request HttpContextBase object
|
|
// is per-request so only one wrapper is created per request
|
|
composition.Register<HttpContextBase>(factory => new HttpContextWrapper(factory.GetInstance<IHttpContextAccessor>().HttpContext), Lifetime.Request);
|
|
|
|
// register the published snapshot accessor - the "current" published snapshot is in the umbraco context
|
|
composition.RegisterUnique<IPublishedSnapshotAccessor, UmbracoContextPublishedSnapshotAccessor>();
|
|
|
|
// we should stop injecting UmbracoContext and always inject IUmbracoContextAccessor, however at the moment
|
|
// there are tons of places (controllers...) which require UmbracoContext in their ctor - so let's register
|
|
// a way to inject the UmbracoContext - and register it per-request to be more efficient
|
|
//TODO: stop doing this
|
|
composition.Register(factory => factory.GetInstance<IUmbracoContextAccessor>().UmbracoContext, Lifetime.Request);
|
|
|
|
// register the umbraco helper
|
|
composition.RegisterUnique<UmbracoHelper>();
|
|
|
|
// register distributed cache
|
|
composition.RegisterUnique(f => new DistributedCache());
|
|
|
|
// replace some services
|
|
composition.RegisterUnique<IEventMessagesFactory, DefaultEventMessagesFactory>();
|
|
composition.RegisterUnique<IEventMessagesAccessor, HybridEventMessagesAccessor>();
|
|
composition.RegisterUnique<IApplicationTreeService, ApplicationTreeService>();
|
|
composition.RegisterUnique<ISectionService, SectionService>();
|
|
|
|
composition.RegisterUnique<IExamineManager>(factory => ExamineManager.Instance);
|
|
|
|
// configure the container for web
|
|
composition.ConfigureForWeb();
|
|
|
|
|
|
composition.RegisterUnique<Dashboards>();
|
|
|
|
composition
|
|
.ComposeUmbracoControllers(GetType().Assembly)
|
|
.SetDefaultRenderMvcController<RenderMvcController>(); // default controller for template views
|
|
|
|
composition.WithCollectionBuilder<SearchableTreeCollectionBuilder>()
|
|
.Add(() => composition.TypeLoader.GetTypes<ISearchableTree>()); // fixme which searchable trees?!
|
|
|
|
composition.WithCollectionBuilder<EditorValidatorCollectionBuilder>()
|
|
.Add(() => composition.TypeLoader.GetTypes<IEditorValidator>());
|
|
|
|
composition.WithCollectionBuilder<TourFilterCollectionBuilder>();
|
|
|
|
composition.RegisterUnique<UmbracoFeatures>();
|
|
|
|
composition.WithCollectionBuilder<ActionCollectionBuilder>()
|
|
.Add(() => composition.TypeLoader.GetTypes<IAction>());
|
|
|
|
var surfaceControllerTypes = new SurfaceControllerTypeCollection(composition.TypeLoader.GetSurfaceControllers());
|
|
composition.RegisterUnique(surfaceControllerTypes);
|
|
|
|
var umbracoApiControllerTypes = new UmbracoApiControllerTypeCollection(composition.TypeLoader.GetUmbracoApiControllers());
|
|
composition.RegisterUnique(umbracoApiControllerTypes);
|
|
|
|
// both TinyMceValueConverter (in Core) and RteMacroRenderingValueConverter (in Web) will be
|
|
// discovered when CoreBootManager configures the converters. We HAVE to remove one of them
|
|
// here because there cannot be two converters for one property editor - and we want the full
|
|
// RteMacroRenderingValueConverter that converts macros, etc. So remove TinyMceValueConverter.
|
|
// (the limited one, defined in Core, is there for tests) - same for others
|
|
composition.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>()
|
|
.Remove<TinyMceValueConverter>()
|
|
.Remove<TextStringValueConverter>()
|
|
.Remove<MarkdownEditorValueConverter>();
|
|
|
|
// add all known factories, devs can then modify this list on application
|
|
// startup either by binding to events or in their own global.asax
|
|
composition.WithCollectionBuilder<FilteredControllerFactoryCollectionBuilder>()
|
|
.Append<RenderControllerFactory>();
|
|
|
|
composition.WithCollectionBuilder<UrlProviderCollectionBuilder>()
|
|
.Append<AliasUrlProvider>()
|
|
.Append<DefaultUrlProvider>()
|
|
.Append<CustomRouteUrlProvider>();
|
|
|
|
composition.RegisterUnique<IContentLastChanceFinder, ContentFinderByLegacy404>();
|
|
|
|
composition.WithCollectionBuilder<ContentFinderCollectionBuilder>()
|
|
// all built-in finders in the correct order,
|
|
// devs can then modify this list on application startup
|
|
.Append<ContentFinderByPageIdQuery>()
|
|
.Append<ContentFinderByUrl>()
|
|
.Append<ContentFinderByIdPath>()
|
|
//.Append<ContentFinderByUrlAndTemplate>() // disabled, this is an odd finder
|
|
.Append<ContentFinderByUrlAlias>()
|
|
.Append<ContentFinderByRedirectUrl>();
|
|
|
|
composition.RegisterUnique<ISiteDomainHelper, SiteDomainHelper>();
|
|
|
|
composition.RegisterUnique<ICultureDictionaryFactory, DefaultCultureDictionaryFactory>();
|
|
|
|
// register *all* checks, except those marked [HideFromTypeFinder] of course
|
|
composition.WithCollectionBuilder<HealthCheckCollectionBuilder>()
|
|
.Add(() => composition.TypeLoader.GetTypes<HealthCheck.HealthCheck>());
|
|
|
|
composition.WithCollectionBuilder<HealthCheckNotificationMethodCollectionBuilder>()
|
|
.Add(() => composition.TypeLoader.GetTypes<HealthCheck.NotificationMethods.IHealthCheckNotificationMethod>());
|
|
|
|
// auto-register views
|
|
composition.RegisterAuto(typeof(UmbracoViewPage<>));
|
|
|
|
// register published router
|
|
composition.RegisterUnique<PublishedRouter>();
|
|
composition.Register(_ => UmbracoConfig.For.UmbracoSettings().WebRouting);
|
|
|
|
// register preview SignalR hub
|
|
composition.RegisterUnique(_ => GlobalHost.ConnectionManager.GetHubContext<PreviewHub>());
|
|
|
|
// register properties fallback
|
|
composition.RegisterUnique<IPublishedValueFallback, PublishedValueFallback>();
|
|
|
|
// register known content apps
|
|
composition.WithCollectionBuilder<ContentAppDefinitionCollectionBuilder>()
|
|
.Append<ListViewContentAppDefinition>()
|
|
.Append<ContentEditorContentAppDefinition>()
|
|
.Append<ContentInfoContentAppDefinition>();
|
|
}
|
|
|
|
internal void Initialize(
|
|
IRuntimeState runtime,
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
|
SurfaceControllerTypeCollection surfaceControllerTypes,
|
|
UmbracoApiControllerTypeCollection apiControllerTypes,
|
|
IPublishedSnapshotService publishedSnapshotService,
|
|
IUserService userService,
|
|
IUmbracoSettingsSection umbracoSettings,
|
|
IGlobalSettings globalSettings,
|
|
IEntityService entityService,
|
|
IVariationContextAccessor variationContextAccessor,
|
|
UrlProviderCollection urlProviders)
|
|
{
|
|
// setup mvc and webapi services
|
|
SetupMvcAndWebApi();
|
|
|
|
// client dependency
|
|
ConfigureClientDependency(globalSettings);
|
|
|
|
// Disable the X-AspNetMvc-Version HTTP Header
|
|
MvcHandler.DisableMvcResponseHeader = true;
|
|
|
|
InstallHelper.DeleteLegacyInstaller();
|
|
|
|
// wrap view engines in the profiling engine
|
|
WrapViewEngines(ViewEngines.Engines);
|
|
|
|
// add global filters
|
|
ConfigureGlobalFilters();
|
|
|
|
// set routes
|
|
CreateRoutes(umbracoContextAccessor, globalSettings, surfaceControllerTypes, apiControllerTypes);
|
|
|
|
// get an http context
|
|
// at that moment, HttpContext.Current != null but its .Request property is null
|
|
var httpContext = new HttpContextWrapper(HttpContext.Current);
|
|
|
|
// ensure there is an UmbracoContext
|
|
// (also sets the accessor)
|
|
// this is a *temp* UmbracoContext
|
|
UmbracoContext.EnsureContext(
|
|
umbracoContextAccessor,
|
|
new HttpContextWrapper(HttpContext.Current),
|
|
publishedSnapshotService,
|
|
new WebSecurity(httpContext, userService, globalSettings),
|
|
umbracoSettings,
|
|
urlProviders,
|
|
globalSettings,
|
|
variationContextAccessor);
|
|
|
|
// ensure WebAPI is initialized, after everything
|
|
GlobalConfiguration.Configuration.EnsureInitialized();
|
|
}
|
|
|
|
private static void ConfigureGlobalFilters()
|
|
{
|
|
GlobalFilters.Filters.Add(new EnsurePartialViewMacroViewContextFilterAttribute());
|
|
}
|
|
|
|
// internal for tests
|
|
internal static void WrapViewEngines(IList<IViewEngine> viewEngines)
|
|
{
|
|
if (viewEngines == null || viewEngines.Count == 0) return;
|
|
|
|
var originalEngines = viewEngines.Select(e => e).ToArray();
|
|
viewEngines.Clear();
|
|
foreach (var engine in originalEngines)
|
|
{
|
|
var wrappedEngine = engine is ProfilingViewEngine ? engine : new ProfilingViewEngine(engine);
|
|
viewEngines.Add(wrappedEngine);
|
|
}
|
|
}
|
|
|
|
// internal for tests
|
|
internal static void CreateRoutes(
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
|
IGlobalSettings globalSettings,
|
|
SurfaceControllerTypeCollection surfaceControllerTypes,
|
|
UmbracoApiControllerTypeCollection apiControllerTypes)
|
|
{
|
|
var umbracoPath = globalSettings.GetUmbracoMvcArea();
|
|
|
|
// create the front-end route
|
|
var defaultRoute = RouteTable.Routes.MapRoute(
|
|
"Umbraco_default",
|
|
umbracoPath + "/RenderMvc/{action}/{id}",
|
|
new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional }
|
|
);
|
|
defaultRoute.RouteHandler = new RenderRouteHandler(umbracoContextAccessor, ControllerBuilder.Current.GetControllerFactory());
|
|
|
|
// register install routes
|
|
RouteTable.Routes.RegisterArea<UmbracoInstallArea>();
|
|
|
|
// register all back office routes
|
|
RouteTable.Routes.RegisterArea(new BackOfficeArea(globalSettings));
|
|
|
|
// plugin controllers must come first because the next route will catch many things
|
|
RoutePluginControllers(globalSettings, surfaceControllerTypes, apiControllerTypes);
|
|
}
|
|
|
|
private static void RoutePluginControllers(
|
|
IGlobalSettings globalSettings,
|
|
SurfaceControllerTypeCollection surfaceControllerTypes,
|
|
UmbracoApiControllerTypeCollection apiControllerTypes)
|
|
{
|
|
var umbracoPath = globalSettings.GetUmbracoMvcArea();
|
|
|
|
// need to find the plugin controllers and route them
|
|
var pluginControllers = surfaceControllerTypes.Concat(apiControllerTypes).ToArray();
|
|
|
|
// local controllers do not contain the attribute
|
|
var localControllers = pluginControllers.Where(x => PluginController.GetMetadata(x).AreaName.IsNullOrWhiteSpace());
|
|
foreach (var s in localControllers)
|
|
{
|
|
if (TypeHelper.IsTypeAssignableFrom<SurfaceController>(s))
|
|
RouteLocalSurfaceController(s, umbracoPath);
|
|
else if (TypeHelper.IsTypeAssignableFrom<UmbracoApiController>(s))
|
|
RouteLocalApiController(s, umbracoPath);
|
|
}
|
|
|
|
// get the plugin controllers that are unique to each area (group by)
|
|
var pluginSurfaceControlleres = pluginControllers.Where(x => PluginController.GetMetadata(x).AreaName.IsNullOrWhiteSpace() == false);
|
|
var groupedAreas = pluginSurfaceControlleres.GroupBy(controller => PluginController.GetMetadata(controller).AreaName);
|
|
// loop through each area defined amongst the controllers
|
|
foreach (var g in groupedAreas)
|
|
{
|
|
// create & register an area for the controllers (this will throw an exception if all controllers are not in the same area)
|
|
var pluginControllerArea = new PluginControllerArea(globalSettings, g.Select(PluginController.GetMetadata));
|
|
RouteTable.Routes.RegisterArea(pluginControllerArea);
|
|
}
|
|
}
|
|
|
|
private static void RouteLocalApiController(Type controller, string umbracoPath)
|
|
{
|
|
var meta = PluginController.GetMetadata(controller);
|
|
var url = umbracoPath + (meta.IsBackOffice ? "/BackOffice" : "") + "/Api/" + meta.ControllerName + "/{action}/{id}";
|
|
var route = RouteTable.Routes.MapHttpRoute(
|
|
$"umbraco-api-{meta.ControllerName}",
|
|
url, // url to match
|
|
new { controller = meta.ControllerName, id = UrlParameter.Optional },
|
|
new[] { meta.ControllerNamespace });
|
|
if (route.DataTokens == null) // web api routes don't set the data tokens object
|
|
route.DataTokens = new RouteValueDictionary();
|
|
route.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, "api"); //ensure the umbraco token is set
|
|
}
|
|
|
|
private static void RouteLocalSurfaceController(Type controller, string umbracoPath)
|
|
{
|
|
var meta = PluginController.GetMetadata(controller);
|
|
var url = umbracoPath + "/Surface/" + meta.ControllerName + "/{action}/{id}";
|
|
var route = RouteTable.Routes.MapRoute(
|
|
$"umbraco-surface-{meta.ControllerName}",
|
|
url, // url to match
|
|
new { controller = meta.ControllerName, action = "Index", id = UrlParameter.Optional },
|
|
new[] { meta.ControllerNamespace }); // look in this namespace to create the controller
|
|
route.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, "surface"); // ensure the umbraco token is set
|
|
route.DataTokens.Add("UseNamespaceFallback", false); // don't look anywhere else except this namespace!
|
|
// make it use our custom/special SurfaceMvcHandler
|
|
route.RouteHandler = new SurfaceRouteHandler();
|
|
}
|
|
|
|
private static void SetupMvcAndWebApi()
|
|
{
|
|
//don't output the MVC version header (security)
|
|
MvcHandler.DisableMvcResponseHeader = true;
|
|
|
|
// set master controller factory
|
|
var controllerFactory = new MasterControllerFactory(() => Current.FilteredControllerFactories);
|
|
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
|
|
|
|
// set the render & plugin view engines
|
|
ViewEngines.Engines.Add(new RenderViewEngine());
|
|
ViewEngines.Engines.Add(new PluginViewEngine());
|
|
|
|
//set model binder
|
|
ModelBinderProviders.BinderProviders.Add(ContentModelBinder.Instance); // is a provider
|
|
|
|
////add the profiling action filter
|
|
//GlobalFilters.Filters.Add(new ProfilingActionFilter());
|
|
|
|
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector),
|
|
new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration));
|
|
}
|
|
|
|
private static void ConfigureClientDependency(IGlobalSettings globalSettings)
|
|
{
|
|
// Backwards compatibility - set the path and URL type for ClientDependency 1.5.1 [LK]
|
|
XmlFileMapper.FileMapDefaultFolder = "~/App_Data/TEMP/ClientDependency";
|
|
BaseCompositeFileProcessingProvider.UrlTypeDefault = CompositeUrlType.Base64QueryStrings;
|
|
|
|
// Now we need to detect if we are running umbracoLocalTempStorage as EnvironmentTemp and in that case we want to change the CDF file
|
|
// location to be there
|
|
if (globalSettings.LocalTempStorageLocation == LocalTempStorage.EnvironmentTemp)
|
|
{
|
|
var appDomainHash = HttpRuntime.AppDomainAppId.ToSHA1();
|
|
var cachePath = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "UmbracoData",
|
|
//include the appdomain hash is just a safety check, for example if a website is moved from worker A to worker B and then back
|
|
// to worker A again, in theory the %temp% folder should already be empty but we really want to make sure that its not
|
|
// utilizing an old path
|
|
appDomainHash);
|
|
|
|
//set the file map and composite file default location to the %temp% location
|
|
BaseCompositeFileProcessingProvider.CompositeFilePathDefaultFolder
|
|
= XmlFileMapper.FileMapDefaultFolder
|
|
= Path.Combine(cachePath, "ClientDependency");
|
|
}
|
|
|
|
if (ConfigurationManager.GetSection("system.web/httpRuntime") is HttpRuntimeSection section)
|
|
{
|
|
//set the max url length for CDF to be the smallest of the max query length, max request length
|
|
ClientDependency.Core.CompositeFiles.CompositeDependencyHandler.MaxHandlerUrlLength = Math.Min(section.MaxQueryStringLength, section.MaxRequestLength);
|
|
}
|
|
|
|
//Register a custom renderer - used to process property editor dependencies
|
|
var renderer = new DependencyPathRenderer();
|
|
renderer.Initialize("Umbraco.DependencyPathRenderer", new NameValueCollection
|
|
{
|
|
{ "compositeFileHandlerPath", ClientDependencySettings.Instance.CompositeFileHandlerPath }
|
|
});
|
|
|
|
ClientDependencySettings.Instance.MvcRendererCollection.Add(renderer);
|
|
}
|
|
}
|
|
}
|