/// <summary> /// Set the compression type used in the response. /// </summary> /// <param name="response">An HTTP response.</param> /// <param name="responseCompressionType">The compression type to use in the response.</param> public void SetContentEncoding(HttpResponseBase response, ResponseCompressionType responseCompressionType) { if (responseCompressionType != ResponseCompressionType.None) { AppendHeader(response, HttpResponseHeader.ContentEncoding, responseCompressionType.ToString().ToLower()); } }
/// <summary> /// Set the compression type used in the response. /// </summary> /// <param name="response">An HTTP response.</param> /// <param name="responseCompressionType">The compression type to use in the response.</param> public void SetContentEncoding(HttpResponseBase response, ResponseCompressionType responseCompressionType) { if (responseCompressionType != ResponseCompressionType.None) { AppendHeader(response, HttpResponseHeader.ContentEncoding, responseCompressionType.ToString().ToLower()); } }
private static void ProduceResponseHeader(HttpResponse response, int count, ResponseCompressionType mode, string physicalFilePath, DateTime lastModified) { response.Buffer = false; response.BufferOutput = false; response.ContentType = MimeMapping.GetMimeMapping(physicalFilePath); if (mode != ResponseCompressionType.None) { response.AppendHeader("Content-Encoding", mode.ToString().ToLower()); } response.AppendHeader("Content-Length", count.ToString(CultureInfo.InvariantCulture)); response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate"); response.Cache.SetMaxAge(_defaultCacheDuration); response.Cache.SetExpires(DateTime.Now.Add(_defaultCacheDuration)); response.Cache.SetLastModified(lastModified); }
//方法执行前 public override void OnActionExecuted(ActionExecutedContext filterContext) { //压缩 string EncodingString = filterContext.HttpContext.Request.Headers["Accept-Encoding"].ToString();; if (string.IsNullOrWhiteSpace(EncodingString)) { return; } else { //if (EncodingString.ToLower().Contains("gzip")) //{ // var responses = filterContext.HttpContext.Response; // responses.AddHeader("context-encoding", "gzip"); // responses.Filter = new GZipStream(responses.Filter, CompressionMode.Compress); //} #region 压缩有关(百度方式,页面压缩) var context = HttpContext.Current; var request = context.Request; var response = context.Response; ResponseCompressionType compressionType = this.GetCompressionMode(request); if (compressionType != ResponseCompressionType.None) { response.AppendHeader("Content-Encoding", compressionType.ToString().ToLower()); if (compressionType == ResponseCompressionType.GZip) { response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); } else { response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } } #endregion } base.OnActionExecuted(filterContext); }
private void ProduceResponseHeader(HttpResponse response, int count, ResponseCompressionType mode, string physicalFilePath, DateTime lastModified) { response.Buffer = false; response.BufferOutput = false; // Emit content type and encoding based on the file extension and // whether the response is compressed response.ContentType = MimeMapping.GetMimeMapping(physicalFilePath); if (mode != ResponseCompressionType.None) { response.AppendHeader("Content-Encoding", mode.ToString().ToLower()); } response.AppendHeader("Content-Length", count.ToString()); // Emit proper cache headers that will cache the response in browser's // cache for the default cache duration response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate"); response.Cache.SetMaxAge(DEFAULT_CACHE_DURATION); response.Cache.SetExpires(DateTime.Now.Add(DEFAULT_CACHE_DURATION)); response.Cache.SetLastModified(lastModified); }
private void ProduceResponseHeader(HttpResponse response, int count, ResponseCompressionType mode, string physicalFilePath, DateTime lastModified) { response.Buffer = false; response.BufferOutput = false; // Emit content type and encoding based on the file extension and // whether the response is compressed response.ContentType = MimeMapping.GetMimeMapping(physicalFilePath); if (mode != ResponseCompressionType.None) response.AppendHeader("Content-Encoding", mode.ToString().ToLower()); response.AppendHeader("Content-Length", count.ToString()); // Emit proper cache headers that will cache the response in browser's // cache for the default cache duration response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate"); response.Cache.SetMaxAge(DEFAULT_CACHE_DURATION); response.Cache.SetExpires(DateTime.Now.Add(DEFAULT_CACHE_DURATION)); response.Cache.SetLastModified(lastModified); }
public virtual IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, object state) { this._context = context; HttpResponse response = context.Response; HttpRequest request = context.Request; try { EnsureProperRequest(request); string physicalFilePath = request.PhysicalPath; ResponseCompressionType compressionType = this.GetCompressionMode(request); FileInfo file = new FileInfo(physicalFilePath); string fileExtension = file.Extension.ToLower(); // Do we handle such file types? if (Array.BinarySearch <string>(FILE_TYPES, fileExtension) >= 0) { // Yes we do. // If this is a binary file like image, then we won't compress it. if (Array.BinarySearch <string>(COMPRESS_FILE_TYPES, fileExtension) < 0) { compressionType = ResponseCompressionType.None; } // If the response bytes are already cached, then deliver the bytes directly from cache string cacheKey = typeof(StaticFileHandler).ToString() + ":" + compressionType.ToString() + ":" + physicalFilePath; if (DeliverFromCache(context, request, response, cacheKey, physicalFilePath, compressionType)) { // Delivered from cache } else { if (file.Exists) { // When not compressed, buffer is the size of the file but when compressed, // initial buffer size is one third of the file size. Assuming, compression // will give us less than 1/3rd of the size using (MemoryStream memoryStream = new MemoryStream( compressionType == ResponseCompressionType.None ? Convert.ToInt32(file.Length) : Convert.ToInt32((double)file.Length / 3))) { ReadFileData(compressionType, file, memoryStream); this.CacheAndDeliver(context, request, response, physicalFilePath, compressionType, cacheKey, memoryStream, file); } } else { throw new HttpException((int)HttpStatusCode.NotFound, request.FilePath + " Not Found"); } } } else { this.TransmitFileUsingHttpResponse(request, response, physicalFilePath, compressionType, file); } return(new HttpAsyncResult(callback, state, true, null, null)); } catch (Exception x) { if (x is HttpException) { HttpException h = x as HttpException; response.StatusCode = h.GetHttpCode(); Debug.WriteLine(h.Message); } return(new HttpAsyncResult(callback, state, true, null, x)); } }