Esempio n. 1
0
        /// <summary>
        /// Handles the BeginRequest event of the context control.
        /// </summary>
        /// <param name="sender">The source of the event</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data</param>
        private void PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (!_configuration.IsCompressionEnabled())
            {
                return;
            }

            IHttpCompressionManager compressionManager =
                _compressionManager ?? HttpCompressionManager.Current;
            HttpContext  context    = ((HttpApplication)sender).Context;
            HttpRequest  request    = context.Request;
            HttpResponse response   = context.Response;
            string       httpMethod = request.HttpMethod;
            string       currentUrl = request.RawUrl;

            if (compressionManager.IsSupportedHttpStatusCode(response.StatusCode) &&
                compressionManager.IsSupportedHttpMethod(httpMethod) &&
                compressionManager.IsProcessablePage(currentUrl) &&
                !request.Path.EndsWith("WebResource.axd", StringComparison.OrdinalIgnoreCase))
            {
                context.Items["originalResponseFilter"] = response.Filter;

                string acceptEncoding = request.Headers["Accept-Encoding"];
                response.Filter = new HttpCompressionFilterStream(new HttpResponseWrapper(response),
                                                                  compressionManager, acceptEncoding);
            }
        }
        public void OnLoad(EventArgs e)
        {
            if (DisableCompression)
            {
                return;
            }

            IHttpCompressionManager compressionManager =
                _compressionManager ?? HttpCompressionManager.Current;
            HttpContext  context    = HttpContext.Current;
            HttpRequest  request    = context.Request;
            HttpResponse response   = context.Response;
            string       httpMethod = request.HttpMethod;
            string       currentUrl = request.RawUrl;

            if (compressionManager.IsSupportedHttpStatusCode(response.StatusCode) &&
                compressionManager.IsSupportedHttpMethod(httpMethod) &&
                compressionManager.IsProcessablePage(currentUrl))
            {
                context.Items["originalResponseFilter"] = response.Filter;

                string acceptEncoding = request.Headers["Accept-Encoding"];
                response.Filter = new HttpCompressionFilterStream(new HttpResponseWrapper(response),
                                                                  compressionManager, acceptEncoding);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Override to compress the content that is generated by an action method
        /// </summary>
        /// <param name="filterContext">Filter context</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!_configuration.IsCompressionEnabled())
            {
                return;
            }

            IHttpCompressionManager compressionManager =
                _compressionManager ?? HttpCompressionManager.Current;
            HttpContextBase  context    = filterContext.HttpContext;
            HttpRequestBase  request    = context.Request;
            HttpResponseBase response   = context.Response;
            string           httpMethod = request.HttpMethod;
            string           currentUrl = request.RawUrl;

            if (response.Filter != null && compressionManager.IsSupportedHttpStatusCode(response.StatusCode) &&
                compressionManager.IsSupportedHttpMethod(httpMethod) &&
                compressionManager.IsProcessablePage(currentUrl))
            {
                context.Items["originalResponseFilter"] = response.Filter;

                string acceptEncoding = request.Headers["Accept-Encoding"];
                response.Filter = new HttpCompressionFilterStream(response, compressionManager, acceptEncoding);
            }
        }
        protected void Initialize()
        {
            if (_wrapperInitializedFlag.Set())
            {
                HttpRequest  request  = _context.Request;
                HttpResponse response = _context.Response;

                int      httpStatusCode = response.StatusCode;
                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 || ASPNETCORE5
                                    .Value
#endif
                                    .ToLowerInvariant()
                        ;
                        encoding = mediaTypeHeader.Encoding;
                    }
                }

                string            currentUrl      = GetCurrentUrl(request);
                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.IsSupportedHttpStatusCode(httpStatusCode) &&
                            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.IsSupportedHttpStatusCode(httpStatusCode) &&
                    _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;
            }
        }