Example #1
0
 /// <summary>
 /// Send headers to cache the response. Only caches on the server-side for now
 /// </summary>
 private void ConfigureCaching()
 {
     _response.AddFileDependency(_fileSystem.MapPath(_request.Url.LocalPath));
     _response.Cache.SetCacheability(HttpCacheability.Server);
     _response.Cache.SetLastModifiedFromFileDependencies();
     _response.Cache.SetETagFromFileDependencies();
 }
        /// <summary>
        /// Adds the HTTP headers for cache expiry, and status code to the current response.
        /// </summary>
        /// <param name="fullPath">The full virtual path of the file to add cache settings for.</param>
        /// <param name="modifiedSinceHeader">The incoming modified since header sent by the browser.</param>
        public void AddStatusCodeForCache(string fullPath, string modifiedSinceHeader)
        {
            if (_context != null)
            {
                // https://developers.google.com/speed/docs/best-practices/caching
                _context.AddFileDependency(fullPath);

                FileInfo info = new FileInfo(fullPath);
                _context.Cache.SetCacheability(HttpCacheability.Public);
                _context.Headers.Add("Expires", "-1");                 // always followed by the browser
                _context.Cache.SetLastModifiedFromFileDependencies();  // sometimes followed by the browser
                int statusCode = GetStatusCodeForCache(info.LastWriteTimeUtc, modifiedSinceHeader);

                _context.StatusCode = statusCode;
                StatusCode          = statusCode;
            }
        }
Example #3
0
 private void SetFileCaching(HttpResponseBase response, string fileName)
 {
     response.AddFileDependency(fileName);
     response.Cache.SetETagFromFileDependencies();
     response.Cache.SetLastModifiedFromFileDependencies();
     response.Cache.SetCacheability(HttpCacheability.Public);
     response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));
     response.Cache.SetSlidingExpiration(true);
 }
Example #4
0
        /// <summary>
        /// Executes the handler. Outputs JavaScript to the response.
        /// </summary>
        public void Execute()
        {
            var relativePath = _request.Url.LocalPath;
            var result       = _environment.JsxTransformer.TransformJsxFile(relativePath);

            // Only cache on the server-side for now
            _response.AddFileDependency(_fileSystem.MapPath(relativePath));
            _response.Cache.SetCacheability(HttpCacheability.Server);
            _response.Cache.SetLastModifiedFromFileDependencies();
            _response.Cache.SetETagFromFileDependencies();

            _response.ContentType = "text/javascript";
            _response.Write(result);
        }
Example #5
0
        //TODO: check file path does not contain ".."
        public override void OnResultExecuting(ResultExecutingContext ctx)
        {
            HttpRequestBase  request  = ctx.RequestContext.HttpContext.Request;
            HttpResponseBase response = ctx.RequestContext.HttpContext.Response;
            var result = ctx.Result as FilePathResult;

            if (result == null)
            {
                base.OnResultExecuting(ctx);
                return;
            }

            if (!File.Exists(result.FileName))
            {
                response.StatusCode        = 404;
                response.StatusDescription = "Not Found";
                ctx.Cancel = true;
                return;
            }

            response.AddFileDependency(result.FileName);

            //Expensive to great eTags for large files and is not really necessary since we are using LastModified Date
            //response.Cache.SetETagFromFileDependencies();

            response.Cache.SetLastModifiedFromFileDependencies();

            response.Cache.SetCacheability(HttpCacheability.Public);

            if (_maxAge > -1)
            {
                response.Cache.SetMaxAge(new TimeSpan(0, 0, 0, _maxAge));
            }

            response.Cache.SetSlidingExpiration(true);

            if ((request.Headers["If-Modified-Since"] != null) &&
                new FileInfo(result.FileName).LastWriteTime <= DateTime.Parse(request.Headers["If-Modified-Since"]).AddSeconds(1))
            {
                response.StatusCode = 304;
                //response.Headers.Add("Content-Encoding", "gzip");
                response.StatusDescription = "Not Modified";
                ctx.Cancel = true;
            }
            else
            {
                base.OnResultExecuting(ctx);
            }
        }
 public override void AddFileDependency(string filename)
 {
     _httpResponseBase.AddFileDependency(filename);
 }
 public override void AddFileDependency(string filename)
 {
     proxiedResponse.AddFileDependency(filename);
 }