HTTP for HTML Authors, Part II - HTML with Style | WebReference

HTTP for HTML Authors, Part II - HTML with Style


HTTP for HTML Authors, Part II

Specifying the content media type

When a Web server responds to an HTTP request with a document, it must specify a few things about this document. The first and most important bit of information is the media type of the document; the user agent has to know what kind of document this is before it can process it. The most common media type transferred over the Web is, of course, HTML.

The media type is specified using the appropriately named Content-Type HTTP header in the response. The value of this header is a MIME media type (MIME stands for Multipurpose Internet Mail Extensions, a standard for transfer of documents through e-mail that has been used far and wide outside the world of e-mail for such purposes). MIME media types (or MIME types for short) consist of a type and a subtype separated by a slash. For instance, the MIME type for HTML is text/html. In order to specify that a document returned in an HTTP response is an HTML document, a server would include the following header in the response:

Content-Type: text/html

The primary type, in this case text is one of eight different primary types. These are used to classify document types, and also to give a hint about a document's nature if the user agent doesn't understand the specific MIME type. For instance, documents that belong to one of the text types are always composed of text that can be displayed as-is, even though most subtypes, like HTML, contain extra semantics. Likewise all subtypes of the image primary type are images.

The list of MIME types is maintained by IANA, the Internet Assigned Numbers Authority. You can find the full list on IANA's Web site, but most of the time you'll be using a few of the most common types:

HTML Document
CSS Style Sheet
GIF image
JPEG image
PNG image

When your Web server is just serving out files from directories, it will usually handle the Content-Type header on its own. Most Web servers are configured to understand the media type of any file on your hard disk and supply the appropriate header to the browser when they serve the file through HTTP. However, you will have to worry about media types if you start writing extensions to your server that serve dynamically generated documents. A common example of this is the despised Web counter, one of those odometer-like things that read “there have been 3 visitors to this site since May 3rd, 1782.” The image used to display the number of visitors is usually generated on-the-fly to correspond to the actual number of visitors recorded by a program that generates the image; this program has to tell the Web server that the data it's producing is, say, of the type image/gif because the Web server can't just look at the image in advance and decide what Content-Type to use.


Next Page...


Produced by Stephanos Piperoglou
Created: January 24, 2001
Revised: February 28, 2001