Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does IIS require mime-types to be declared?

In one of my assignments I was trying to render an SVG image. It took me some time to come across one link which states that, in order for IIS express to render an SVG image, you have to include the following code in your web.config file

<staticContent>
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>

It worked. But I don't understand why/how? Previously I thought that the server should send the correct Content-Type header. But my svg code was in Javascript. I thought (maybe wrongly) that IIS sends only HTML files to the client side, and those HTML files have the Javascript linked in the header. So, does that mean that IIS is scanning all the javascript files associated with the HTML too? That seems a little hard to believe. If I have 10 javascript files in my HTML main file, and one of them has an SVG file in it, does that mean IIS will scan all of them, and then find out SVG is missing? Is this how it works?

Thanks

like image 301
SandBag_1996 Avatar asked Aug 21 '12 15:08

SandBag_1996


People also ask

Why is MIME type needed?

MIME types describe the media type of content, either in email, or served by web servers or web applications. They are intended to help provide a hint as to how the content should be processed and displayed. Examples of MIME types: text/html for HTML documents.

What is MIME type in IIS?

If IIS does not recognize the file name extension requested by the client, IIS sends the content as the default MIME type, which is Application. This MIME type signifies that the file contains application data, and it usually means that clients cannot process the file.

Why should MIME type information be essentially included in HTTP responses?

Setting the correct MIME data type in the body of the message is critical for both HTTP requests and responses and allows you to control how the request is interpreted by the client and server. Web servers and browsers have a list of well-known file extensions and MIME types.


1 Answers

When your browser hits a URL, it initially only downloads the HTML. For every linked file (Javascript, images, CSS, SVG, etc), the browser will make a separate request to the server. And as you noted, IIS won't serve those files unless it recognizes the MIME type.

To answer your question, no, that's not how it works. IIS doesn't scan the HTML files, it just responds passively to requests from the client (browser). It's the browser that parses the HTML and Javascript, and executes the Javscript, making an additional round-trips back to the server for linked resources as needed.

Edit

The purpose of MIME types for IIS is twofold:

  1. Restrict access to the server's resources. If the client request a Web.config file, then of course IIS should block that request, because the file is likely to contain sensitive information like passwords.
  2. Keep track of how to process each file type. For example, HTML files should generally just be sent, but ASPX files need to be processed first by ASP.Net, and then sent.
like image 176
McGarnagle Avatar answered Sep 22 '22 06:09

McGarnagle