Пример #1
0
        private static void SetHeaders(BundleResponse response, BundleContext context, bool noCache)
        {
            if (context.HttpContext.Response != null)
            {
                if (response.ContentType != null)
                {
                    context.HttpContext.Response.ContentType = response.ContentType;
                }

                // Do set caching headers if instrumentation mode is on
                if (!context.EnableInstrumentation && context.HttpContext.Response.Cache != null)
                {
                    // NOTE: These caching headers were based off of what AssemblyResourceLoader does
                    // TODO: let the bundle change the caching semantics?
                    HttpCachePolicyBase cachePolicy = context.HttpContext.Response.Cache;
                    if (noCache)
                    {
                        cachePolicy.SetCacheability(HttpCacheability.NoCache);
                    }
                    else
                    {
                        cachePolicy.SetCacheability(response.Cacheability);
                        cachePolicy.SetOmitVaryStar(true);
                        cachePolicy.SetExpires(DateTime.Now.AddYears(1));
                        cachePolicy.SetValidUntilExpires(true);
                        cachePolicy.SetLastModified(DateTime.Now);
                        cachePolicy.VaryByHeaders["User-Agent"] = true;
                        // CONSIDER: Last modified logic, need to compute a hash of all the dates/ETag support
                    }
                }
            }
        }
        private void SetBundleHeaders(BundleResponse bundleResponse, BundleContext context)
        {
            if (context.HttpContext.Response == null)
            {
                return;
            }

            if (bundleResponse.ContentType != null)
            {
                context.HttpContext.Response.ContentType = bundleResponse.ContentType;
            }

            if (context.EnableInstrumentation || context.HttpContext.Response.Cache == null)
            {
                return;
            }

            HttpCachePolicyBase cache = context.HttpContext.Response.Cache;

            cache.SetCacheability(bundleResponse.Cacheability);
            cache.SetOmitVaryStar(true);
            cache.SetExpires(DateTime.Now.AddYears(1));
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(DateTime.Now);
            cache.VaryByHeaders["User-Agent"] = true;
        }
Пример #3
0
        protected void SetClientCacheHeader(DateTime?LastModified, string Etag, HttpCacheability CacheKind, bool ReValidate = true)
        {
            if (!EnableClientCache || LastModified == null && Etag == null)
            {
                return;
            }
            HttpCachePolicyBase cp = Response.Cache;

            cp.AppendCacheExtension("max-age=" + 3600 * MaxClientCacheAgeInHours);
            if (ReValidate)
            {
                cp.AppendCacheExtension("must-revalidate");
                cp.AppendCacheExtension("proxy-revalidate");
            }
            cp.SetCacheability(CacheKind);
            cp.SetOmitVaryStar(false);
            if (LastModified != null)
            {
                cp.SetLastModified(LastModified.Value);
            }
            cp.SetExpires(DateTime.UtcNow.AddHours(MaxClientCacheAgeInHours));
            if (Etag != null)
            {
                cp.SetETag(Etag);
            }
        }
Пример #4
0
        protected virtual void SetCache(HttpResponseBase response, int cacheDuration, params string[] varyByParams)
        {
            // Cache
            if (cacheDuration > 0)
            {
                DateTime timestamp = DateTime.Now;

                HttpCachePolicyBase cache = response.Cache;
                int duration = cacheDuration;

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetAllowResponseInBrowserHistory(true);
                cache.SetExpires(timestamp.AddSeconds(duration));
                cache.SetMaxAge(new TimeSpan(0, 0, duration));
                cache.SetValidUntilExpires(true);
                cache.SetLastModified(timestamp);
                cache.VaryByHeaders["Accept-Encoding"] = true;
                if (varyByParams != null)
                {
                    foreach (var p in varyByParams)
                    {
                        cache.VaryByParams[p] = true;
                    }
                }

                cache.SetOmitVaryStar(true);
                response.AddHeader("Cache-Control", string.Format("public, max-age={0}", cacheDuration));
            }
        }
Пример #5
0
        public override void ExecuteResult(ControllerContext context)
        {
            HttpCachePolicyBase cache = context.HttpContext.Response.Cache;

            cache.SetCacheability(Cacheability);
            cache.SetLastModified(LastModified);

            base.ExecuteResult(context);
        }
        public static void CacheResponseFor(this HttpContextBase context, TimeSpan duration)
        {
            HttpCachePolicyBase cache = context.Response.Cache;

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetLastModified(context.Timestamp);
            cache.SetExpires(context.Timestamp.Add(duration));
            cache.SetMaxAge(duration);
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        }
Пример #7
0
        private static void PrepareResponseNoCache(HttpResponseBase response)
        {
            HttpCachePolicyBase cachePolicy = response.Cache;
            DateTime            now         = DateTime.Now;

            cachePolicy.SetCacheability(HttpCacheability.Public);
            cachePolicy.SetExpires(now + TimeSpan.FromDays(365));
            cachePolicy.SetValidUntilExpires(true);
            cachePolicy.SetLastModified(now);
            cachePolicy.SetNoServerCaching();
        }
Пример #8
0
        private static void PrepareResponseCache(HttpResponseBase response)
        {
            HttpCachePolicyBase cachePolicy = response.Cache;
            DateTime            now         = DateTime.Now;

            cachePolicy.SetCacheability(HttpCacheability.Public);
            cachePolicy.VaryByParams["d"] = true;
            cachePolicy.SetOmitVaryStar(true);
            cachePolicy.SetExpires(now + TimeSpan.FromDays(365));
            cachePolicy.SetValidUntilExpires(true);
            cachePolicy.SetLastModified(now);
        }
        /// <summary>
        /// Caches the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="duration">The duration.</param>
        public static void Cache(this HttpContextBase instance, TimeSpan duration)
        {
            HttpCachePolicyBase cache = instance.Response.Cache;

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetOmitVaryStar(true);
            cache.SetExpires(instance.Timestamp.Add(duration));
            cache.SetMaxAge(duration);
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(instance.Timestamp);
            cache.SetLastModifiedFromFileDependencies();
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        }
Пример #10
0
        public override void Process(BundleContext context, BundleResponse bundleResponse)
        {
            HttpCachePolicyBase cachePolicy = context.HttpContext.Response.Cache;

            cachePolicy.SetCacheability(HttpCacheability.Public);
            cachePolicy.SetOmitVaryStar(true);
            cachePolicy.SetExpires(DateTime.Now.AddDays(3));
            cachePolicy.SetValidUntilExpires(true);
            cachePolicy.SetLastModified(DateTime.Now);

            base.Process(context, bundleResponse);


            /* cachePolicy.VaryByHeaders["User-Agent"] = true; */
        }
Пример #11
0
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (Day <= 0)
            {
                return;
            }
            HttpCachePolicyBase cache    = filterContext.HttpContext.Response.Cache;
            TimeSpan            Duration = TimeSpan.FromDays(Day);

            cache.SetExpires(DateTime.Now.Add(Duration));
            cache.SetMaxAge(Duration);
            cache.SetLastModified(DateTime.Now);
            cache.SetCacheability(Cacheability);
            base.OnActionExecuted(filterContext);
        }
Пример #12
0
        /// <summary>
        /// Set the caching policy.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="duration">The duration.</param>
        public void Cache(HttpContextBase context, TimeSpan duration)
        {
            if ((duration > TimeSpan.Zero) || !context.IsDebuggingEnabled) // Do not cache in debug mode.
            {
                HttpCachePolicyBase cache = context.Response.Cache;

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetOmitVaryStar(true);
                cache.SetExpires(context.Timestamp.Add(duration));
                cache.SetMaxAge(duration);
                cache.SetValidUntilExpires(true);
                cache.SetLastModified(context.Timestamp);
                cache.SetLastModifiedFromFileDependencies();
                cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            }
        }
Пример #13
0
        protected override void SendMediaHeaders(Media media, HttpContextBase context)
        {
            GetResponseCacheHeadersArgs args = new GetResponseCacheHeadersArgs(media.MediaData.MediaItem.InnerItem, new ResponseCacheHeaders()
            {
                Vary = this.GetVaryHeader(media, context)
            })
            {
                RequestType = new RequestTypes?(RequestTypes.Media)
            };

            GetResponseCacheHeadersPipeline.Run(args);
            HttpCachePolicyBase cache = context.Response.Cache;

            if (args.CacheHeaders.LastModifiedDate.HasValue)
            {
                cache.SetLastModified(args.CacheHeaders.LastModifiedDate.Value);
            }
            if (!string.IsNullOrEmpty(args.CacheHeaders.ETag))
            {
                cache.SetETag(args.CacheHeaders.ETag);
            }
            if (args.CacheHeaders.Cacheability.HasValue)
            {
                cache.SetCacheability(args.CacheHeaders.Cacheability.Value);
            }
            if (args.CacheHeaders.MaxAge.HasValue)
            {
                cache.SetMaxAge(args.CacheHeaders.MaxAge.Value);
            }
            if (args.CacheHeaders.ExpirationDate.HasValue)
            {
                cache.SetExpires(args.CacheHeaders.ExpirationDate.Value);
            }
            if (!string.IsNullOrEmpty(args.CacheHeaders.CacheExtension))
            {
                cache.AppendCacheExtension(args.CacheHeaders.CacheExtension);
            }
            if (string.IsNullOrEmpty(args.CacheHeaders.Vary))
            {
                return;
            }
            context.Response.AppendHeader("vary", args.CacheHeaders.Vary);
        }
Пример #14
0
        public static void OutputCache(this HttpResponseBase response,
                                       int numberOfSeconds,
                                       bool sliding = false,
                                       IEnumerable <string> varyByParams           = null,
                                       IEnumerable <string> varyByHeaders          = null,
                                       IEnumerable <string> varyByContentEncodings = null,
                                       HttpCacheability cacheability = HttpCacheability.Public)
        {
            HttpCachePolicyBase cache = response.Cache;

            var context = HttpContext.Current;

            cache.SetCacheability(cacheability);
            cache.SetExpires(context.Timestamp.AddSeconds(numberOfSeconds));
            cache.SetMaxAge(new TimeSpan(0, 0, numberOfSeconds));
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(context.Timestamp);
            cache.SetSlidingExpiration(sliding);

            if (varyByParams != null)
            {
                foreach (var p in varyByParams)
                {
                    cache.VaryByParams[p] = true;
                }
            }

            if (varyByHeaders != null)
            {
                foreach (var headerName in varyByHeaders)
                {
                    cache.VaryByHeaders[headerName] = true;
                }
            }

            if (varyByContentEncodings != null)
            {
                foreach (var contentEncoding in varyByContentEncodings)
                {
                    cache.VaryByContentEncodings[contentEncoding] = true;
                }
            }
        }
Пример #15
0
        internal static void OutputCache(
            HttpContextBase httpContext,
            HttpCachePolicyBase cache,
            int numberOfSeconds,
            bool sliding,
            IEnumerable <string> varyByParams,
            IEnumerable <string> varyByHeaders,
            IEnumerable <string> varyByContentEncodings,
            HttpCacheability cacheability
            )
        {
            cache.SetCacheability(cacheability);
            cache.SetExpires(httpContext.Timestamp.AddSeconds(numberOfSeconds));
            cache.SetMaxAge(new TimeSpan(0, 0, numberOfSeconds));
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(httpContext.Timestamp);
            cache.SetSlidingExpiration(sliding);

            if (varyByParams != null)
            {
                foreach (var p in varyByParams)
                {
                    cache.VaryByParams[p] = true;
                }
            }

            if (varyByHeaders != null)
            {
                foreach (var headerName in varyByHeaders)
                {
                    cache.VaryByHeaders[headerName] = true;
                }
            }

            if (varyByContentEncodings != null)
            {
                foreach (var contentEncoding in varyByContentEncodings)
                {
                    cache.VaryByContentEncodings[contentEncoding] = true;
                }
            }
        }
Пример #16
0
        internal static void OutputCache(HttpContextBase httpContext,
                                         HttpCachePolicyBase cache,
                                         int numberOfSeconds,
                                         bool sliding,
                                         IEnumerable<string> varyByParams,
                                         IEnumerable<string> varyByHeaders,
                                         IEnumerable<string> varyByContentEncodings,
                                         HttpCacheability cacheability)
        {
            cache.SetCacheability(cacheability);
            cache.SetExpires(httpContext.Timestamp.AddSeconds(numberOfSeconds));
            cache.SetMaxAge(new TimeSpan(0, 0, numberOfSeconds));
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(httpContext.Timestamp);
            cache.SetSlidingExpiration(sliding);

            if (varyByParams != null)
            {
                foreach (var p in varyByParams)
                {
                    cache.VaryByParams[p] = true;
                }
            }

            if (varyByHeaders != null)
            {
                foreach (var headerName in varyByHeaders)
                {
                    cache.VaryByHeaders[headerName] = true;
                }
            }

            if (varyByContentEncodings != null)
            {
                foreach (var contentEncoding in varyByContentEncodings)
                {
                    cache.VaryByContentEncodings[contentEncoding] = true;
                }
            }
        }
Пример #17
0
        protected void SendOuptToClient(HttpContextBase context)
        {
            HttpResponseBase    response = context.Response;
            HttpCachePolicyBase cache    = context.Response.Cache;

            cache.SetETag(_etagCacheKey);
            cache.SetLastModified(_lastModify);
            cache.SetExpires(NetworkTime.Now.AddDays(_durationInDays)); // For HTTP 1.0 browsers
            cache.SetOmitVaryStar(true);
            cache.SetMaxAge(TimeSpan.FromDays(_durationInDays));
            //cache.SetLastModified(NetworkTime.Now);

            cache.SetValidUntilExpires(true);
            cache.SetCacheability(HttpCacheability.Public);
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cache.VaryByHeaders["Accept-Encoding"] = true;// Tell proxy to cache different versions depending on Accept-Encoding
            response.ContentType = "application/json";
            if (response.IsClientConnected)
            {
                response.Flush();
            }
        }
Пример #18
0
        public static void Apply(this HttpCacheOptions options, HttpCachePolicyBase policy)
        {
            if (policy == null)
            {
                return;
            }

            policy.SetCacheability(options.IsPublic ? HttpCacheability.Public : HttpCacheability.Private);

            if (options.LastModified.HasValue)
            {
                policy.SetLastModified(options.LastModified.Value);
            }

            if (!string.IsNullOrEmpty(options.ETag))
            {
                policy.SetETag(options.QuotedETag);
            }

            if (options.MaxAge.HasValue)
            {
                policy.SetMaxAge(options.MaxAge.Value);
            }
        }
        public void Index(string name, string version, string condition)
        {
            HttpResponseBase response = Response;

            WebResourcesSection section = ConfigurationManager.GetSection(Kooboo.Common.Web.AreaHelpers.GetAreaName(this.RouteData));

            if (section == null)
            {
                throw new HttpException(500, "Unable to find the web resource configuration.");
            }

            ReferenceElement settings = section.References[name];

            if (settings == null)
            {
                throw new HttpException(500, string.Format("Unable to find any matching web resource settings for {0}.", name));
            }

            Condition conditionInfo = new Condition
            {
                If = condition ?? string.Empty
            };

            // filter the files based on the condition (Action / If) passed in
            IList <FileInfoElement> files = new List <FileInfoElement>();

            foreach (FileInfoElement fileInfo in settings.Files)
            {
                if (fileInfo.If.Equals(conditionInfo.If))
                {
                    files.Add(fileInfo);
                }
            }

            // Ooutput Type
            response.ContentType = settings.MimeType;
            Stream output = response.OutputStream;

            // Compress
            if (section.Compress)
            {
                string acceptEncoding = Request.Headers["Accept-Encoding"];

                if (!string.IsNullOrEmpty(acceptEncoding))
                {
                    acceptEncoding = acceptEncoding.ToLowerInvariant();

                    if (acceptEncoding.Contains("gzip"))
                    {
                        response.AddHeader("Content-encoding", "gzip");
                        output = new GZipStream(output, CompressionMode.Compress);
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        response.AddHeader("Content-encoding", "deflate");
                        output = new DeflateStream(output, CompressionMode.Compress);
                    }
                }
            }

            // Combine
            using (StreamWriter sw = new StreamWriter(output))
            {
                foreach (FileInfoElement fileInfo in files)
                {
                    string content;
                    var    dynamicResource = DynamicClientResourceFactory.Default.ResolveProvider(fileInfo.Filename);

                    if (dynamicResource != null)
                    {
                        content = dynamicResource.Parse(fileInfo.Filename);
                    }
                    else
                    {
                        content = System.IO.File.ReadAllText(Server.MapPath(fileInfo.Filename));
                    }
                    switch (settings.MimeType)
                    {
                    case "text/css":
                        content = CSSMinify.Minify(Url, fileInfo.Filename, Request.Url.AbsolutePath, content);
                        break;

                    case "text/x-javascript":
                    case "text/javascript":
                    case "text/ecmascript":
                        if (section.Compact && fileInfo.Compact)
                        {
                            content = JSMinify.Minify(content);
                        }
                        break;
                    }
                    sw.WriteLine(content.Trim());
                }
            }

            // Cache

            if (section.CacheDuration > 0)
            {
                DateTime            timestamp = HttpContext.Timestamp;
                HttpCachePolicyBase cache     = response.Cache;
                int duration = section.CacheDuration;

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetExpires(timestamp.AddSeconds(duration));
                cache.SetMaxAge(new TimeSpan(0, 0, duration));
                cache.SetValidUntilExpires(true);
                cache.SetLastModified(timestamp);
                cache.VaryByHeaders["Accept-Encoding"] = true;
                cache.VaryByParams["name"]             = true;
                cache.VaryByParams["version"]          = true;
                cache.VaryByParams["display"]          = true;
                cache.VaryByParams["condition"]        = true;
                cache.SetOmitVaryStar(true);
            }
        }
Пример #20
0
 /// <summary>
 /// 设置请求内容最后一次被修改的时间
 /// </summary>
 /// <param name="lastModified">请求内容最后一次被修改的时间</param>
 public override void SetLastModified(DateTimeOffset lastModified)
 {
     _cachePolicy.SetLastModified(lastModified.UtcDateTime);
 }