From 606ebcae928f02e41a79f8414e34bd42f8f69f7f Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 24 Mar 2017 11:07:15 +0100 Subject: [PATCH] U4-9604 - fix current HttpContext in ManyObjectsResolverBase --- .../ManyObjectsResolverBase.cs | 32 ++++++++----------- .../Checks/Security/ClickJackingCheck.cs | 3 +- .../Checks/Security/ExcessiveHeadersCheck.cs | 3 +- .../HealthCheck/HealthCheckController.cs | 12 ++++++- .../HealthCheck/HealthCheckResolver.cs | 4 +-- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Core/ObjectResolution/ManyObjectsResolverBase.cs b/src/Umbraco.Core/ObjectResolution/ManyObjectsResolverBase.cs index 5e170f47f4..e54e6f550b 100644 --- a/src/Umbraco.Core/ObjectResolution/ManyObjectsResolverBase.cs +++ b/src/Umbraco.Core/ObjectResolution/ManyObjectsResolverBase.cs @@ -22,6 +22,7 @@ namespace Umbraco.Core.ObjectResolution private readonly string _httpContextKey; private readonly List _instanceTypes = new List(); private IEnumerable _sortedValues; + private readonly Func _httpContextGetter; private int _defaultPluginWeight = 100; @@ -42,12 +43,7 @@ namespace Umbraco.Core.ObjectResolution if (logger == null) throw new ArgumentNullException("logger"); CanResolveBeforeFrozen = false; if (scope == ObjectLifetimeScope.HttpRequest) - { - if (HttpContext.Current == null) - throw new InvalidOperationException("Use alternative constructor accepting a HttpContextBase object in order to set the lifetime scope to HttpRequest when HttpContext.Current is null"); - - CurrentHttpContext = new HttpContextWrapper(HttpContext.Current); - } + _httpContextGetter = () => new HttpContextWrapper(HttpContext.Current); ServiceProvider = serviceProvider; Logger = logger; @@ -84,7 +80,7 @@ namespace Umbraco.Core.ObjectResolution LifetimeScope = ObjectLifetimeScope.HttpRequest; _httpContextKey = GetType().FullName; ServiceProvider = serviceProvider; - CurrentHttpContext = httpContext; + _httpContextGetter = () => httpContext; _instanceTypes = new List(); InitializeAppInstances(); @@ -160,7 +156,7 @@ namespace Umbraco.Core.ObjectResolution /// Gets or sets the used to initialize this object, if any. /// /// If not null, then LifetimeScope will be ObjectLifetimeScope.HttpRequest. - protected HttpContextBase CurrentHttpContext { get; private set; } + protected HttpContextBase CurrentHttpContext { get { return _httpContextGetter == null ? null : _httpContextGetter(); } } /// /// Returns the service provider used to instantiate objects @@ -196,7 +192,7 @@ namespace Umbraco.Core.ObjectResolution /// /// Gets or sets the default type weight. /// - /// Determines the weight of types that do not have a WeightAttribute set on + /// Determines the weight of types that do not have a WeightAttribute set on /// them, when calling GetSortedValues. protected virtual int DefaultPluginWeight { @@ -276,7 +272,7 @@ namespace Umbraco.Core.ObjectResolution /// Removes a type. /// /// The type to remove. - /// the resolver does not support removing types, or + /// the resolver does not support removing types, or /// the type is not a valid type for the resolver. public virtual void RemoveType(Type value) { @@ -296,7 +292,7 @@ namespace Umbraco.Core.ObjectResolution /// Removes a type. /// /// The type to remove. - /// the resolver does not support removing types, or + /// the resolver does not support removing types, or /// the type is not a valid type for the resolver. public void RemoveType() where T : TResolved @@ -309,7 +305,7 @@ namespace Umbraco.Core.ObjectResolution /// /// The types to add. /// The types are appended at the end of the list. - /// the resolver does not support adding types, or + /// the resolver does not support adding types, or /// a type is not a valid type for the resolver, or a type is already in the collection of types. protected void AddTypes(IEnumerable types) { @@ -336,7 +332,7 @@ namespace Umbraco.Core.ObjectResolution /// /// The type to add. /// The type is appended at the end of the list. - /// the resolver does not support adding types, or + /// the resolver does not support adding types, or /// the type is not a valid type for the resolver, or the type is already in the collection of types. public virtual void AddType(Type value) { @@ -362,7 +358,7 @@ namespace Umbraco.Core.ObjectResolution /// /// The type to add. /// The type is appended at the end of the list. - /// the resolver does not support adding types, or + /// the resolver does not support adding types, or /// the type is not a valid type for the resolver, or the type is already in the collection of types. public void AddType() where T : TResolved @@ -404,7 +400,7 @@ namespace Umbraco.Core.ObjectResolution /// /// The zero-based index at which the type should be inserted. /// The type to insert. - /// the resolver does not support inserting types, or + /// the resolver does not support inserting types, or /// the type is not a valid type for the resolver, or the type is already in the collection of types. /// is out of range. public virtual void InsertType(int index, Type value) @@ -430,7 +426,7 @@ namespace Umbraco.Core.ObjectResolution /// Inserts a type at the beginning of the list. /// /// The type to insert. - /// the resolver does not support inserting types, or + /// the resolver does not support inserting types, or /// the type is not a valid type for the resolver, or the type is already in the collection of types. public virtual void InsertType(Type value) { @@ -464,7 +460,7 @@ namespace Umbraco.Core.ObjectResolution /// /// The existing type before which to insert. /// The type to insert. - /// the resolver does not support inserting types, or + /// the resolver does not support inserting types, or /// one of the types is not a valid type for the resolver, or the existing type is not in the collection, /// or the new type is already in the collection of types. public virtual void InsertTypeBefore(Type existingType, Type value) @@ -498,7 +494,7 @@ namespace Umbraco.Core.ObjectResolution /// /// The existing type before which to insert. /// The type to insert. - /// the resolver does not support inserting types, or + /// the resolver does not support inserting types, or /// one of the types is not a valid type for the resolver, or the existing type is not in the collection, /// or the new type is already in the collection of types. public void InsertTypeBefore() diff --git a/src/Umbraco.Web/HealthCheck/Checks/Security/ClickJackingCheck.cs b/src/Umbraco.Web/HealthCheck/Checks/Security/ClickJackingCheck.cs index bdfd504d73..54f72dc88a 100644 --- a/src/Umbraco.Web/HealthCheck/Checks/Security/ClickJackingCheck.cs +++ b/src/Umbraco.Web/HealthCheck/Checks/Security/ClickJackingCheck.cs @@ -64,7 +64,8 @@ namespace Umbraco.Web.HealthCheck.Checks.Security var url = HealthCheckContext.HttpContext.Request.Url; // Access the site home page and check for the click-jack protection header or meta tag - var useSsl = GlobalSettings.UseSSL || HealthCheckContext.HttpContext.Request.ServerVariables["SERVER_PORT"] == "443"; + var serverVariables = HealthCheckContext.HttpContext.Request.ServerVariables; + var useSsl = GlobalSettings.UseSSL || serverVariables["SERVER_PORT"] == "443"; var address = string.Format("http{0}://{1}:{2}", useSsl ? "s" : "", url.Host.ToLower(), url.Port); var request = WebRequest.Create(address); request.Method = "GET"; diff --git a/src/Umbraco.Web/HealthCheck/Checks/Security/ExcessiveHeadersCheck.cs b/src/Umbraco.Web/HealthCheck/Checks/Security/ExcessiveHeadersCheck.cs index cf279bf3f8..2ca63662d8 100644 --- a/src/Umbraco.Web/HealthCheck/Checks/Security/ExcessiveHeadersCheck.cs +++ b/src/Umbraco.Web/HealthCheck/Checks/Security/ExcessiveHeadersCheck.cs @@ -49,7 +49,8 @@ namespace Umbraco.Web.HealthCheck.Checks.Security var url = HealthCheckContext.HttpContext.Request.Url; // Access the site home page and check for the headers - var useSsl = GlobalSettings.UseSSL || HealthCheckContext.HttpContext.Request.ServerVariables["SERVER_PORT"] == "443"; + var serverVariables = HealthCheckContext.HttpContext.Request.ServerVariables; + var useSsl = GlobalSettings.UseSSL || serverVariables["SERVER_PORT"] == "443"; var address = string.Format("http{0}://{1}:{2}", useSsl ? "s" : "", url.Host.ToLower(), url.Port); var request = WebRequest.Create(address); request.Method = "HEAD"; diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckController.cs b/src/Umbraco.Web/HealthCheck/HealthCheckController.cs index 919df88962..14bfaaea9f 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckController.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckController.cs @@ -48,12 +48,22 @@ namespace Umbraco.Web.HealthCheck return healthCheckGroups; } + [HttpGet] public object GetStatus(Guid id) { var check = _healthCheckResolver.HealthChecks.FirstOrDefault(x => x.Id == id); if (check == null) throw new InvalidOperationException("No health check found with ID " + id); - return check.GetStatus(); + try + { + //Core.Logging.LogHelper.Debug("Running health check: " + check.Name); + return check.GetStatus(); + } + catch (Exception e) + { + Core.Logging.LogHelper.Error("Exception in health check: " + check.Name, e); + throw; + } } [HttpPost] diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs index dfe5b792a5..7ae302fa49 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs @@ -19,7 +19,7 @@ namespace Umbraco.Web.HealthCheck /// internal class HealthCheckResolver : LazyManyObjectsResolverBase, IHealthCheckResolver { - public HealthCheckResolver(ILogger logger, Func> lazyTypeList) + public HealthCheckResolver(ILogger logger, Func> lazyTypeList) : base(new HealthCheckServiceProvider(), logger, lazyTypeList, ObjectLifetimeScope.HttpRequest) { } @@ -51,7 +51,7 @@ namespace Umbraco.Web.HealthCheck new HealthCheckContext(new HttpContextWrapper(HttpContext.Current), UmbracoContext.Current) }); } - + //use normal ctor return Activator.CreateInstance(serviceType); }