Getting around IE’s MIME type mangling

For some reason, I ended up with the MSDN article on MIME type ignoring open long enough that I actually read it. If you aren’t familiar with this misfeature, well, it’s a “everyone’s at fault” thing where Apache serves up files with unknown extensions with a default MIME type, rather than with no MIME type at all, so Internet Explorer only rarely believes the server-provided MIME type, and instead looks at the content and decides what it really is.

As a result, non-IE users quite often provide examples of things like HTML or RSS or Movable Type templates by just adding .txt to the filename so they will be served as text/plain, and IE blissfully displays them as HTML or XML, or refuses to display them because they aren’t valid XML.

There are 26 MIME types that IE “knows,” plus the two it doesn’t trust: text/plain and application/octet-stream. If your type isn’t any of those, IE will accept it without munging. If your type is one of the 28 special types, IE will decide for itself whether to believe you or not. The interesting part is that it decides by looking at just a buffer containing up to the first 256 bytes of the file. If you are serving up a file that contains angle brackets as text/plain, and you can somehow avoid using anything that makes IE think it’s really something else for 256 bytes, then you can avoid having IE users email you to say that your example can’t be displayed.

The very nice thing is that comments don’t count against text. Start with a comment over 256 bytes long and you can do anything you like in the rest of the file, and IE will still display it as text. Unfortunately, you can’t put anything before the XML declaration that way, so I don’t know of a way to stop IE from trying to interpret XML-as-text as XML, but for HTML, or PHP source that otherwise might look like HTML, a nice fat comment to start will fool IE into thinking you actually know what you’re talking about.

31 Comments

Comment by Geof #
2004-04-06 14:47:24

God help me, but I just started sucking my thumb, reading this.

:sigh:

 
Comment by Jim Dabell #
2004-04-06 16:17:01

If you aren’t familiar with this misfeature, well, it’s a ”everyone’s at fault” thing where Apache serves up files with unknown extensions with a default MIME type, rather than with no MIME type at all, so Internet Explorer only rarely believes the server-provided MIME type, and instead looks at the content and decides what it really is.

It certainly isn’t an ”everyone’s at fault” thing. From RFC 2616 (the HTTP 1.1 specification):

If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource.

Basically, any attempt at file type guessing when a Content-Type header is supplied is a flat out violation of the specification. The blame entirely lies with Internet Explorer.

Of course, now Opera has an option to do the same thing, and I heard Firefox 0.8 added this misfeature as well.

Comment by Scott Johnson #
2004-04-06 16:59:13

For at least 75% of the users out there, this is defnitely a ”feature”. My grandma doesn’t want to know about mime types. She just wants the web page to display properly. She doesn’t care about XML. She just wants to be able to read the newspaper article on the web.

I’m not trying to take Microsoft’s side with this issue. In fact, I disagree with violating the standards, but sometimes it just has to be done in order to make the sale. And when you let a corporation control your browsing experience, you get what sells.

Comment by Georg Bauer #
2004-04-07 06:34:51

Your grandma will die eventually – do you really want to build up a legacy for generations to come, just to help your grandma along?

Comment by Phil Ringnalda #
2004-04-07 09:25:19