eekim.com > Publications > CGI Developer's Guide > Chapter 2

Chapter 2: The Basics    (01)

<Next | Table of Contents | Previous>    (02)

Outputting CGI    (03)

You can now take a closer look at how to send information to the Web browser. As you saw in the "Hello, world!" example, Web browsers expect two sets of data (see Figure 2.3): a header that contains information such as the type of information to display (such as the Content-Type: line) and the actual information (what shows up on the Web browser). These two blocks of information are separated by a blank line.    (04)

[Figure 2.3 not available]    (05)

Figure 2.3. Browsers expect a header and the data from CGI programs, separated by a blank line.    (06)

The header is called an HTTP header. It provides important information about the information the browser is about to receive. There are several different types of HTTP headers, and the most common is the one you used previously: the Content-Type: header. You can use different combinations of HTTP headers by separating them with a carriage return and a newline (\r\n). The blank line separating the header from the data also consists of a carriage return and a newline (why you need both is described briefly in the preceding note and in detail in Chapter 4). You learn the other HTTP headers in Chapter 4; for now, you focus on the Content-Type: header.    (07)

The Content-Type: header describes the type of data the CGI is returning. The proper format for this header is    (08)


Content-Type: subtype/type    (09)

where subtype/type is a valid multipurpose Internet mail extensions (MIME) type. The most common MIME type is the HTML type: text/html. Table 2.1 lists a few of the more common MIME types you will see; a more complete list and discussion of MIME types is in Chapter 4.    (010)


NOTE    (011)

MIME was originally invented as a way to describe the content of mail message bodies. It has become a fairly common way of expressing content type information. You can get more information on MIME from RFC1521. Internet RFCs are "Requests for Comments," which are summaries of decisions made by groups on the Internet attempting to set standards. You can see the results of RFC1521 at the following URL:    (012)

http://andrew2.andrew.cmu.edu/rfc/rfc1521.html    (013)


Table 2.1. Some common MIME types.    (014)

MIME type Description
text/html HyperText Markup Language (HTML)
text/plain Plain text files
image/gif GIF graphics files
image/jpeg JPEG compressed graphics files
audio/basic Sun *.au audio files
audio/x-wav Windows *.wav files

Following the header and the blank line, you simply print the data as you want it to appear. If you are sending HTML, then print the HTML tags and data to stdout following the header. You can send graphics, sound, and other binary files as well simply by printing the contents of the file to stdout. There are some examples of this in Chapter 4.    (015)

<Next | Table of Contents | Previous>    (016)

Copyright © 1997 Sams.Net Publishing    (017)