/// <summary>
        /// Sets the caching used for resources on the http response cache policy.
        /// </summary>
        private static void SetCachingPolicy(HttpCachePolicy cachePolicy)
        {
            cachePolicy.SetCacheability(HttpCacheability.ServerAndPrivate);
            cachePolicy.SetNoServerCaching();
            cachePolicy.SetMaxAge(TimeSpan.FromMinutes(5));

            /* NOTE: The two calls below prevent resources from being stored on
             * disk but they also set the expiration to be immediate. Which means
             * that if we preload images (e.x., grid) then the next time they are
             * requested they will again go back to the server. So we cannot use these. */
            // cachePolicy.SetNoStore();
            // cachePolicy.SetCacheability(HttpCacheability.NoCache);
        }
Пример #2
0
 /// <summary>
 ///     Stops all origin-server caching for the current response.
 /// </summary>
 public void SetNoServerCaching()
 {
     policy.SetNoServerCaching();
 }
Пример #3
0
 public override void SetNoServerCaching()
 {
     _httpCachePolicy.SetNoServerCaching();
 }
Пример #4
0
        public void Deny_Unrestricted()
        {
            HttpCachePolicy cache = response.Cache;

            Assert.IsNotNull(cache.VaryByHeaders, "VaryByHeaders");
            Assert.IsNotNull(cache.VaryByParams, "VaryByParams");
            cache.AddValidationCallback(new HttpCacheValidateHandler(Validate), null);
            cache.AppendCacheExtension("mono");
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetCacheability(HttpCacheability.NoCache, "mono");
            cache.SetETag("etag");
            try
            {
                cache.SetETagFromFileDependencies();
            }
            catch (TypeInitializationException)
            {
                // 1.1 tries to initialize HttpRuntime
            }
            catch (InvalidOperationException)
            {
                // expected
            }
            cache.SetExpires(DateTime.MinValue);
            cache.SetLastModified(DateTime.Now);
            try
            {
                cache.SetLastModifiedFromFileDependencies();
            }
            catch (InvalidOperationException)
            {
                // expected
            }
            catch (NotImplementedException)
            {
                // mono
            }
            cache.SetMaxAge(TimeSpan.FromTicks(1000));
            try
            {
                cache.SetNoServerCaching();
            }
            catch (NotImplementedException)
            {
                // mono
            }
            try
            {
                cache.SetNoStore();
            }
            catch (NotImplementedException)
            {
                // mono
            }
            try
            {
                cache.SetNoTransforms();
            }
            catch (NotImplementedException)
            {
                // mono
            }
            cache.SetProxyMaxAge(TimeSpan.FromTicks(2000));
            cache.SetRevalidation(HttpCacheRevalidation.None);
            cache.SetSlidingExpiration(true);
            try
            {
                cache.SetValidUntilExpires(true);
            }
            catch (NotImplementedException)
            {
                // mono
            }
            cache.SetVaryByCustom("custom");
            cache.SetAllowResponseInBrowserHistory(true);

#if NET_2_0
            try
            {
                cache.SetOmitVaryStar(false);
            }
            catch (NotImplementedException)
            {
                // mono
            }
#endif
        }
        void SetCacheFromCacheProfile()
        {
            HttpCachePolicy cache = HttpContext.Current.Response.Cache;

            if (this.cacheProfile.NoStore)
            {
                cache.SetNoStore();
            }

            // Location is not required to be set in the config.  The default is Any,
            // but if it is not set in the config the value will be -1.  So must correct for this.
            if ((int)(this.cacheProfile.Location) == -1)
            {
                cache.SetCacheability(HttpCacheability.Public);
            }
            else
            {
                switch (this.cacheProfile.Location)
                {
                case OutputCacheLocation.Any:
                    cache.SetCacheability(HttpCacheability.Public);
                    break;

                case OutputCacheLocation.Client:
                    cache.SetCacheability(HttpCacheability.Private);
                    break;

                case OutputCacheLocation.Downstream:
                    cache.SetCacheability(HttpCacheability.Public);
                    cache.SetNoServerCaching();
                    break;

                case OutputCacheLocation.None:
                    cache.SetCacheability(HttpCacheability.NoCache);
                    break;

                case OutputCacheLocation.Server:
                    cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                    break;

                case OutputCacheLocation.ServerAndClient:
                    cache.SetCacheability(HttpCacheability.ServerAndPrivate);
                    break;

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.CacheProfileLocationNotSupported, this.cacheProfile.Location)));
                }
            }

            if (this.cacheProfile.Location != OutputCacheLocation.None)
            {
                cache.SetExpires(HttpContext.Current.Timestamp.AddSeconds((double)this.cacheProfile.Duration));
                cache.SetMaxAge(new TimeSpan(0, 0, this.cacheProfile.Duration));
                cache.SetValidUntilExpires(true);
                cache.SetLastModified(HttpContext.Current.Timestamp);

                if (this.cacheProfile.Location != OutputCacheLocation.Client)
                {
                    if (!string.IsNullOrEmpty(this.cacheProfile.VaryByContentEncoding))
                    {
                        foreach (string contentEncoding in this.cacheProfile.VaryByContentEncoding.Split(seperatorChar))
                        {
                            cache.VaryByContentEncodings[contentEncoding.Trim()] = true;
                        }
                    }

                    if (!string.IsNullOrEmpty(this.cacheProfile.VaryByHeader))
                    {
                        foreach (string header in this.cacheProfile.VaryByHeader.Split(seperatorChar))
                        {
                            cache.VaryByHeaders[header.Trim()] = true;
                        }
                    }

                    if (this.cacheProfile.Location != OutputCacheLocation.Downstream)
                    {
                        if (!string.IsNullOrEmpty(this.cacheProfile.VaryByCustom))
                        {
                            cache.SetVaryByCustom(this.cacheProfile.VaryByCustom);
                        }

                        if (!string.IsNullOrEmpty(this.cacheProfile.VaryByParam))
                        {
                            foreach (string parameter in cacheProfile.VaryByParam.Split(seperatorChar))
                            {
                                cache.VaryByParams[parameter.Trim()] = true;
                            }
                        }

                        if (!string.IsNullOrEmpty(this.cacheProfile.SqlDependency))
                        {
                            CacheDependency cacheDependency = this.CreateSingleCacheDependency(cacheProfile.SqlDependency);
                            HttpContext.Current.Response.AddCacheDependency(new CacheDependency[] { cacheDependency });
                        }
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Configures ASP.Net's Cache policy based on properties set
        /// </summary>
        /// <param name="policy">cache policy to set</param>
        void ICachePolicyConfigurer.Configure(HttpCachePolicy policy)
        {
            policy.SetAllowResponseInBrowserHistory(allowInHistory);
            policy.SetCacheability(cacheability);
            policy.SetOmitVaryStar(omitVaryStar);
            policy.SetRevalidation(revalidation);
            policy.SetSlidingExpiration(slidingExpiration);
            policy.SetValidUntilExpires(validUntilExpires);

            if (duration != 0)
            {
                policy.SetExpires(DateTime.Now.AddSeconds(duration));
            }

            if (varyByContentEncodings != null)
            {
                foreach (var header in varyByContentEncodings.Split(','))
                {
                    policy.VaryByContentEncodings[header.Trim()] = true;
                }
            }

            if (varyByCustom != null)
            {
                policy.SetVaryByCustom(varyByCustom);
            }

            if (varyByHeaders != null)
            {
                foreach (var header in varyByHeaders.Split(','))
                {
                    policy.VaryByHeaders[header.Trim()] = true;
                }
            }

            if (varyByParams != null)
            {
                foreach (var param in varyByParams.Split(','))
                {
                    policy.VaryByParams[param.Trim()] = true;
                }
            }

            if (cacheExtension != null)
            {
                policy.AppendCacheExtension(cacheExtension);
            }

            if (setEtagFromFileDependencies)
            {
                policy.SetETagFromFileDependencies();
            }

            if (setLastModifiedFromFileDependencies)
            {
                policy.SetLastModifiedFromFileDependencies();
            }

            if (setNoServerCaching)
            {
                policy.SetNoServerCaching();
            }

            if (setNoStore)
            {
                policy.SetNoStore();
            }

            if (setNoTransforms)
            {
                policy.SetNoTransforms();
            }

            if (etag != null)
            {
                policy.SetETag(etag);
            }

            if (isLastModifiedSet)
            {
                policy.SetLastModified(lastModified);
            }

            if (isMaxAgeSet)
            {
                policy.SetMaxAge(TimeSpan.FromSeconds(maxAge));
            }

            if (isProxyMaxAgeSet)
            {
                policy.SetProxyMaxAge(TimeSpan.FromSeconds(proxyMaxAge));
            }
        }
 public override void SetNoServerCaching()
 {
     _policy.SetNoServerCaching();
 }