Esempio n. 1
0
        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>
        /// 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);
            }
        }
        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);
            }
        }
Esempio n. 4
0
        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 || ASPNETCORE3
                                        .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();

                    int minificationManagerCount = _minificationManagers.Count;
                    if (minificationManagerCount > 0)
                    {
                        for (int managerIndex = 0; managerIndex < minificationManagerCount; managerIndex++)
                        {
                            IMarkupMinificationManager minificationManager = _minificationManagers[managerIndex];
                            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;
                }
            }
        }