The Internet
The Internet

Reputation: 8103

How to determine the Content-Type in an HTTP Response

I'm building a web server as an exercise. When I receive a raw request, it gets parsed into an simple syntax tree, and a response is built by evaluating this tree. My question this: When sending an HTTP Response, does the Content-Type field get set by taking the file extension of the requested resource and looking it up in a dictionary of MIME-types? A good example would be the anatomy of how the response for a favicon.ico is built. Any insight into this would be most helpful. Thanks.

Upvotes: 2

Views: 4423

Answers (1)

Raptor
Raptor

Reputation: 54212

By default, web server looks into file extension and select what kind of Content Type it should interpret the file as. However, server-side scripting can send custom header ( e.g. header() function of PHP ) to override the settings . For example, a JPEG can be interpreted as PNG if you send Content Type as image/png to web server with the following code:

header('Content-Type: image/png');

For non-file requests, the web server looks into custom header directly.

Web server maps extension with MIME type. As you tag , Apache uses AddType directive to identify file's MIME type, while IIS and other web servers have similar settings .

Upvotes: 3

Related Questions