Ejemplo n.º 1
0
        private void CompressDynamicContent(object sender, EventArgs e)
        {
            if (Globals.UseDynamicCompress && !Common.CommonUtil.ContainsInstalledKey(Context) && Context.Items.Contains("need-compress"))
            {
                if (string.IsNullOrEmpty(Context.Request.Form["_ajaxpanelid"]) && string.IsNullOrEmpty(Context.Request.QueryString["_ajaxpanelid"]))
                {
                    Common.CommonUtil.SetInstalledKey(Context);

                    string realPath = Request.Path.Remove(0, Request.ApplicationPath.Length + 1);

                    Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

                    CompressingType compressingType = RequestUtil.GetCompressingType(Context);

                    if (compressingType == CompressingType.GZip)
                    {
                        Response.Filter = new GZipFilter(Response.Filter);
                    }
                    else if (compressingType == CompressingType.Deflate)
                    {
                        Response.Filter = new DeflateFilter(Response.Filter);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static bool CompressStaticContent(HttpContext context)
        {
            if (Globals.UseStaticCompress == false || StringUtil.EndsWithIgnoreCase(context.Request.PhysicalPath, ".fast.aspx") == false)
            {
                return(false);
            }

            SetInstalledKey(context);

            string physicalPath = context.Request.PhysicalPath.Remove(context.Request.PhysicalPath.Length - 10);
            string contentType  = null;

            if (StringUtil.EndsWithIgnoreCase(physicalPath, ".css"))
            {
                contentType = "text/css";
            }

            else if (StringUtil.EndsWithIgnoreCase(physicalPath, ".js"))
            {
                contentType = "application/x-javascript";
            }

            else
            {
                return(false);
            }

            CompressingType compressingType = RequestUtil.GetCompressingType(context);

            context.Response.ClearHeaders();

            if (compressingType == CompressingType.None)
            {
                if (File.Exists(physicalPath))
                {
                    DateTime lastModified = File.GetLastWriteTime(physicalPath);

                    if (context.Request.Headers["If-Modified-Since"] != null)
                    {
                        DateTime ifModifiedSince;

                        if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"].Split(';')[0], out ifModifiedSince))
                        {
                            if (ifModifiedSince > lastModified.AddSeconds(-1))
                            {
                                context.Response.StatusCode        = 304;
                                context.Response.StatusDescription = "Not Modified";
                                context.ApplicationInstance.CompleteRequest();
                                return(true);
                            }
                        }
                    }

                    context.Response.ContentType = contentType;

                    if (lastModified < DateTime.Now)
                    {
                        context.Response.Cache.SetLastModified(lastModified);
                    }

                    context.Response.Cache.SetETag(lastModified.Ticks.ToString());
                    context.Response.BinaryWrite(File.ReadAllBytes(physicalPath));

                    context.ApplicationInstance.CompleteRequest();
                }
            }
            else
            {
                string cacheKey = string.Concat("fast.aspx/", compressingType.ToString(), "/", physicalPath);

                FastAspxCacheData cacheData = null;

                if (CacheUtil.TryGetValue <FastAspxCacheData>(cacheKey, out cacheData) == false)
                {
                    if (File.Exists(physicalPath))
                    {
                        cacheData = new FastAspxCacheData();

                        using (MemoryStream stream = new MemoryStream())
                        {
                            Stream compressStream = null;

                            if (compressingType == CompressingType.GZip)
                            {
                                compressStream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Compress);
                            }
                            else if (compressingType == CompressingType.Deflate)
                            {
                                compressStream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Compress);
                            }

                            byte[] buffer = File.ReadAllBytes(physicalPath);

                            compressStream.Write(buffer, 0, buffer.Length);
                            compressStream.Close();
                            compressStream.Dispose();
                            compressStream = null;

                            cacheData.Data = stream.ToArray();
                        }

                        cacheData.LastModified = File.GetLastWriteTime(physicalPath);

                        CacheUtil.Set <FastAspxCacheData>(cacheKey, cacheData, CacheTime.Long, CacheExpiresType.Sliding, new CacheDependency(physicalPath));
                    }
                }

                if (cacheData != null && cacheData.Data.Length > 0)
                {
                    if (context.Request.Headers["If-Modified-Since"] != null)
                    {
                        DateTime ifModifiedSince;

                        if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"].Split(';')[0], out ifModifiedSince))
                        {
                            if (ifModifiedSince > cacheData.LastModified.AddSeconds(-1))
                            {
                                context.Response.StatusCode        = 304;
                                context.Response.StatusDescription = "Not Modified";
                                context.ApplicationInstance.CompleteRequest();
                                return(true);
                            }
                        }
                    }

                    context.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

                    context.Response.ContentType = contentType;

                    if (compressingType == CompressingType.GZip)
                    {
                        context.Response.AppendHeader("Content-Encoding", "gzip");
                    }
                    else
                    {
                        context.Response.AppendHeader("Content-Encoding", "deflate");
                    }

                    if (cacheData.LastModified < DateTime.Now)
                    {
                        context.Response.Cache.SetLastModified(cacheData.LastModified);
                    }

                    context.Response.Cache.SetETag(cacheData.LastModified.Ticks.ToString());
                    context.Response.BinaryWrite(cacheData.Data);

                    context.ApplicationInstance.CompleteRequest();
                }
            }

            return(true);
        }