asp.nethttp-compression

HTTP Compression: Some external scripts/CSS not decompressing properly some of the time


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?


Solution

  • This is an issue that I have face 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 https://www.giftofspeed.com/gzip-test/ if your pages is 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: ASP.NET site sometimes freezing up and/or showing odd text at top of the page while loading, on load balanced servers