//******************************************************************************************************
// WebServerOptions.cs - Gbtc
//
// Copyright © 2017, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 08/27/2017 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System.Text.RegularExpressions;
using GSF.IO;
using GSF.Web.Security;
using GSF.Web.Shared;
namespace GSF.Web.Hosting
{
///
/// Represents configuration options for a instance.
///
public class WebServerOptions
{
#region [ Members ]
// Constants
///
/// Default value for .
///
public const string DefaultWebRootPath = "wwwroot";
///
/// Default value for .
///
public const string DefaultErrorTemplateName = Resources.Root + "/Shared/Views/Error.cshtml";
///
/// Default value for .
///
public const bool DefaultClientCacheEnabled = true;
///
/// Default value for .
///
public const bool DefaultMinifyJavascript = true;
///
/// Default value for the .
///
public const string DefaultMinifyJavascriptExclusionExpression = "";
///
/// Default value for .
///
public const bool DefaultMinifyStyleSheets = true;
///
/// Default value for the .
///
public const string DefaultMinifyStyleSheetsExclusionExpression = "";
///
/// Default value for .
///
public const bool DefaultUseMinifyInDebug = false;
// Fields
private string m_authTestPage;
private string m_webRootPath;
private string m_minifyJavascriptExclusionExpression;
private string m_minifyStyleSheetsExclusionExpression;
private Regex m_minifyJavascriptExclusionRegex;
private Regex m_minifyStyleSheetsExclusionRegex;
#endregion
#region [ Constructors ]
///
/// Creates a new instance.
///
public WebServerOptions()
{
WebRootPath = DefaultWebRootPath;
MinifyJavascriptExclusionExpression = DefaultMinifyJavascriptExclusionExpression;
MinifyStyleSheetsExclusionExpression = DefaultMinifyStyleSheetsExclusionExpression;
}
#endregion
#region [ Properties ]
///
/// Gets or sets root path defined for this .
///
public string WebRootPath
{
get => m_webRootPath;
set
{
m_webRootPath = value;
PhysicalWebRootPath = FilePath.AddPathSuffix(FilePath.GetAbsolutePath(m_webRootPath));
}
}
///
/// Gets physical path for .
///
public string PhysicalWebRootPath { get; private set; }
///
/// Gets or sets flag that determines if cache control is enabled for browser clients; default to true.
///
public bool ClientCacheEnabled { get; set; } = DefaultClientCacheEnabled;
///
/// Gets or sets value that determines if minification should be applied to rendered Javascript files.
///
public bool MinifyJavascript { get; set; } = DefaultMinifyJavascript;
///
/// Gets or sets the regular expression that will exclude Javascript files from being minified.
/// Assigning empty or null value results in all files targeted for minification.
///
public string MinifyJavascriptExclusionExpression
{
get => m_minifyJavascriptExclusionExpression;
set
{
m_minifyJavascriptExclusionExpression = value;
m_minifyJavascriptExclusionRegex = string.IsNullOrWhiteSpace(value) ?
null :
new Regex(value, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
}
///
/// Gets or sets value that determines if minification should be applied to rendered CSS files.
///
public bool MinifyStyleSheets { get; set; } = DefaultMinifyStyleSheets;
///
/// Gets or sets the regular expression that will exclude CSS files from being minified.
/// Assigning empty or null value results in all files targeted for minification.
///
public string MinifyStyleSheetsExclusionExpression
{
get => m_minifyStyleSheetsExclusionExpression;
set
{
m_minifyStyleSheetsExclusionExpression = value;
m_minifyStyleSheetsExclusionRegex = string.IsNullOrWhiteSpace(value) ?
null :
new Regex(value, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
}
///
/// Gets or sets value that determines if minification should be applied when running a Debug build.
///
public bool UseMinifyInDebug { get; set; } = DefaultUseMinifyInDebug;
///
/// Gets or sets the token used for identifying the session ID in cookie headers.
///
public string SessionToken { get; set; } = SessionHandler.DefaultSessionToken;
///
/// Gets or sets the page name used to test user authorization.
///
///
/// Page name for web server use will not be prefixed with slash. Any slash
/// prefix will automatically be removed during value assignment.
///
public string AuthTestPage
{
get
{
if (m_authTestPage is null)
AuthTestPage = AuthenticationOptions.DefaultAuthTestPage;
return m_authTestPage;
}
set
{
m_authTestPage = value;
// Remove any slash prefix for WebServer use
if (!string.IsNullOrEmpty(m_authTestPage) && m_authTestPage.StartsWith("/"))
m_authTestPage = m_authTestPage.Length > 1 ? m_authTestPage.Substring(1) : "";
}
}
///
/// Gets or sets template file name to use when a Razor compile or execution exception occurs.
///
public string ErrorTemplateName { get; set; } = DefaultErrorTemplateName;
///
/// Gets an immutable version of the web server options.
///
public ReadonlyWebServerOptions Readonly => new ReadonlyWebServerOptions(this);
#endregion
#region [ Methods ]
///
/// Determines if Javascript file referenced by should be minified
/// according to .
///
/// Javascript filename to check.
/// true if should be minified; otherwise, false.
///
/// Result will always be false if is not true.
///
public bool MinifyJavascriptResource(string urlPath)
{
if (!MinifyJavascript)
return false;
if (m_minifyJavascriptExclusionRegex is null)
return true;
return !m_minifyJavascriptExclusionRegex.IsMatch(urlPath);
}
///
/// Determines if CSS file referenced by should be minified
/// according to .
///
/// CSS filename to check.
/// true if should be minified; otherwise, false.
///
/// Result will always be false if is not true.
///
public bool MinifyStyleSheetResource(string urlPath)
{
if (!MinifyStyleSheets)
return false;
if (m_minifyStyleSheetsExclusionRegex is null)
return true;
return !m_minifyStyleSheetsExclusionRegex.IsMatch(urlPath);
}
#endregion
}
///
/// Represents an immutable version of .
///
public class ReadonlyWebServerOptions
{
#region [ Members ]
// Fields
private readonly WebServerOptions m_webServerOptions;
#endregion
#region [ Constructors ]
internal ReadonlyWebServerOptions(WebServerOptions webServerOptions)
{
// Make sure exposed properties cannot change source web server options
m_webServerOptions = webServerOptions;
}
#endregion
#region [ Properties ]
///
/// Gets root path defined for this .
///
public string WebRootPath => m_webServerOptions.WebRootPath;
///
/// Gets physical path for .
///
public string PhysicalWebRootPath => m_webServerOptions.PhysicalWebRootPath;
///
/// Gets flag that determines if cache control is enabled for browser clients; default to true.
///
public bool ClientCacheEnabled => m_webServerOptions.ClientCacheEnabled;
///
/// Gets value that determines if minification should be applied to rendered Javascript files.
///
public bool MinifyJavascript => m_webServerOptions.MinifyJavascript;
///
/// Gets regular expression that will exclude Javascript files from being minified.
///
public string MinifyJavascriptExclusionExpression => m_webServerOptions.MinifyJavascriptExclusionExpression;
///
/// Gets value that determines if minification should be applied to rendered CSS files.
///
public bool MinifyStyleSheets => m_webServerOptions.MinifyStyleSheets;
///
/// Gets regular expression that will exclude CSS files from being minified.
///
public string MinifyStyleSheetsExclusionExpression => m_webServerOptions.MinifyStyleSheetsExclusionExpression;
///
/// Gets value that determines if minification should be applied when running a Debug build.
///
public bool UseMinifyInDebug => m_webServerOptions.UseMinifyInDebug;
///
/// Gets the token used for identifying the session ID in cookie headers.
///
public string SessionToken => m_webServerOptions.SessionToken;
///
/// Gets the page name used to test user authorization.
///
///
/// Page name for web server use will not be prefixed with slash.
///
public string AuthTestPage => m_webServerOptions.AuthTestPage;
///
/// Gets template file name to use when a Razor compile or execution exception occurs.
///
public string ErrorTemplateName => m_webServerOptions.ErrorTemplateName;
#endregion
}
}