(See updated post: New & Shiny WebResource.axd compression Module)
This post is an update for my previews post about http compression.
In that post, I posted a http module that compress aspx pages and WebResource.axd files.
Thanks to Mads Kristensen, that found a performance issue in my code and had a solution for that,
I improved my compressor component and also I included a complete project to compress asp.net pages,
The implementation in really very simple and not lot of changes are necessary in existing project.
The page compression is very simple, and common to all http compressors:
Registering the httpmodule in the web.config:
<add name="HttpCompressionModule" type="Miron.HttpHandlersAndModules.HttpCompressionModule"/>
and the compression itself in the httpmodule:
HttpApplication app = sender as
// Check if GZIP is supported by the client
app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
SetEncodingType(GZIP); // Set the response encoding type header
// Check if DEFLATE is supported by the client
else if (IsDeflateEncodingSupported())
app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
SetEncodingType(DEFLATE); // Set the response encoding type header
The WebResource.axd compression is a bit more complicate, but the idea is to make a 'fake' request
based on the requested url when the client asked for WebResource.axd file,
get the response into byte, compress it into MemoryStream cache it as a byte and send it to the client.
The next time the client ask for this WebResource, it served from the cache (thanks again to Mads Kirstensen).
The CSS and the JS files compression is similar to the page compression, the differents are that in those cases, we load the requested file,
read it into string, manipulate it (remove unneeded spaces and stuff), compress it, cache it, and send it to the client.
The 'problem' is that css and js files are served by the IIS itself, and not by ASP.NET engine,
so what we do is, changing the 'src' attribute for js files to jslib.axd or the 'href' attribute for css files to css.axd, and add as query string the real files name
(see in the ReadMe.txt file how it done), and those files are handled by ASP.NET engine and to compress tham - for that, we register the following handlers in the web.config: (another option, is to serve those files via WebResources)
<add verb="*" path="css.axd" type="Miron.HttpHandlersAndModules.CssCompressionHandler" validate="false"/>
The caching for those files have CacheDependency on the files, so the content is removed from the cache in the moment the files are changed,
and reload again when requested.
Before sending data to the client, we check the ETag, and if its the same as the client sent, so we can stop the response and send only the code 304.
By configuration in the web.config, it is possible to disable any compression type, decide the time (by days) the cached file will be in the cache,
and also it is possible to specified mimeTypes content to not compress or specified aspx files to exclude from the compression.
All configuration description in the ReadMe.txt file.
All implementation instructions are in the attach zip file in ReadMe.txt file.
The WebResource compression may not work if the request is url that modified in the windows 'host' file.
In that case, you will need to disable WebResource compression (See in the ReadMe.txt)
(See update post with new & better code: New & Shiny WebResource.axd compression Module)
Latest code can be downloaded from: