|
|
An Introduction to CGI - The Common Gateway Interface
by Jay Eckles
HTTP Headers
You can send the client any type of output you want, be it an html
page, a graphic, a sound clip, or even a reference to another web
document. However, before you print your output to the standard output
stream, you first need to send an HTTP Header to the client so it knows
what type of information you are sending. This will allow the client's
software to display the information correctly.
The HTTP server will send most of the HTTP headers required to
complete the response to the client, but you should provide the last
header which describes the type of information, whether it is an actual
document or a reference to one. Usually, you will send an HTTP
Content-type header; if you are going to print an html document to
stdout, the content-type header should have a value of text/html. If you
are sending a plain text document, the value should be text/plain.
Here's what the header will look like:
Content-type: text/html\n\n
There are two important things to note here:
- The header is case sensitive.
- The header is followed by two newline characters (
\n\n).
Without these two newline characters, the client's browser can't tell
where your header section stops and your output starts. If you are using
more than one HTTP header in your response to the client, each header is
separated by a newline and the two newline characters go after the last
header. See the HTTP specification for more information.
There are more header types than just "Content-type". For example, if
you want to redirect the client to another URL rather than send output,
you would send a header called a Location header. It would look
something like this:
Location: http://www.JayEckles.Com\n\n
Once again notice the two newlines. Even if you don't send any output,
you must send a header followed by two newline characters; else the user
will simply be staring at an inactive browser for an indefinite amount
of time. For a more detailed explanation of what HTTP headers are
available for you to use, refer to the HTTP specification or an HTTP
quick-reference. However, in the vast majority of cases you will simply
be sending an html document to the user, preceding it with the header
"Content-type: text/html\n\n" or sending the user to
another location using the header "Location:
http://domain.com\n\n". The next most common header is
"Content-type: image/gif\n\n" or "Content-type: image/jpg\n\n", followed by an image being printed to stdout.
After you send your HTTP header(s) back to the client, you should
then print your output to the client. If you want to send back an html
document, either read an existing html document into memory and then
print the buffer it is stored in on stdout or just print html tags
straight to stdout, thus generating your html page dynamically and
on-the-fly. Many people alter the information presented in the html
document they return based on the input received by the program.
[Contents] [Next] [Previous]
If you have any questions or would like to contact me for any reason, please email me at j.eckles@computer.org.
|