Beispiel #1
0
        public static void SetResponseCachePolicy(
            HttpCachePolicyElement policy,
            HttpResponseBase response,
            HttpCacheability defaultCacheability,
            string defaultMaxAge                 = "01:00:00",
            string defaultVaryByParams           = null,
            string defaultVaryByContentEncodings = null)
        {
            if (!string.IsNullOrWhiteSpace(policy.CacheExtension))
            {
                response.Cache.AppendCacheExtension(policy.CacheExtension);
            }

            var maxAge = policy.MaxAge ?? defaultMaxAge;

            if (!string.IsNullOrWhiteSpace(maxAge))
            {
                var maxAgeSpan = TimeSpan.Parse(maxAge);
                response.Cache.SetExpires(DateTime.Now.Add(maxAgeSpan));
                response.Cache.SetMaxAge(maxAgeSpan);
            }

            if (!string.IsNullOrWhiteSpace(policy.Expires))
            {
                var expires = DateTime.Parse(policy.Expires);
                response.Cache.SetExpires(expires);
            }

            var cacheability = policy.Cacheability != null ? policy.Cacheability.Value : defaultCacheability;

            response.Cache.SetCacheability(cacheability);

            if (policy.Revalidation != null)
            {
                response.Cache.SetRevalidation(policy.Revalidation.Value);
            }

            if (policy.SlidingExpiration != null)
            {
                response.Cache.SetSlidingExpiration(policy.SlidingExpiration.Value);
            }

            if (policy.ValidUntilExpires != null)
            {
                response.Cache.SetValidUntilExpires(policy.ValidUntilExpires.Value);
            }

            if (!string.IsNullOrWhiteSpace(policy.VaryByCustom))
            {
                response.Cache.SetVaryByCustom(policy.VaryByCustom);
            }

            // encoding based output caching should be disabled for annotations

            var varyByContentEncodings = policy.VaryByContentEncodings == null || policy.VaryByContentEncodings == "gzip;x-gzip;deflate"
                                ? defaultVaryByContentEncodings
                                : policy.VaryByContentEncodings;

            if (!string.IsNullOrWhiteSpace(varyByContentEncodings))
            {
                ForEachParam(varyByContentEncodings, param => response.Cache.VaryByContentEncodings[param] = true);
            }

            if (!string.IsNullOrWhiteSpace(policy.VaryByHeaders))
            {
                ForEachParam(policy.VaryByHeaders, param => response.Cache.VaryByHeaders[param] = true);
            }

            var varyByParams = policy.VaryByParams ?? defaultVaryByParams;

            if (!string.IsNullOrWhiteSpace(varyByParams))
            {
                ForEachParam(varyByParams, param => response.Cache.VaryByParams[param] = true);
            }
        }
Beispiel #2
0
        internal RequestCachingSectionInternal(RequestCachingSection section)
        {
#if !FEATURE_PAL // IE caching
//ROTORTODO: Review // IE caching
//CORIOLISTODO: Review // IE caching
            if (!section.DisableAllCaching)
            {
                this.defaultCachePolicy    = new RequestCachePolicy(section.DefaultPolicyLevel); // default should be RequestCacheLevel.BypassCache
                this.isPrivateCache        = section.IsPrivateCache;
                this.unspecifiedMaximumAge = section.UnspecifiedMaximumAge;                      //default should be  TimeSpan.FromDays(1)
            }
            else
            {
                this.disableAllCaching = true;
            }

            this.httpRequestCacheValidator = new HttpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
            this.ftpRequestCacheValidator  = new FtpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
            this.defaultCache = new Microsoft.Win32.WinInetCache(this.IsPrivateCache, true, true);

            if (section.DisableAllCaching)
            {
                return;
            }


            HttpCachePolicyElement httpPolicy = section.DefaultHttpCachePolicy;

            if (httpPolicy.WasReadFromConfig)
            {
                if (httpPolicy.PolicyLevel == HttpRequestCacheLevel.Default)
                {
                    HttpCacheAgeControl cacheAgeControl =
                        (httpPolicy.MinimumFresh != TimeSpan.MinValue ? HttpCacheAgeControl.MaxAgeAndMinFresh : HttpCacheAgeControl.MaxAgeAndMaxStale);

                    this.defaultHttpCachePolicy = new HttpRequestCachePolicy(cacheAgeControl, httpPolicy.MaximumAge, (httpPolicy.MinimumFresh != TimeSpan.MinValue ? httpPolicy.MinimumFresh : httpPolicy.MaximumStale));
                }
                else
                {
                    this.defaultHttpCachePolicy = new HttpRequestCachePolicy(httpPolicy.PolicyLevel);
                }
            }
#else //!FEATURE_PAL // IE caching
#if CORIOLIS
            if (section.DisableAllCaching)
            {
                this.httpRequestCacheValidator = new HttpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
                this.disableAllCaching         = true;
            }
            else
            {
                // Caching needs to be disabled in the configuration since Coriolis
                // does not support it.
                // This is a validity check, that it is actually disabled.
                throw new NotImplementedException("ROTORTODO - RequestCaching - IE caching");
            }
#else // CORIOLIS
            this.httpRequestCacheValidator = new HttpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
            this.disableAllCaching         = true;
#endif
#endif //!FEATURE_PAL // IE caching

            FtpCachePolicyElement ftpPolicy = section.DefaultFtpCachePolicy;

            if (ftpPolicy.WasReadFromConfig)
            {
                this.defaultFtpCachePolicy = new RequestCachePolicy(ftpPolicy.PolicyLevel);
            }
        }