Understanding Compression and Decompression in ASP.NET 2.0
page 3 of 6
by SANJIT SIL
Feedback
Average Rating: 
Views (Total / Last 10 Days): 34729/ 56

Compressing HTTP output

There are couple of different supported compression formats, but gzip is the most commonly used and compatible format. Most graphical browsers, especially Internet Explorer and Netscape, support gzip decompression. Compression is used on all html output, which means it is applicable to both static and dynamic files. In case of images which are already compressed, they do not need to be touched by an http compression system. We can divide the way of implementation of compression and decompression into three different categories or we can say that there are three different ways of implementation. One is to buy a third-party tool which plugs into the Internet Information Server (IIS) software and enables http compression completely transparently outside of applications. This is good, but costs are involved here. The second option is to use an open source HTTP Compression library specifically written for ASP.NET from the inside of application. The third option is to use IIS built in http compression, but Netscape 4 does not support the same properly. The new System.IO.Compression namespace in .NET 2.0 makes it easy to implement HTTP compression without having to touch IIS. The best thing about it is that we no longer need any third party compression components; it is all built directly into .NET Framework. There are different ways to implement the compression, but I think an Http Module is the right choice for this feature. HTTP compression provides faster transmission time between compression-enabled browsers (Microsoft Internet Explorer 5.0 or later) and IIS. We can either compress static files alone, or both static files and applications. If our network bandwidth is restricted, we should consider HTTP compression, at least for static files. Here I have created a class HttpCompressionModule which implements the Init method and attach the Application.BeginRequest event and the event handler that will do the compression. We have to inherit IHttpModule interface as shown in Listing 2.

Listing 2

public class HttpCompressionModule : IHttpModule

We should write the following in web.config as specified in Listing 3:

Listing 3

<httpModules>
<add type="HttpCompressionModule" name="HttpCompressionModule"/>
</httpModules>

We have to implement the two members (Dispose and Init method) of IHttpModule interface and the same is specified in Listing 4 and 5.

Listing 4

void IHttpModule.Dispose()
}
}

Listing 5

void IHttpModule.Init(HttpApplication context)
{
  context.BeginRequest += new EventHandler(context_BeginRequest);
}

Listing 6

void context_BeginRequest(object sender, EventArgs e)
  {
    HttpApplication app = sender as HttpApplication;
 
    if (IsEncodingAccepted("gzip"))
    {
      app.Response.Filter = new GZipStream (app.Response.Filter,
          CompressionMode.Compress);
      SetEncoding("gzip");
    }
    else if (IsEncodingAccepted("deflate"))
    {
      app.Response.Filter = new DeflateStream (app.Response.Filter, 
          CompressionMode.Compress);
      SetEncoding("deflate");
    }
     
  }

The following two methods (Listing 7 and Listing 8) are for checking the request headers to see if the specified encoding is accepted by the client and adding the specified encoding to the response headers.

Listing 7

private bool IsEncodingAccepted(string encoding)
{
  return HttpContext.Current.Request.Headers["Accept-encoding"] != null &&
      HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
}

Listing 8

private void SetEncoding(string encoding)
{
  HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
}

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 3:29:01 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search