ben lowery

HttpCompress

Jul 7 2008

An open compression engine for ASP.NET

News

Mar 12, 2012 – Moved to GitHub

I decided to move the source over to GitHub. Pull requests happily accepted.

Jan 14, 2008 – Version 7 available

A while ago I worked in a patch for the very commonly reported bug “HttpCompress doesn’t work for documents in the root of a site”, but never cut a release. So, here’s a release with that patch incorporated.

Oct 27, 2007 – Project Hosting now at Google Code

In an effort to allow others to easily contribute to this project, I’m now hosting it at http://code.google.com/p/httpcompress/. Head over there to track issues, contribute patches and keep up to date on what’s going on. I’ll still keep current downloads available here, but the build of the project will be taking place over on the new site.

Nov 15, 2005 – Version 6 for .NET 2.0 released!

This is a pretty simple recompile of the version 6 source, targeting the 2.0 version of the .NET Framework. It now uses the built-in deflate and gzip streams found in System.IO.Compression instead of #ziplib. I consider this a beta release; please test it thoroughly before releasing it onto any production systems. [Binary Only, Source Only]

Apr 21, 2004 – Version 6 released!

Another bugfix release:

  • Changed how the Content-Encoding header is written. It is now written on the first call to the compressing stream’s Write method. This fixes the issue where the response would come back with a header indicating it was compressed when the filter was really skipped. This allows Server.Transfer and the default exception reporting mechanism in ASP.NET to work without modification, though their output will not be compressed.

Mar 19, 2004 – Version 5 released!

This is mainly a bugfix release over version 4. New Features:

  • (v5) Plays nice with the OutputCache using the VaryByHeader property.
  • (v5) No longer installs the filter if the CompressionLevel is set to “None”.
  • (v5) No longer throws an exception if a q-value cannot be parsed.
  • (v5) Properly install the INSTALLED_TAG, preventing double processing when
    a filter is not installed
  • Path-based exclusions
  • ContentType / MimeType based exclusions
  • A newer SharpZipLib

Download

Current Version
Older Versions

Known Bugs

Compression breaks when Server.Transfer is used
In ASP.NET 1.0 and 1.1, Server.Transfer and response filters do not play well together. Thankfully, a fix is available.
Images compressed with the filter are broken
This usually occurs with Internet Explorer 5.5 or 6.0. Both of these browsers can drop the first 2048 bytes of the response, breaking images that are compressed. The situation is documented by KnowledgeBase articles Q312496 for IE 6 and Q313712 for IE 5.5. The latest service pack fixes the issue for IE6, while you have to get a hotfix for IE 5.5.

Articles

OnDotNet -Filtering HTTP Requests with .NET
An article about filtering HTTP requests using ASP.NET. The article was based upon my experiences building the HttpCompressionModule.

36 Responses to “HttpCompress”

  1. chiefconcern says:

    I have a bug I found with this compression module. I have tried to debug but have not found a way. When you use the asp substitution control with this module it causes the substition to not occur.

    Thought?

  2. Ben says:

    Please file a bug over on the code.google.com site.

  3. Benjie says:

    I just want to inquire if this module is supported in ASP.NET Development Server environment. Thanks!

  4. Ben says:

    If that environment supports HttpModules and Response.Filter, it should work. Are you talking about the super-Cassini that ships with the more recent ASP.NET environments?

    That said, you probably want the svn version right now. It fixes a bug with paths to apps that live off the root of the application instead of in a virtual directory.

  5. Flying59 says:

    Thanks Ben, for HttpCompress. I’m using it to compress the data from an ASP.NET web service. I’d like to know if the deflate compression method uses zlib. If so, how can I turn on the zlib headers in the response?

  6. Google code block me, why you host this project else where like SF.Net that is greater? google code is not realy open! becuase of political functions.

  7. Ajay says:

    Hi, its a good tool. Do you have any plans to add functionality to compress webresource.axd files also to compress other resources files which are emitted by some third party components such as Telrik, Infragistics etc.

  8. ????? ??? says:

    hi
    is there any known bug in this module related to ajax?
    i want to use this http module with asp.net ajax and i want to know is there any problem or not?

  9. Paul says:

    Hi Ben,

    I’m using your compression module and it works very well. I use some PageMethods in my application and have noticed that the response from these does not get compressed. Is the mime type of “application/json” not supported at all?

    Thanks.

  10. alexandru says:

    May I have the source code of version 7? I want to apply the code from issue 15 from google code.

  11. Ben says:

    The source is available via Subversion, over on the Google Code project page. See http://code.google.com/p/httpcompress/

  12. Sean says:

    I have configured this according to “http://www.c-sharpcorner.com/UploadFile/Ihelpable/httpcompression05022006094806AM/httpcompression.aspx?ArticleID=b0c4a586-97b4-4fbd-a95a-8b1200f0e068″. I tested compression using “http://www.whatsmyip.org/http_compression/” before and after configuring and it shows to be working. My client is running Windows XP. The site comes up fine after compressing on Firefox and Chrome, but IE7 cannot display the site. Adding a single aspx page to the exception list allows IE7 to display that page. Any suggestions on getting the site to display in IE7?

  13. Roberto says:

    I’m downloading the bin folder, add the reference and modified the web.config, but then al my pages display with weird characters, any solution?

    thanks a lot

  14. Jim says:

    I’ve got this installed on IIS 6.0 and it appears to be working well. Thanks for providing.

    I noticed, however, that the compression is not as good as IIS for some files. I was able to get IIS to compress EVERYTHING by editing the metabase and it compressed my file from 146k to 10k. With HttpCompressionModule, that same file is stuck at 22k.

    Any idea why?

  15. Fred says:

    Today we tried to access our ASP.NET app via Android HTC Dream and it failed with the error: “Invalid Use of Response Filter” and references to GZip.

    Every other browser, including iPhone and Palm Pre, along with all the standard PC/Mac browsers, work fine.

    Any idea why I might be getting the error?

    I found this reference at: http://www.nsilverbullet.net/CategoryView,category,AJAX.aspx

    ***************************
    After doing this I received an “Invalid use of response filter” exception (more info at dasBlog.us) which turns out to be because my host already implements httpCompression, but disabling the blowery handler sorted it out.
    ***************************

    We are running blower.Web.HttpCompress.dll 11/18/07 V. 6.0.0.0 and blowery.Web.HttpCompress.xml at 10/22/07.

    Our web.config is like this:

    Thanks for any advice you can give me.

  16. Fred says:

    Sorry, a few other points and correction. We are running .NET Framework 1.1 and here’s our web.config with the tags changed so that the contents can be viewed:

    [configuration]
    [configSections]
    [sectionGroup name="blowery.web"]
    [section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress" /]
    [/sectionGroup]
    [/configSections]

    [blowery.web]
    [httpCompress preferredAlgorithm="deflate" compressionLevel="high"]
    [excludedMimeTypes]
    [add type="image/jpeg" /]
    [add type="image/jpg" /]
    [add type="image/gif" /]
    [/excludedMimeTypes]
    [excludedPaths]
    [add path="NoCompress.aspx" /]
    [add path="spread11_msoft.aspx" /]
    [add path="ScriptResource.axd" /]
    [add path="WebResource.axd" /]
    [/excludedPaths]
    [/httpCompress]
    [/blowery.web]

    [httpModules]
    [!-- blowery --]
    [add name="CompressionModule" type="blowery.Web.HttpCompress.HttpModule, blowery.web.HttpCompress" /]
    [!-- end blowery --]
    [/httpModules]

  17. Roberto says:

    I try it, no errors , but when I run my site I only get weird symbols, but if I set compressionLevel=”none” everythings works fine again

    Any reason for that behaivor?

    Thanks a lot

  18. Ryan says:

    > Im not hosting it at http://code.google.com/p/httpcompress/

    You surely mean “I’m now” :)

  19. mogadanez says:

    On IIS6 with i map WildCard to Aspnet, it not compress static resources.
    in debugger i not reach CompressContent method

  20. [...] or you serve dynamic content that should not be compressed. A better solution is provided in HttpCompress by Ben Lowery, a configurable HttpModule that allows content to be excluded from compression by [...]

  21. markus says:

    I noticed the same problem as mogadanez: httpCompress will compress static files when run in the Visual Studio Web Developement Server, but not in IIS6 with wildcard mapping set. It does however compress all dynamic requests as expected, including all my extension-less URLs, so the wildcard mapping actually seems to work.

    Anybody any idea on what could be wrong here or how to work around it?

    I understand it could become too CPU intensive for high-traffic sites to compress a lot of static content on the files (without server-side caching of compressed content as IIS could do it itself), but that’s not an issue for me now.

  22. george k says:

    Using YSlow to check the stats on my site, I noticed even with blowery, js and css files are not being compressed. My config states to exclude image files and binaries, but not js or css. This is a .net 2 site. Anyone have any ideas why this is happening? I’ve included the source as is in my project btw, rather than using the precompiled binary. This is just for ease of deployment purposes.

  23. Brian says:

    for those having issues with js files (and other static files) I found adding this:

    To the HTTPHandlers node in web.config seems to work, no idea why as surely thats what its doing by default but there you go.

  24. mike says:

    Brian, what line did you add, i dont see it in your comment.

  25. vps windows says:

    No. You would need a dedicated server. I looked into doing something just as CPU intensive a while back and I kept reaching my limits. I had to move it to a dedicated server which was about $180/month. Good luck!

  26. Mike or Brain, what’s the config entry to have the js file compressed?

  27. NightOwl888 says:

    Great module, however during testing I discovered that the WebResource.axd automated files were blank until I put in an exclusion for it. I am using ASP.NET 2.0 on IIS 5.1 (Windows XP). Is there any explanation why it doesn’t work on those dynamic files?

    Here is a description of the solution I found and how I traced it back to blowery HttpCompress: http://www.rosshawkins.net/archive/2006/10/08/webresource.axd.aspx

    • Ben says:

      Yeah, known issue. Never had to figure what’s going on there. If you can, I would love for someone to figure it out.

      • Alan says:

        Hey there! Thanks for writing the component and making it available. Very helpful!
        The issue with .axd is that the method to check that checks for excluded paths looks for the full file name and not only extensions. The default settings add “.axd” but no file is named just “.axd”! This (basic) change should solve the problem:

        public bool IsExcludedPath(string relUrl)
        {
        foreach (string path in _excludedPaths)
        {
        if (path.Contains(relUrl.ToLower()) || relUrl.ToLower().EndsWith(path))
        return true;
        }
        return false; // _excludedPaths.Contains(relUrl.ToLower());
        }

        BTW, the source of the issue is that axd files are, apparently, already compressed, so this module is double-compressing them. Another option would be to check if the response header already contains the compression on it and, if so, do not add the compression filter again. That may be a better solution but I didn’t investigate to see if it works.

        Thanks!

  28. Amit says:

    Hello,
    first thanks for sharing this wonderful technology. but can i use it for my web application running on .net framework 3.5 and 4?

  29. German says:

    I tried using this on a website running .NET 4 and I keep getting the error below. Any ideas?

    Exception information:
    Exception type: ArgumentOutOfRangeException
    Exception message: Index and count must refer to a location within the string.
    Parameter name: count
    at System.String.RemoveInternal(Int32 startIndex, Int32 count)
    at System.String.Remove(Int32 startIndex, Int32 count)
    at blowery.Web.HttpCompress.HttpModule.CompressContent(Object sender, EventArgs e)

  30. Manish says:

    Are there installation instructions for IIS 7.5?

  31. Mahady says:

    I use this dll for compression .It’s working fine. But when i export a report in world/Excel it’s showing this error

    “Server cannot append header after HTTP headers have been sent. ”

    My Code :

    public static void ExportToWord(GridView gvReport,Panel tdReportInfo,string reportName)
    {
    try
    {
    reportName = reportName + “.doc”;
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.AddHeader(“content-disposition”,
    “attachment;filename=” + reportName + “”);
    HttpContext.Current.Response.Charset = “”;
    HttpContext.Current.Response.ContentType = “application/vnd.ms-word “;
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    tdReportInfo.RenderControl(hw);
    gvReport.RenderControl(hw);
    HttpContext.Current.Response.Output.Write(sw.ToString());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
    }
    catch (Exception ex)
    {
    ReallySimpleLog.WriteLog(ex);

    }
    }

    Any suggestion please ?

  32. Mario says:

    First of all, thank you for this useful code.

    Only a heads-up:
    When compressing a response, Fiddler complains about a HTTP protocol violation, because content-length still has the UNcompressed content size.

    Bye

  33. […] The blowery project Steven Rogers mentioned is a HttpModule as well. […]

Leave a Reply

%d bloggers like this: