📄 ch3.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 3 -- Crash Course in CGI</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 3</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Crash Course in CGI</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#WhyCGIExists" >Why CGI Exists</A>
<LI><A HREF="#WannaHaveaConversation" >Wanna Have a Conversation?</A>
<UL>
<LI><A HREF="#MakingaConnection" >Making a Connection</A>
<LI><A HREF="#GettingDowntoBusiness" >Getting Down to Business</A>
</UL>
<LI><A HREF="#ParlezVousEnvironmentVariables" ><I>Parlez Vous</I> Environment Variables?</A>
<UL>
<LI><A HREF="#ServerSpecificEnvironmentVariables" >Server-Specific Environment Variables</A>
<LI><A HREF="#ClientSpecificEnvironmentVariables" >Client-Specific Environment Variables</A>
<LI><A HREF="#RequestSpecificEnvironmentVariables" >Request-Specific Environment Variables</A>
</UL>
<LI><A HREF="#TakingItAllIn" >Taking It All In</A>
<UL>
<LI><A HREF="#REQUEST_METHOD" >REQUEST_METHOD</A>
<LI><A HREF="#StrangeLookingData" >Strange Looking Data</A>
</UL>
<LI><A HREF="#RSVP" >RSVP</A>
<UL>
<LI><A HREF="#TypesofResponses" >Types of Responses</A>
<LI><A HREF="#Headers" >Headers</A>
</UL>
<LI><A HREF="#SomeThingstoConsider" >Some Things to Consider</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
Divide and conquer. If something is made up of a group of things,
go after the parts of it you're comfortable tackling one by one,
and before you know it, you'll have gotten through it all. It
doesn't matter if it's a kid trying to finish all the vegetables
he or she doesn't like or an adult trying to file a tax return;
lots of things are just easier when you take them step by step.
<P>
With any new thing, like CGI, you can get overwhelmed trying to
take everything in at once. There are new concepts and things
that sometimes seem like background information you'll never use
unless you plan on winning a trivia game. But it's hard to know
what's important until you see how it all works in real-world
terms, so that you can picture how "mysterious CGI processes"
really just do some straightforward things for some very good
reasons-and in a specific order.
<P>
We're going to demystify those reasons and that order for you
by showing you how you and CGI programs can communicate with one
another. Starting at the very beginning, you're going to see things
like the following:
<UL>
<LI><FONT COLOR=#000000>Why CGI exists</FONT>
<LI><FONT COLOR=#000000>What CGI really is</FONT>
<LI><FONT COLOR=#000000>Starting a CGI conversation</FONT>
<LI><FONT COLOR=#000000>How CGI gets information</FONT>
<LI><FONT COLOR=#000000>What you can do with that information</FONT>
<LI><FONT COLOR=#000000>How you can respond</FONT>
</UL>
<P>
As you'll see, there's nothing mysterious about the way CGI works.
Unusual at first, perhaps, but nothing that can't be easily examined
and put to work for you when you're ready to give it a try-and
you'll be ready soon enough.
<H2><A NAME="WhyCGIExists"><FONT SIZE=5 COLOR=#FF0000>Why CGI
Exists</FONT></A></H2>
<P>
When kids are really little, they can't do everything by themselves.
They try, and they give it their best effort, but when it comes
to things that are too high up for them to reach or too dangerous
for them to try, you step in to help them out to make sure things
get done without any smashed fingers, broken toes, or other mishaps.
<P>
Web servers are like little kids. Heck, the World Wide Web has
been around only a few years, and while these little tykes are
growing fast, they still haven't learned to do everything they
want to do. Each kid on the block is also very different, with
his or her own set of values and his or her own personality. As
they grow up, each of them will have their own likes and dislikes,
as well as their own set of skills. Besides, while they're young,
you can't expect them to do everything that you normally do in
a day, can you?
<P>
CGI programs are the helpful older siblings of the Web server,
the ones willing to do things that the servers haven't learned
how to do, or just refuse to do. They'll help with math and get
things out of storage for the servers to play with. Yes, there
will occasionally be squabbles and disagreements, but if you convince
the CGI program to bear with the server, and treat it nicely,
a good deal of cooperation can take place.
<P>
This cooperation between Web servers and their CGI siblings allows
things to get done that normally the Web server would have to
turn away. It only knows how to do certain tasks, like answer
when it's called and hand over a file that it has available. When
it gets a request that it can't handle, it needs help.
<H2><A NAME="WannaHaveaConversation"><FONT SIZE=5 COLOR=#FF0000>Wanna
Have a Conversation?</FONT></A></H2>
<P>
When a Web server asks for help from a CGI program, it's basically
striking up a conversation. Like any conversation, there are several
parts. First, the server tells the CGI program that it wants to
talk. Normally what causes this is that someone has sent a message
to the server indicating that they want something special done,
and that there's a specific file (in this case a CGI program)
that can perform that special function. The user who started the
request normally has no idea what CGI program they're calling-to
them it might just have been a button on a page that says "Click
Me," or a link, or something the server has been trained
automatically to call on the CGI program for without checking
with the user. An example would be any of the popular Web Search
sites where you type data into a box and press the Search button.
You don't necessarily keep track of what's being done, but as
long as you get back the information you were looking for, it
doesn't matter too much. No matter how the request came in, or
what it's supposed to do, it's there, and the server needs to
deal with it.
<H3><A NAME="MakingaConnection">Making a Connection</A></H3>
<P>
To start a CGI program, the server needs to find it first. Because
it's been brought up right, the server doesn't go screaming around,
looking everywhere for the CGI program, and disrupting everything
else that's going on. Instead, the server knows where it can normally
find the CGI program when it's available to help, and that's where
it looks. For most servers, the place where the CGI programs are
found is the <TT><FONT FACE="Courier">cgi-bin</FONT></TT> directory.
<P>
After it finds the CGI program, if it's available, the server
begins a conversation with it. This is the <I>initialization</I>
phase, where initial contact starts up and things like "Hi,
how are you?" greetings are exchanged before they get down
to the real reason the CGI script is being called.
<P>
During initialization, the machine that the server software is
on has to make additional space for the CGI program to execute,
and it has to start a copy of the program. CGI programs have a
one-track mind: Once they've been started, they carry on one conversation
until they're done. If you want to talk to them about something
else, you have to start up another copy of the CGI program so
that the new conversation can take place. Every instance of the
CGI program having a conversation with the server software is
a <I>process</I>, and each process takes up a certain amount of
space. Unless the conversations go on for a long time or get off
track, most conversations are short enough that you can have large
numbers of them going on at once, even though each is off on its
own topic.
<H3><A NAME="GettingDowntoBusiness">Getting Down to Business</A>
</H3>
<P>
Once the conversation between the server and the CGI program has
begun, the server needs to tell the CGI program what it wants.
Because each CGI program normally has a special set of things
it does, there's already some indication of what the server wants
by which CGI program is being called. Like siblings, you might
find one who's better at math and would be called to help with
homework, while the other is better at drawing and would be called
to help make a picture of a house.
<P>
To let the CGI program do its work, the server needs to give it
all the information that's available about this particular request
and let the server decide how to handle it. It also needs to present
the information in a coherent manner so that the CGI program doesn't
get first names mixed up with last names, look in the wrong place
for other information, or just give up in frustration because
nothing makes any sense. Fortunately, the server and the CGI programs
have an agreed-upon way of sharing information so that nothing
ever gets left out unless there's a real problem.
<H2><A NAME="ParlezVousEnvironmentVariables"><TT><I><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Parlez
Vous</FONT></I></TT><FONT SIZE=5 COLOR=#FF0000> Environment Variables?</FONT></A>
</H2>
<P>
The agreed-upon method that servers and CGI programs use to exchange
information is the use of <I>environment variables</I>. No matter
what the request, the CGI program always knows it can expect certain
pieces of information to be in a specific location and no matter
what they look like, it will be certain of what the information
is supposed to be used for.
<P>
Environment variables are nothing more than storage blocks that
hold onto bits of information about the user. For instance, most
computers have a <TT><FONT FACE="Courier">PATH</FONT></TT> environment
variable, which tells them locations where they can look for files
if they don't find them in the current directory. When the server
gets a request to do something, its first step is to gather all
the relevant information it can think of and place it into storage.
What kind of information does it gather?
<UL>
<LI><FONT COLOR=#000000>Details about itself</FONT>
<LI><FONT COLOR=#000000>Details about the user</FONT>
<LI><FONT COLOR=#000000>Details about the user's request</FONT>
</UL>
<P>
You see, it doesn't know what the CGI program's going to need
to get the job done, and if it just collects all this information
every time, it'll get in the habit and never accidentally forget
to find out something important.
<P>
To show you what kind of information the server stores, we're
going to look at each of the environment variables. Don't worry
too much about memorizing them, or even completely understanding
why in the world the server would store that kind of information.
We'll come back to the important ones when we move into how the
server gets hold of the data in the section "Taking It All
In." Right now, you should just get a feel for how much is
actually stored.
<H3><A NAME="ServerSpecificEnvironmentVariables">Server-Specific
Environment Variables</A></H3>
<P>
Servers like you to know who they are, so they tell you about
themselves. Normally, you already know this, so it's not of too
much use. If you've got a bunch of servers all calling the same
CGI scripts, that might be a different situation; but for the
most part, you can just look at these as a way of giving the server
its due. Table 3.1 shows the environment variables your server
uses to identify itself and the workings around it.<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -