Standard Output

Standard output is the place for data which is given to the web server to be sent back to the user's web browser. The data sent to standard output by the CGI program must be in a certain rough format, which is very straightforward as well. The output data comes in two parts:

[optional] HTTP headers
Content-Type: <MIME type>
   <<blank line>>
<output content>

HTTP headers can be any standard HTTP header information. HTTP headers allow the program [and the web server] to send control and other information to the web browser which isn't displayed, but used to provide meta-information about the content which is about to be sent. Allowing HTTP headers to be output by the CGI program allows for arbritary extension of the web server, allowing for an old server to output newer HTTP headers [which the web server doesn't know about and thus cannot output on it's own] to the web browser [which may be current enough know how to interpret them]. However, outputting any HTTP headers [other than "Content-Type"] is optional and normally is not useful.

The "Content-Type" header denotes what type of data is contained in the content as a MIME type. For instance, when outputting HTML from the CGI program, the program would output the line:

Content-Type: text/html
which denotes that the content below the blank line is HTML text, and should be interpreted as such. If the CGI program was outputting the binary data contained in a GIF image, the Content-Type header would be:
Content-Type: image/gif

A blank line must be output after the last HTTP header to signify that the headers have stopped and the content is about to begin.

The content is then output, and is passed as output to the web browser. If a Content-Type of text/html is used, then HTML must be output in this section. If a Content-Type of image/gif is used, then binary GIF data must be output. In general, the Content-Type must match the data being sent, or the web browser will not be able to decode it.

An example of the output of a simple CGI program generating a short HTML file is shown in Example 2-1.

Example 2-1. Example CGI program output

Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Foo bar</TITLE>
</HEAD>
<BODY>
<IMG SRC="filename.gif" ALT="[A GIF image]" WIDTH=200 HEIGHT=200 ALIGN=right>
This is the output of a CGI Program.
<HR>
<BR CLEAR=all>
Isn't it pretty?
</BODY>
</HTML>

Notice that the correct header [Content-Type: text/html] is the first line of the output, followed by a blank line, followed by the HTML content as described by the header. Discussion of other HTTP headers aside, this is the general form of output for nearly all CGI programs.

Useful HTTP headers which a CGI script may like to output are [but are not limited to — check the HTTP 1.1 documentation for more info]:

Expires

>Tells how long the document is valid for [for caching purposes].

Last-Modified

Tells when the document was last modified [for caching purposes].

Location

For redirecting the web browser to look for the necessary data at a different URL.