/// <summary> /// Constructs a instance of HTTP module for markup minification /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MarkupMinificationModuleBase( WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
public override void OnResultExecuted(ResultExecutedContext filterContext) { if (!_configuration.IsMinificationEnabled()) { return; } IMarkupMinificationManager minificationManager = _minificationManager ?? GetDefaultMinificationManager(); HttpContextBase context = filterContext.HttpContext; HttpRequestBase request = context.Request; HttpResponseBase response = context.Response; Encoding encoding = response.ContentEncoding; string httpMethod = request.HttpMethod; string mediaType = response.ContentType; string currentUrl = request.RawUrl; if (response.Filter != null && response.StatusCode == 200 && minificationManager.IsSupportedHttpMethod(httpMethod) && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { response.Filter = new MarkupMinificationFilterStream(response, _configuration, minificationManager, currentUrl, encoding); } }
/// <summary> /// Constructs a instance of HTTP module for markup minification /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MarkupMinificationModuleBase( WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Processes the response and sets a XML minification response filter /// </summary> /// <param name="sender">The source of the event (HTTP application)</param> /// <param name="e">An System.EventArgs that contains no event data</param> private void ProcessResponse(object sender, EventArgs e) { if (!_configuration.IsMinificationEnabled()) { return; } IMarkupMinificationManager minificationManager = _minificationManager ?? GetDefaultMinificationManager(); HttpContext context = ((HttpApplication)sender).Context; HttpRequest request = context.Request; HttpResponse response = context.Response; Encoding encoding = response.ContentEncoding; string mediaType = response.ContentType; string currentUrl = request.RawUrl; if (request.HttpMethod == "GET" && response.StatusCode == 200 && context.CurrentHandler != null && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { response.Filter = new MarkupMinificationFilterStream(new HttpResponseWrapper(response), _configuration, minificationManager, currentUrl, encoding); } }
/// <summary> /// Constructs a instance of markup minification attribute /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MinifyMarkupAttribute( WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Constructs a instance of markup minification attribute /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MinifyMarkupAttribute( WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Constructs a instance of Web Forms page with support of markup minification and HTTP compression /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> /// <param name="compressionManager">HTTP compression manager</param> protected MinifiedAndCompressedPageBase(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager, IHttpCompressionManager compressionManager) : base(configuration, minificationManager) { _compressedComponent = new CompressedComponent(configuration, compressionManager); }
/// <summary> /// Constructs a instance of master page with support of markup minification and HTTP compression /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> /// <param name="compressionManager">HTTP compression manager</param> protected MinifiedAndCompressedMasterPageBase(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager, IHttpCompressionManager compressionManager) : base(configuration, minificationManager) { _compressedComponent = new CompressedComponent(configuration, compressionManager); }
public override async ValueTask DisposeAsync() { if (_disposedFlag.Set()) { if (_compressionStream != null) { await _compressionStream.DisposeAsync(); _compressionStream = null; } _currentCompressor = null; if (_cachedStream != null) { await _cachedStream.DisposeAsync(); _cachedStream = null; } _currentMinificationManager = null; await base.DisposeAsync(); } }
protected override void Dispose(bool disposing) { if (_disposedFlag.Set()) { if (disposing) { if (_compressionStream != null) { _compressionStream.Dispose(); _compressionStream = null; } _currentCompressor = null; if (_cachedStream != null) { _cachedStream.Dispose(); _cachedStream = null; } _currentMinificationManager = null; } base.Dispose(disposing); } }
/// <summary> /// Appends a <code>*-Minification-Powered-By</code> HTTP-header /// </summary> /// <param name="minificationManager">Markup minification manager</param> /// <param name="append">HTTP header appending delegate</param> public static void AppendPoweredByHttpHeader(this IMarkupMinificationManager minificationManager, Action <string, string> append) { KeyValuePair <string, string> poweredByHttpHeader = minificationManager.PoweredByHttpHeader; append(poweredByHttpHeader.Key, poweredByHttpHeader.Value); }
public void Render(HtmlTextWriter writer, Action <HtmlTextWriter> renderHandler) { if (!DisableMinification) { IMarkupMinificationManager minificationManager = _minificationManager ?? GetDefaultMinificationManager(); HttpContext context = HttpContext.Current; HttpResponse response = context.Response; HttpRequest request = context.Request; using (var htmlwriter = new HtmlTextWriter(new StringWriter())) { renderHandler(htmlwriter); string content = htmlwriter.InnerWriter.ToString(); bool isMinified = false; Encoding encoding = response.ContentEncoding; int responseSize = encoding.GetByteCount(content); string httpMethod = request.HttpMethod; string mediaType = response.ContentType; string currentUrl = request.RawUrl; if (response.StatusCode == 200 && _configuration.IsAllowableResponseSize(responseSize) && minificationManager.IsSupportedHttpMethod(httpMethod) && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { IMarkupMinifier minifier = minificationManager.CreateMinifier(); MarkupMinificationResult minificationResult = minifier.Minify(content, currentUrl, encoding, minificationManager.GenerateStatistics); if (minificationResult.Errors.Count == 0) { if (_configuration.IsPoweredByHttpHeadersEnabled()) { minificationManager.AppendPoweredByHttpHeader((key, value) => { response.Headers[key] = value; }); } writer.Write(minificationResult.MinifiedContent); isMinified = true; } } if (!isMinified) { writer.Write(content); } } } else { renderHandler(writer); } }
/// <summary> /// Constructs a instance of markup minification response filter /// </summary> /// <param name="response">HTTP response</param> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> /// <param name="currentUrl">Current URL</param> /// <param name="encoding">Text encoding</param> public MarkupMinificationFilterStream(HttpResponseBase response, WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager, string currentUrl, Encoding encoding) { _response = response; _originalStream = response.Filter; _configuration = configuration; _minificationManager = minificationManager; _currentUrl = currentUrl; _encoding = encoding; }
/// <summary> /// Constructs a instance of markup minification response filter /// </summary> /// <param name="response">HTTP response</param> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> /// <param name="currentUrl">Current URL</param> /// <param name="encoding">Text encoding</param> public MarkupMinificationFilterStream(HttpResponseBase response, WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager, string currentUrl, Encoding encoding) { _response = response; _originalStream = response.Filter; _configuration = configuration; _minificationManager = minificationManager; _currentUrl = currentUrl; _encoding = encoding; }
/// <summary> /// Сheck whether to allow processing of page /// </summary> /// <param name="minificationManager">Markup minification manager</param> /// <param name="pageUrl">URL of page</param> /// <returns>Result of check (true - allowed; false - disallowed)</returns> public static bool IsProcessablePage(this IMarkupMinificationManager minificationManager, string pageUrl) { IList <IUrlMatcher> includedPages = minificationManager.IncludedPages; IList <IUrlMatcher> excludedPages = minificationManager.ExcludedPages; if (includedPages.Count == 0 && excludedPages.Count == 0) { return(true); } if (excludedPages.Count > 0) { foreach (IUrlMatcher matcher in excludedPages) { bool isExcludedPage = matcher.IsMatch(pageUrl); if (isExcludedPage) { return(false); } } } if (includedPages.Count > 0) { bool isIncludedPage = false; foreach (IUrlMatcher matcher in includedPages) { isIncludedPage = matcher.IsMatch(pageUrl); if (isIncludedPage) { break; } } if (isIncludedPage) { return(true); } } else { return(true); } return(false); }
/// <summary> /// Constructs a instance of minified component /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> public MinifiedComponent(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Constructs a instance of master page with support of markup minification /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MinifiedMasterPageBase(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _minifiedComponent = new MinifiedComponent(configuration, minificationManager); }
/// <summary> /// Constructs a instance of minified component /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MinifiedComponentBase(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Constructs a instance of markup minification response filter /// </summary> /// <param name="response">HTTP response</param> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> public MarkupMinificationFilterStream(HttpResponseBase response, WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) : this(response, configuration, minificationManager, string.Empty, Encoding.Default) { }
private void Initialize() { if (_wrapperInitializedFlag.Set()) { HttpRequest request = _context.Request; HttpResponse response = _context.Response; if (response.StatusCode == 200) { string httpMethod = request.Method; string contentType = response.ContentType; string mediaType = null; Encoding encoding = null; if (contentType != null) { MediaTypeHeaderValue mediaTypeHeader; if (MediaTypeHeaderValue.TryParse(contentType, out mediaTypeHeader)) { mediaType = mediaTypeHeader.MediaType #if ASPNETCORE2 .Value #endif .ToLowerInvariant() ; encoding = mediaTypeHeader.Encoding; } } string currentUrl = request.Path.Value; QueryString queryString = request.QueryString; if (queryString.HasValue) { currentUrl += queryString.Value; } IHeaderDictionary responseHeaders = response.Headers; bool isEncodedContent = responseHeaders.IsEncodedContent(); if (_minificationManagers.Count > 0) { foreach (IMarkupMinificationManager minificationManager in _minificationManagers) { if (minificationManager.IsSupportedHttpMethod(httpMethod) && mediaType != null && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { if (isEncodedContent) { throw new InvalidOperationException( string.Format( AspNetCommonStrings.MarkupMinificationIsNotApplicableToEncodedContent, responseHeaders[HeaderNames.ContentEncoding] ) ); } _currentMinificationManager = minificationManager; _cachedStream = new MemoryStream(); _minificationEnabled = true; break; } } } if (_compressionManager != null && !isEncodedContent && _compressionManager.IsSupportedHttpMethod(httpMethod) && _compressionManager.IsSupportedMediaType(mediaType) && _compressionManager.IsProcessablePage(currentUrl)) { string acceptEncoding = request.Headers[HeaderNames.AcceptEncoding]; ICompressor compressor = InitializeCurrentCompressor(acceptEncoding); if (compressor != null) { _compressionStream = compressor.Compress(_originalStream); _compressionEnabled = true; } } _currentUrl = currentUrl; _encoding = encoding; } } }
/// <summary> /// Constructs a instance of markup minification response filter /// </summary> /// <param name="response">HTTP response</param> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> public MarkupMinificationFilterStream(HttpResponseBase response, WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) : this(response, configuration, minificationManager, string.Empty, TextEncodingShortcuts.Default) { }
/// <summary> /// Constructs a instance of Web Forms page with support of markup minification /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MinifiedPageBase(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _minifiedComponent = new MinifiedComponent(configuration, minificationManager); }
/// <summary> /// Constructs a instance of minified component /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> protected MinifiedComponentBase(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Constructs a instance of minified component /// </summary> /// <param name="configuration">WebMarkupMin configuration</param> /// <param name="minificationManager">Markup minification manager</param> public MinifiedComponent(WebMarkupMinConfiguration configuration, IMarkupMinificationManager minificationManager) { _configuration = configuration; _minificationManager = minificationManager; }
/// <summary> /// Checks whether the media-type is supported /// </summary> /// <param name="minificationManager">Markup minification manager</param> /// <param name="mediaType">Media-type</param> /// <returns>Result of check (true - supported; false - not supported)</returns> public static bool IsSupportedMediaType(this IMarkupMinificationManager minificationManager, string mediaType) { return(minificationManager.SupportedMediaTypes.Contains(mediaType)); }