/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> /// <exception cref="System.Web.HttpException">404;Not found</exception> public void ProcessRequest(HttpContext context) { if (this.FileExists(context.Request.Url.AbsolutePath)) { using (var fileStream = this.OpenFile(context.Request.Url.AbsolutePath)) { var fileName = VirtualPathUtility.GetFileName(context.Request.Url.AbsolutePath); var buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); context.Response.ContentType = ResourceHttpHandler.GetMimeMapping(fileName); if (fileName.EndsWith("css", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith("js", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith("html", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith("htm", StringComparison.OrdinalIgnoreCase)) { var cache = context.Response.Cache; cache.SetCacheability(HttpCacheability.Public); cache.SetExpires(DateTime.Now + TimeSpan.FromDays(7)); cache.SetValidUntilExpires(true); var lastWriteTime = ResourceHttpHandler.GetAssemblyLastWriteTime(); cache.SetLastModified(lastWriteTime); } this.WriteToOutput(context, buffer); } } else { throw new HttpException(404, "Not found"); } }
/// <summary> /// Writes the contents of a static resource to the response. /// </summary> /// <param name="context">The context.</param> /// <param name="fileStream">The file stream.</param> /// <param name="fileName">Name of the file.</param> protected virtual void SendStaticResource(HttpContext context, Stream fileStream, string fileName) { var buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); context.Response.ContentType = ResourceHttpHandler.GetMimeMapping(fileName); this.WriteToOutput(context, buffer); }
/// <summary> /// Sets client cache for the requested resource. In debug mode this caching is disabled. /// </summary> /// <param name="context">The context.</param> /// <param name="fileName">Name of the file.</param> protected virtual void SetResponseClientCache(HttpContext context, string fileName) { #if !DEBUG if (fileName.EndsWith(".css", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".js", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".html", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".htm", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".sf-cshtml", StringComparison.OrdinalIgnoreCase)) { var cache = context.Response.Cache; cache.SetCacheability(HttpCacheability.Public); cache.SetExpires(DateTime.Now + TimeSpan.FromDays(7)); cache.SetValidUntilExpires(true); var lastWriteTime = ResourceHttpHandler.GetAssemblyLastWriteTime(); cache.SetLastModified(lastWriteTime); } #endif }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> /// <exception cref="System.Web.HttpException">404;Not found</exception> public void ProcessRequest(HttpContext context) { if (this.FileExists(context.Request.Url.AbsolutePath)) { using (var fileStream = this.OpenFile(context.Request.Url.AbsolutePath)) { var fileName = VirtualPathUtility.GetFileName(context.Request.Url.AbsolutePath); var buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); this.WriteToOutput(context, buffer); context.Response.ContentType = ResourceHttpHandler.GetMimeMapping(fileName); } } else { throw new HttpException(404, "Not found"); } }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> /// <exception cref="System.Web.HttpException">404;Not found</exception> public void ProcessRequest(HttpContext context) { if (this.FileExists(context.Request.Url.AbsolutePath)) { using (var fileStream = this.OpenFile(context.Request.Url.AbsolutePath)) { var fileName = VirtualPathUtility.GetFileName(context.Request.Url.AbsolutePath); using (var strReader = new StreamReader(fileStream)) { var fileContent = strReader.ReadToEnd(); context.Response.Write(fileContent); } context.Response.ContentType = ResourceHttpHandler.GetMimeMapping(fileName); } } else { throw new HttpException(404, "Not found"); } }
/// <summary> /// Writes the contents of a static resource to the response. /// </summary> /// <param name="context">The context.</param> /// <param name="fileStream">The file stream.</param> /// <param name="fileName">Name of the file.</param> protected virtual void SendStaticResource(HttpContext context, Stream fileStream, string fileName) { var buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); context.Response.ContentType = ResourceHttpHandler.GetMimeMapping(fileName); // if the resource was requested with a version param it can be cached by cdns var versionParam = context.Request.QueryString["v"]; if (!string.IsNullOrEmpty(versionParam)) { var staticResourcesAge = Config.Get <SystemConfig>().CacheSettings.MaxAgeForStaticResources; if (staticResourcesAge.HasValue) { context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(staticResourcesAge.Value)); } } this.WriteToOutput(context, buffer); }