How are file types interpreted in browsers?

Zangmonkey

Supreme [H]ardness
Joined
Jul 6, 2005
Messages
5,064
How is this jpg animated?

http://i52.tinypic.com/24fcith.jpg

In chrome, with I right-click > Save
the extension is still .jpg

Then, if I point to the file-path in the browser I see the same animated image.

EDIT: In IE the right-click > save will give it a .gif extension.

EDIT2: So the response packet labels it as a gif... interesting that the browser will disregard the filename.
 
Last edited:
It's a gif. The extension is completely arbitrary as long as the server sends the right MIME type. The filename could be octopus.bacon and it would still be an animated gif.
 
What CEpeep said. You can configure a web server to map a MIME type or script interpreter to any extension you want. There are certain default mappings out of the box, but you can change those or even make up your own.
 
...and when you try to access a binary resource (image, PDF) from a server and raw text dumps to your browser, you have found a site whose MIME types are F-ed up. :)
 
In Opera, it goes by the mime type it gets from the server (or extension for local files). In "Ctrl + F12 -> advanced -> downloads", you can configure the handler for each mime type (the default application is gotten from the registry). You can also add extensions to a mime type's list.

So, the mime type the file is served with triggers the handling for that mime type in the list mentioned above. The extension doesn't really matter. But, if you wanted to serve a jpeg as image/jpeg with a filename of test.txt, when you try to save the file in Opera, Opera will override the .txt extension and force a .jpg one. If you don't like that, you'd have to add .txt to the image/jpeg type in the preferences mentioned above. This describes the same issue you're seeing with Opera forcing a gif extension on the pic you linked.

There are other types that are special like application/octet-stream where no extensions are mentioned in the mime type's list. The file just gets downloaded with the same filename and extension as given.

The extension also doesn't matter in Opera's built-in mail client for email attachments. If you have:

-----
Content-Type: text/plain; name="file.doc"
Content-Disposition: attachment; filename="file.doc"
Content-Transfer-Encoding: base64

base64_data
-----

, Opera will treat it as a text file. The mime type needs to be applicaton/msword for it to be treated correctly as a doc file.

There is opera:config#Trust%20Server%20Types in Opera though (off by default) which means "Determine file handling by MIME type exclusively" that can make Opera be more strict.

There are special details with <embed> and <object> resource loading too.

IE supports a no-sniff header too. <http://blogs.msdn.com/b/ie/archive/2010/10/26/mime-handling-changes-in-internet-explorer.aspx>

Firefox sometimes sniffs files and overrides their mime type.

There are also different behaviors depending on whether the part before '/' is "application" or "text".

Also, see this draft <http://tools.ietf.org/html/draft-ietf-websec-mime-sniff-03> for more info.
 
Back
Top