二、页面GZIP压缩
对页面GZIP压缩几乎是每篇讲解高性能WEB程序的几大做法之一,因为使用GZIP压缩可以降低服务器发送的字节数,能让客户感觉到网页的速度更快也减少了对带宽的使用情况。当然,这里也存在客户端的浏览器是否支持它。因此,我们要做的是,如果客户端支持GZIP,我们就发送GZIP压缩过的内容,如果不支持,我们直接发送静态文件的内容。幸运的是,现代浏览器IE6.7.8.0,火狐等都支持GZIP。
为了实现这个功能,我们需要改写上面的 Application_BeginRequest 事件:
private void Application_BeginRequest( object sender, EventArgs e ) {
var context = HttpContext.Current;
var request = context.Request;
var url = request.RawUrl;
var response = context.Response;
var path = GetPath( url );
var file = new FileInfo( path );
// 使用页面压缩
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 );
}
}
if ( DateTime.Now.Subtract( file.LastWriteTime ).TotalMinutes < 5 ) {
response.TransmitFile( path );
response.End();
return;
}
try {
var stream = file.OpenWrite();
response.Filter = new CommonFilter( response.Filter, stream );
}
catch ( Exception ) {
//Log.Insert("");
}
}
private ResponseCompressionType GetCompressionMode( HttpRequest request ) {
string acceptEncoding = request.Headers[ "Accept-Encoding" ];
if ( string.IsNullOrEmpty( acceptEncoding ) )
return ResponseCompressionType.None;
acceptEncoding = acceptEncoding.ToUpperInvariant();
if ( acceptEncoding.Contains( "GZIP" ) )
return ResponseCompressionType.GZip;
else if ( acceptEncoding.Contains( "DEFLATE" ) )
return ResponseCompressionType.Deflate;
else
return ResponseCompressionType.None;
}
private enum ResponseCompressionType {
None,
GZip,
Deflate
}
var context = HttpContext.Current;
var request = context.Request;
var url = request.RawUrl;
var response = context.Response;
var path = GetPath( url );
var file = new FileInfo( path );
// 使用页面压缩
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 );
}
}
if ( DateTime.Now.Subtract( file.LastWriteTime ).TotalMinutes < 5 ) {
response.TransmitFile( path );
response.End();
return;
}
try {
var stream = file.OpenWrite();
response.Filter = new CommonFilter( response.Filter, stream );
}
catch ( Exception ) {
//Log.Insert("");
}
}
private ResponseCompressionType GetCompressionMode( HttpRequest request ) {
string acceptEncoding = request.Headers[ "Accept-Encoding" ];
if ( string.IsNullOrEmpty( acceptEncoding ) )
return ResponseCompressionType.None;
acceptEncoding = acceptEncoding.ToUpperInvariant();
if ( acceptEncoding.Contains( "GZIP" ) )
return ResponseCompressionType.GZip;
else if ( acceptEncoding.Contains( "DEFLATE" ) )
return ResponseCompressionType.Deflate;
else
return ResponseCompressionType.None;
}
private enum ResponseCompressionType {
None,
GZip,
Deflate
}