HTTP Compression via HttpModule

I’ve talked about HTTP compression in IIS 6.0, and HTTP compression using Net.WebClient, but what about deploying ASP.NET websites to servers you don’t control, e.g., third party hosts? How can we enable compression in that scenario?

We can implement HTTP compression on a per-website basis in ASP.NET through a compression HttpModule. Sure enough, a little searching dug up the blowery.org HttpCompressionModule. And it works very well; the current version has been around a while, and already reflects a number of significant bugfixes. After modifying the Web.config...

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

...and deploying the two dlls to the bin folder, it passes the sniffer test: the HTTP compression header is set, and the pages are sent to the browser as gzipped binary data.

I did, however, run into one bug related to my ASP.NET Unhandled Exception Handling HttpModule – for some reason, the compression module prevented the exception module from dumping output to the webpage via Response.Write and Response.End. The automatic emails and text logs worked fine, so the code was executing, but the response output methods had no effect. Clearly, some kind of problem related to having two HttpModules running at the same time... a quick change to the blowery source fixed that:

void CompressContent(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
if (app.Server.GetLastError() != null) {
return;
}
[.. rest of code snipped]

Basically, in the case of any Exception – if it bubbles up this far, it’s definitely unhandled – we want to return immediately and do nothing in the compression handler. I am not sure if this is the optimal fix, but without it, all I get is the default .NET exception page. And who wants that?

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments