コード例 #1
0
        /// <summary>
        /// Write a StreamReader to an outpit stream
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="context"></param>
        protected static void CompressAndWriteToStream(StreamReader reader, HttpContext context)
        {
            var compressionType = CompressionModuleHelper.IsSpecificEncodingSupported(context, CompressionModuleHelper.GZIP) ? CompressionModuleHelper.GZIP : CompressionModuleHelper.DEFLATE;

            CompressionModuleHelper.SetEncodingType(context.Response, compressionType);

            // All the supported content for compression can be read as string, so we use reader.ReadToEnd()
            var compressed = CompressionModuleHelper.Compressor(reader.ReadToEnd(), compressionType);

            context.Response.OutputStream.Write(compressed, 0, compressed.Length);
            reader.Dispose();
        }
コード例 #2
0
        protected static void OnPostAcquireRequestState(object sender, EventArgs e)
        {
            var httpContext = HttpContext.Current;

            // This part of the module compress only handlers from type System.Web.UI.Page
            // Other types such JavaScript or CSS files will be compressed in an httpHandler.
            // Here we check if the current handler if a Page, if so, we compress it.
            // Because there is a problem with async postbacks compression, we check here if the current request if an 'MS AJAX' call.
            // If so, we will not compress it.
            // Important !!! : I didn't check this module with another Ajax frameworks such 'infragistics' or 'SmartClient'.
            // probably you will have to change the IsAjaxPostBackRequest method.

            if (!(httpContext.CurrentHandler is Page || httpContext.CurrentHandler.GetType().BaseType.FullName == "System.Web.Mvc.MvcHandler") || CompressionModuleHelper.IsAjaxPostBackRequest(httpContext))
            {
                return;
            }

            if (!CompressionModuleHelper.IsCompressionSupported(httpContext, true))
            {
                return;
            }

            var response = httpContext.Response;

            // Check if GZIP is supported by the client
            if (CompressionModuleHelper.IsSpecificEncodingSupported(httpContext, CompressionModuleHelper.GZIP))
            {
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                CompressionModuleHelper.SetEncodingType(response, CompressionModuleHelper.GZIP);
            }
            // If GZIP is not supported, so only DEFLATE is.
            else
            {
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                CompressionModuleHelper.SetEncodingType(response, CompressionModuleHelper.DEFLATE);
            }
        }