Get Started!

How to Gzip HttpHandler Response

It's always a good idea GZip your HttpResponses, but especially if you have a large response.

I'll add a flag to the underlying mechs soon, but for now Add the folowing to any HTTP Handlers class and call the GZipResponse method :

GZipResponse(*oResponse, oRequest);

Here's the source :

public static void GZipResponse(HttpResponse response, HttpRequest request)
{
  var encoding = request.Headers["Accept-Encoding"];
  if( string.IsNullOrEmpty( encoding ) ) { return; }

  if (encoding.Contains("gzip"))
  {
    response.AppendHeader("Content-Encoding", "gzip");
    response.Filter = new System.IO.Compression.GZipStream(
      response.Filter,
      System.IO.Compression.CompressionMode.Compress
    );

    return;        
  }

  if( encoding.Contains("deflate") )
  {
    response.AppendHeader("Content-Encoding", "deflate");
    response.Filter = new System.IO.Compression.DeflateStream(
      response.Filter,
      System.IO.Compression.CompressionMode.Compress
    );
    return;
  }

  // Furture support of differnet types
}