Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I detect if content has been compressed in my HttpModule?

Tags:

c#

asp.net

iis

I have an HttpModule which is used to dynamically compress content from an ASP.NET (MVC3) web application. The approach is very similar to the CompressionModule in this article (where the module applies a GZip filter to the HttpResponse and sets the correct Content-encoding header).

For one reason and another, this needs to run in classic mode, not integrated pipeline mode.

The problem I've got, is that on some servers that have IIS compression enabled, IIS compresses the content and then my module compresses that.

The upshot is that I get content compressed twice, with an encoding:

Content-encoding: gzip,gzip

one from IIS, and one from this line in my code:

 httpResponse.AppendHeader("Content-encoding", "gzip");

Does anyone know a way, in classic mode, that I can check to see if the content is already compressed, or if compression is enabled on the server, in order to bypass my own compression?

In pipeline mode, this check is as simple as

if (httpResponse.Headers["Content-encoding"]!= null)
{
   return;
}

i.e. check if anything has already set a content-encoding and if so, do nothing.

However, I'm stumped in classic mode. Unfortunately, accessing HttpResponse.Headers is not allowed in classic mode, so I can't do my barrier check.

All ideas gratefully received.

like image 906
Rob Levine Avatar asked Mar 25 '11 15:03

Rob Levine


1 Answers

Theoretically, you can use reflection to peek into HttpRequest._cacheHeaders field, where ASP.NET apparently stores all yet-to-be sent headers in classic mode:

if (this._wr is IIS7WorkerRequest)
{
    this.Headers.Add(HttpResponseHeader.MaybeEncodeHeader(name), HttpResponseHeader.MaybeEncodeHeader(value));
}
else if (flag)
{
    if (this._cacheHeaders == null)
    {
        this._cacheHeaders = new ArrayList();
    }
    this._cacheHeaders.Add(new HttpResponseHeader(knownResponseHeaderIndex, value));
}
like image 133
Anton Gogolev Avatar answered Oct 14 '22 10:10

Anton Gogolev