views:

38

answers:

1

I am implementing page/resource compression to improve website performance.

I have tried to implement both blowery and wicked HttpCompress but end up getting the same result. This only seems to affect Firefox, I have tested on Chrome and IE.

What happens is the first time I request the page all the external resources decompress ok. The 2nd or 3rd time the page has errors because the resource doesn't seem to be decompressed. I get unicode characters like:

������í½`I%&/mÊ{JõJ×àt¡`$Ø@ìÁÍæìiG#)«*ÊeVe]f

(actually they can't be displayed properly here)

Inspecting the page via firebug displays the response header as:

Cache-Control private

Content-Type text/html; charset=utf-8

Content-Encoding gzip

Server Microsoft-IIS/7.5

X-AspNetMvc-Version 2.0

X-AspNet-Version 2.0.50727

X-Compressed-By HttpCompress

X-Powered-By ASP.NET Date Fri, 09 Jul

2010 06:51:40 GMT Content-Length 2622

This clearly states that the resource is compressed by gzip. So something seems to be going wrong on the deflate side on the client?

I have added the following sections (in the appropriate locations) in the web.config:

<sectionGroup name="blowery.web">
  <section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup>

<blowery.web>
    <httpCompress preferredAlgorithm="gzip" compressionLevel="high">
      <excludedMimeTypes>
        <add type="image/jpeg"/>
        <add type="image/png"/>
        <add type="image/gif"/>
      </excludedMimeTypes>
      <excludedPaths>
        <add path="NoCompress.aspx"/>
      </excludedPaths>
    </httpCompress>
</blowery.web>

<add name="CompressionModule" type="blowery.Web.HttpCompress.HttpModule, blowery.web.HttpCompress"/>

Any help?

+1  A: 

This is an issue that I have see before and the problem is that the Content-Length is not correct. Why is not correct ? because its probably calculate before the compression.

If you set Content-Lenght by hand, just remove it and let the module set it if he can.

I note that you use the Blowery compression. Probably this is a bug/issue inside Blowery. If you can not locate it and fix it, why not use the Ms compression ?

@ptutt if you are on shared iis, then maybe there have all ready set compression, so there is one compression over the other, and you only need to remove yours. If this is the issue then for sure the content-lenght is false because after the first compression, the second is break it.

Check it out using this site http://www.pipeboost.com/report.asp if your pages all ready compressed by default by iis.

If not compressed by default then you can do it very easy. On Global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string cTheFile = HttpContext.Current.Request.Path;
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);

    if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        string acceptEncoding = MyCurrentContent.Request.Headers["Accept-Encoding"].ToLower();;

        if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
        {
            // defalte
            HttpContext.Current.Response.Filter = new DeflateStream(prevUncompressedStream,
                CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
        } else if (acceptEncoding.Contains("gzip"))
        {
            // gzip
            HttpContext.Current.Response.Filter = new GZipStream(prevUncompressedStream,
                CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
        }       
    }
}

Please note, I just write this code and have not tested. My code is a little more complicate, so I just create a simple verion of it.

Find more examples: http://www.google.com/search?q=Response.Filter+GZipStream

Reference: http://stackoverflow.com/questions/2582139/2719080#2719080

Aristos
I was getting the exact same result using "wicked HttpCompress". I don't know about MS compression...this is deployed on a shared server so I don't have access to IIS. My guess is they deliberately don't turn compression on so that the cpu doesn't get overloaded. It's probably debatable how much benefit I will get considering it is on a shared server.I assume I am doing something wrong considering I have the same issue for both compression tools. Maybe it is an IIS configuration? I have only tested this locally.
ptutt
@putt I have update my answer
Aristos
Just implemented this code and it works. With something so simple you gotta wonder why blowery and httpcompress had the same error (maybe they use the same codebase?). Anyway, thanks for the help.
ptutt
@ptutt the error is what I have told you all ready, the length on header.
Aristos