3

I've been trying to implement HTTP compression on a few of my sites for some time now.

I've tried implementing HTTP compression using variations on the following two techniques:

From Application_BeginRequest in Global.asax

From a custom HttpModule

In both cases, it initially seems to work. But, as I navagate around the site, I quickly lose my CSS styling. And the brower reports a JS file contains invalid characters. And, at some point, the entire site turns entirely to jibberish.

I sure wish I knew what was happening here. Does anyone have any suggestion? Is it because these techniques only work on older versions of ASP.NET/IIS?

EDIT: I do not have access to IIS, and should not require it. With the latest iterations of ASP.NET and IIS, more and more control is being given to web.config without touching IIS. This should be fully supported without delving into IIS and, in fact, the two links above claim they are able to do just this. I just can't get it to work for me.

EDIT: On further inspection, using the code in the second link above (the HTTP module), I can see the module handler is being called for all file types on my site. This is clearly part of the problem. (Note that, in some cases, the entire page turns to garbage, which suggests other problems.) Can anyone suggest what determines which files are sent to the HTTP handler, or why the code at that link might work for someone else but not me?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    The compression method you cited only apply to dynamic aspx code. CSS and JS are static files that would be compressed only through configuration of IIS. Those requests are not normally processed by ASP.NET unless you've reconfigured IIS to force them to be. – Samuel Neff Feb 13 '11 at 21:56
  • @Samuel: I have not made any changes that affect which files are processed by ASP.NET. I could tell you more but, frankly, I'm not sure where else to look. – Jonathan Wood Feb 14 '11 at 19:58
  • @Samuel: I spent a bit of time today trying to figure out exactly what is happening. Using the code from the second link in my question (creating an HTTP module), I can see the module handler is being called for all file types (pages, CSS, javascript, and images). This is clearly part of the problem. Why does it behave differently for me? I didn't see where the article mentioned file types. However, note that eventually the entire page seems to turn to gibberish, so I may have more than one problem. – Jonathan Wood Feb 14 '11 at 20:03
  • @Jonathan Wood, use an http proxy to see what is actually being sent over the wire. I prefer Charles (http://www.charlesproxy.com/) but Fiddler works well too. When you say "gibberish", can you actually tell what it is? Is it gzip'd but not being labeled as gzip'd and thus not being decompressed properly on the client? Perhaps it's double gzipped? – Samuel Neff Feb 14 '11 at 22:27
  • @Samuel: I've been busy on other projects but modified the code I got from the second link I posted to compress only if the extension is "." or ".aspx". A couple of quick tests seems to suggest this works. I still don't understand why non-ASP.NET pages are being handled or how the original code could've worked. "Gibberish" meant an unformatted page full of meaningless characters, as though I was viewing compressed data. I'm reluctant to put this on the web until it's working. Will those proxy apps work locally? What would I be looking for? – Jonathan Wood Feb 15 '11 at 00:41
  • @Samuel: Please see my own answer to my question. I think I figured out what has changed. Thanks. – Jonathan Wood Feb 15 '11 at 00:57
  • @Jonathan Wood, yes, Charles and other http proxies will definitely work locally. They're great tools. I understand what you mean by gibberish but my point is that what people see as gibberish is almost never truly random data. It isn't what you expect, but it is something explainable, it's just a matter of determining what exactly is happening to the data which usually results in identifying the problem. Perhaps your custom http module was compressing the data but not sending headers for those file types to indicate they were compressed. – Samuel Neff Feb 15 '11 at 02:27
  • @Samuel: Well, I'm not new to computers. I know it comes from somewhere. But if, for example, I was looking at compressed data that was not decompressed, that would certainly look like gibberish to a human. At least, that's the idea I was trying to convey. – Jonathan Wood Feb 15 '11 at 02:30

2 Answers2

3

IIS 7 supports compression natively - there should not be any coding required. Unless, perhaps, you are trying to implement it on a hosting provider that has not enabled it, but that is not made clear in your question

Peter T. LaComb Jr.
  • 2,935
  • 2
  • 29
  • 44
  • As demonstrated in the links to the techniques I've been trying, I'm want to handle this on the ASP.NET side. Given the improved integration between ASP.NET 4.0 and IIS7, this should be possible. And, looking at those links, it appears people are doing it. – Jonathan Wood Feb 14 '11 at 20:00
1

While I may have had a few issues going on here, the following quote from Walkthrough: Creating and Registering a Custom HTTP Module seems to illuminate the problem:

If the ASP.NET application is running under IIS 6.0, you can use HTTP modules to customize requests for resources that are serviced by ASP.NET. This includes ASP.NET Web pages (.aspx files), Web services (.asmx files), ASP.NET handlers (.ashx files), and any file types that you have mapped to ASP.NET. If the ASP.NET application is running under IIS 7.0, you can use HTTP modules to customize requests for any resources that are served by IIS. This includes not just ASP.NET resources, but HTML files (.htm or .html files), graphics files, and so on.

As I was beginning to suspect, this is a change in ASP.NET 4.0 and IIS7. As I've mentioned several times, these are becoming more and more integrated. And the quote above suggests that HTTP modules now handle all files types served by IIS.

So that's why the older code doesn't work for me. And the fix appears to be to simply check the file type type from my handler.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466