Esempio n. 1
0
 private void CompressContent(HttpApplication httpApplication)
 {
     if (!httpApplication.Context.Items.Contains("__@fluorinehttpcompress"))
     {
         httpApplication.Context.Items.Add("__@fluorinehttpcompress", 1);
         HttpCompressSettings httpCompressSettings = FluorineConfiguration.Instance.HttpCompressSettings;
         if (((httpCompressSettings.HandleRequest != HandleRequest.None) && ((httpCompressSettings.HandleRequest != HandleRequest.Amf) || !(httpApplication.Request.ContentType != "application/x-amf"))) && (httpCompressSettings.CompressionLevel != CompressionLevels.None))
         {
             string fileName = Path.GetFileName(httpApplication.Request.Path);
             if (!httpCompressSettings.IsExcludedPath(fileName) && ((httpApplication.Response.ContentType != null) && !httpCompressSettings.IsExcludedMimeType(httpApplication.Response.ContentType)))
             {
                 httpApplication.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
                 string str2 = httpApplication.Request.Headers["Accept-Encoding"];
                 if (str2 != null)
                 {
                     CompressingFilter compressStream = GetFilterForScheme(str2.Split(new char[] { ',' }), httpApplication.Response.Filter, httpCompressSettings);
                     if (compressStream != null)
                     {
                         if (httpApplication.Request.ContentType == "application/x-amf")
                         {
                             httpApplication.Response.Filter = new ThresholdFilter(compressStream, httpApplication.Response.Filter, httpCompressSettings.Threshold);
                         }
                         else
                         {
                             httpApplication.Response.Filter = compressStream;
                         }
                     }
                 }
             }
         }
     }
 }
Esempio n. 2
0
        private void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;

            if ((application.Response.Filter is CompressingFilter) || (application.Response.Filter is ThresholdFilter))
            {
                CompressingFilter compressingFilter = null;
                if (application.Response.Filter is ThresholdFilter)
                {
                    compressingFilter = (application.Response.Filter as ThresholdFilter).CompressingFilter;
                }
                else
                {
                    compressingFilter = application.Response.Filter as CompressingFilter;
                }
                ILog logger = null;
                try
                {
                    logger = LogManager.GetLogger(typeof(FluorineGateway));
                }
                catch
                {
                }
                if (((compressingFilter != null) && (logger != null)) && logger.get_IsDebugEnabled())
                {
                    float num = 0f;
                    if (compressingFilter.TotalIn != 0L)
                    {
                        num = (compressingFilter.TotalOut * 100L) / compressingFilter.TotalIn;
                    }
                    string fileName = Path.GetFileName(application.Request.Path);
                    if (application.Request.ContentType == "application/x-amf")
                    {
                        fileName = fileName + "(x-amf)";
                    }
                    string str2 = __Res.GetString("Compress_Info", new object[] { fileName, num });
                    logger.Debug(str2);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// EventHandler that gets ahold of the current request context and attempts to compress the output.
        /// </summary>
        /// <param name="httpApplication">The <see cref="HttpApplication"/> that is firing this event.</param>
        public void CompressContent(HttpApplication httpApplication)
        {
            // Only do this if we haven't already attempted an install.  This prevents PreSendRequestHeaders from
            // trying to add this item way to late.  We only want the first run through to do anything.
            // also, we use the context to store whether or not we've attempted an add, as it's thread-safe and
            // scoped to the request.  An instance of this module can service multiple requests at the same time,
            // so we cannot use a member variable.
            if (!httpApplication.Context.Items.Contains(FluorineHttpCompressKey))
            {
                // Log the install attempt in the HttpContext. Must do this first as several IF statements below skip full processing of this method
                httpApplication.Context.Items.Add(FluorineHttpCompressKey, 1);
                // Get the config settings
                HttpCompressSettings settings = FluorineConfiguration.Instance.HttpCompressSettings;
                // Skip if no request can be handled
                if (settings.HandleRequest == HandleRequest.None)
                {
                    return;
                }
                // Skip if only AMF is compressed and we do not have an AMF request
                if (settings.HandleRequest == HandleRequest.Amf && httpApplication.Request.ContentType != ContentType.AMF)
                {
                    return;
                }
                // Skip if the CompressionLevel is set to 'None'
                if (settings.CompressionLevel == CompressionLevels.None)
                {
                    return;
                }
                //string realPath = httpApplication.Request.Path.Remove(0, httpApplication.Request.ApplicationPath.Length+1);
                string realPath = Path.GetFileName(httpApplication.Request.Path);
                if (settings.IsExcludedPath(realPath))
                {
                    // Skip if the file path excludes compression
                    return;
                }
                // Skip if the MimeType excludes compression
                if (httpApplication.Response.ContentType == null || settings.IsExcludedMimeType(httpApplication.Response.ContentType))
                {
                    return;
                }

                // Fix to handle caching appropriately, see http://www.pocketsoap.com/weblog/2003/07/1330.html
                // This header is added only when the request has the possibility of being compressed
                // i.e. it is not added when the request is excluded from compression by CompressionLevel, Path, or MimeType
                httpApplication.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

                // Grab an array of algorithm;q=x, algorith;q=x style values
                string acceptedTypes = httpApplication.Request.Headers["Accept-Encoding"];
                // If we couldn't find the header, bail out
                if (acceptedTypes == null)
                {
                    return;
                }

                // The actual types could be , delimited.  split 'em out.
                string[] types = acceptedTypes.Split(',');

                CompressingFilter filter = GetFilterForScheme(types, httpApplication.Response.Filter, settings);
                // If we didn't find a filter, bail out
                if (filter == null)
                {
                    return;
                }
                // If we get here, we found a viable filter.
                // Set the filter and change the Content-Encoding header to match so the client can decode the response
                if (httpApplication.Request.ContentType == ContentType.AMF)
                {
                    httpApplication.Response.Filter = new ThresholdFilter(filter, httpApplication.Response.Filter, settings.Threshold);
                }
                else
                {
                    httpApplication.Response.Filter = filter;
                }
            }
        }
        private void CompressContent(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;

            if ((app == null) || (app.Context == null) || (app.Context.Items == null))
            {
                return;
            }
            else
            {
                CommonLibrary.Framework.CDefault page = app.Context.Handler as CommonLibrary.Framework.CDefault;
                if ((page == null))
                {
                    return;
                }
            }
            if (app.Response == null || app.Response.ContentType == null || app.Response.ContentType.ToLower() != "text/html")
            {
                return;
            }
            if (!app.Context.Items.Contains(INSTALLED_KEY))
            {
                app.Context.Items.Add(INSTALLED_KEY, INSTALLED_TAG);
                string realPath = app.Request.Url.PathAndQuery;
                CommonLibrary.HttpModules.Compression.Config.Settings _Settings = CommonLibrary.HttpModules.Compression.Config.Settings.GetSettings();
                if (_Settings == null)
                {
                    return;
                }
                bool compress = true;
                if (_Settings.PreferredAlgorithm == CommonLibrary.HttpModules.Compression.Config.Algorithms.None)
                {
                    compress = false;
                    if (!_Settings.Whitespace)
                    {
                        return;
                    }
                }
                string acceptedTypes = app.Request.Headers["Accept-Encoding"];
                if (_Settings.IsExcludedPath(realPath) || acceptedTypes == null)
                {
                    return;
                }
                app.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
                CompressingFilter filter = null;
                if (compress)
                {
                    string[] types = acceptedTypes.Split(',');
                    filter = GetFilterForScheme(types, app.Response.Filter, _Settings);
                }
                if (filter == null)
                {
                    if (_Settings.Whitespace)
                    {
                        app.Response.Filter = new WhitespaceFilter(app.Response.Filter, _Settings.Reg);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (_Settings.Whitespace)
                    {
                        app.Response.Filter = new WhitespaceFilter(filter, _Settings.Reg);
                    }
                    else
                    {
                        app.Response.Filter = filter;
                    }
                }
            }
        }