Exemple #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
                    }
                }
            }
        }
		protected virtual void SetHttpCachePolicy(HttpCachePolicyBase policy)
		{
			policy.SetCacheability(HttpCacheability.Private);
			policy.SetExpires(DateTime.Now.Add(CacheDuration));
			policy.SetMaxAge(CacheDuration);
			policy.SetValidUntilExpires(true);
		}
        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;
        }
        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));
            }
        }
 protected virtual void SetHttpCachePolicy(HttpCachePolicyBase policy)
 {
     policy.SetCacheability(HttpCacheability.Private);
     policy.SetExpires(DateTime.Now.Add(CacheDuration));
     policy.SetMaxAge(CacheDuration);
     policy.SetValidUntilExpires(true);
 }
 private static void InvalidateCache(HttpCachePolicyBase cache)
 {
     cache.SetExpires(DateTime.UtcNow.AddDays(-1));
     cache.SetValidUntilExpires(false);
     cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
     cache.SetCacheability(HttpCacheability.NoCache);
     cache.SetNoStore();
 }
 protected virtual void SetHttpCachePolicy(HttpCachePolicyBase policy, TemplateInfo info)
 {
     policy.SetCacheability(info.Cacheability);
     policy.SetExpires(DateTime.Now.AddMinutes(info.CacheDuration));
     policy.SetMaxAge(TimeSpan.FromMinutes(info.CacheDuration));
     policy.SetValidUntilExpires(true);
     policy.SetETagFromFileDependencies();
     policy.SetLastModifiedFromFileDependencies();
 }
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;

        cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        cache.SetValidUntilExpires(false);
        cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        cache.SetCacheability(HttpCacheability.NoCache);
        cache.SetNoStore();
    }
        private void AddNoOutputCacheHeaders(AuthorizationContext filterContext)
        {
            HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;

            cachePolicy.SetExpires(DateTime.UtcNow.AddDays(-1));
            cachePolicy.SetValidUntilExpires(false);
            cachePolicy.AppendCacheExtension("must-revalidate, proxy-revalidate");
            cachePolicy.SetCacheability(HttpCacheability.NoCache);
            cachePolicy.SetNoStore();
        }
Exemple #10
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();
        }
Exemple #11
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);
        }
        public new void OnResultExecuted(ResultExecutedContext filterContext)
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;

            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cache.SetValidUntilExpires(false);
            cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            cache.SetNoStore();
            cache.AppendCacheExtension("private");
            cache.AppendCacheExtension("no-cache=Set-Cookie");
            cache.SetProxyMaxAge(TimeSpan.Zero);
        }
        /// <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);
        }
Exemple #14
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; */
        }
        /// <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);
            }
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            if (filterContext.IsChildAction)
            {
                return;
            }

            HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;

            cachePolicy.SetExpires(DateTime.UtcNow.AddDays(-1));
            cachePolicy.SetValidUntilExpires(false);
            //cachePolicy.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cachePolicy.AppendCacheExtension("must-revalidate, proxy-revalidate");
            cachePolicy.SetCacheability(HttpCacheability.NoCache);
            cachePolicy.SetNoStore();

            base.OnResultExecuting(filterContext);
        }
Exemple #17
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;
                }
            }
        }
Exemple #18
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;
                }
            }
        }
        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;
                }
            }
        }
Exemple #20
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();
            }
        }
        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);
            }
        }
Exemple #22
0
        public void ProcessRequest(HttpContextBase context)
        {
            this._context = context;
            HttpRequestBase  request  = context.Request;
            HttpResponseBase response = context.Response;
            Uri    url = request.Url;
            string str = !(url == (Uri)null) ? url.LocalPath : throw new HttpException(500, BundleTransformer.Core.Resources.Strings.Common_ValueIsNull);

            if (string.IsNullOrWhiteSpace(str))
            {
                throw new HttpException(500, BundleTransformer.Core.Resources.Strings.Common_ValueIsEmpty);
            }
            if (!this._virtualFileSystemWrapper.FileExists(str))
            {
                throw new HttpException(404, string.Format(BundleTransformer.Core.Resources.Strings.Common_FileNotExist, (object)str));
            }
            string bundleVirtualPath = request.QueryString[Common.BundleVirtualPathQueryStringParameterName];

            if (string.IsNullOrWhiteSpace(bundleVirtualPath) && this.IsStaticAsset)
            {
                AssetHandlerBase.ProcessStaticAssetRequest(context);
            }
            else
            {
                string processedAssetContent;
                try
                {
                    processedAssetContent = this.GetProcessedAssetContent(str, bundleVirtualPath);
                }
                catch (HttpException ex)
                {
                    throw;
                }
                catch (AssetTranslationException ex)
                {
                    throw new HttpException(500, ex.Message, (Exception)ex);
                }
                catch (AssetPostProcessingException ex)
                {
                    throw new HttpException(500, ex.Message, (Exception)ex);
                }
                catch (FileNotFoundException ex)
                {
                    throw new HttpException(500, string.Format(BundleTransformer.Core.Resources.Strings.AssetHandler_DependencyNotFound, (object)ex.Message, (object)ex));
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, string.Format(BundleTransformer.Core.Resources.Strings.AssetHandler_UnknownError, (object)ex.Message, (object)ex));
                }
                HttpCachePolicyBase cache = response.Cache;
                if (this._assetHandlerConfig.DisableClientCache)
                {
                    response.StatusCode        = 200;
                    response.StatusDescription = "OK";
                    cache.SetCacheability(HttpCacheability.NoCache);
                    cache.SetExpires(DateTime.UtcNow.AddYears(-1));
                    cache.SetValidUntilExpires(false);
                    cache.SetNoStore();
                    cache.SetNoServerCaching();
                    response.ContentType = this.ContentType;
                    response.Write(processedAssetContent);
                }
                else
                {
                    string assetEtag = AssetHandlerBase.GenerateAssetETag(processedAssetContent);
                    int    num       = AssetHandlerBase.IsETagHeaderChanged(request, assetEtag) ? 1 : 0;
                    if (num != 0)
                    {
                        response.StatusCode        = 200;
                        response.StatusDescription = "OK";
                    }
                    else
                    {
                        response.StatusCode        = 304;
                        response.StatusDescription = "Not Modified";
                        response.AddHeader("Content-Length", "0");
                    }
                    response.AddHeader("X-Asset-Transformation-Powered-By", "Bundle Transformer");
                    cache.SetCacheability(HttpCacheability.Public);
                    cache.SetExpires(DateTime.UtcNow.AddYears(-1));
                    cache.SetValidUntilExpires(true);
                    cache.AppendCacheExtension("must-revalidate");
                    cache.SetNoServerCaching();
                    cache.VaryByHeaders["If-None-Match"] = true;
                    cache.SetETag(assetEtag);
                    if (num != 0)
                    {
                        response.ContentType = this.ContentType;
                        response.Write(processedAssetContent);
                    }
                }
                context.ApplicationInstance.CompleteRequest();
            }
        }
		protected virtual void SetHttpCachePolicy(HttpCachePolicyBase policy, TemplateInfo info)
		{
			policy.SetCacheability(info.Cacheability);
			policy.SetExpires(DateTime.Now.AddMinutes(info.CacheDuration));
			policy.SetMaxAge(TimeSpan.FromMinutes(info.CacheDuration));
			policy.SetValidUntilExpires(true);
			policy.SetETagFromFileDependencies();
			policy.SetLastModifiedFromFileDependencies();
		}