📄 ch10.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 10 -- The Common Gateway Interface</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 10</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>The Common Gateway Interface</FONT>
</H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER>
<UL>
<LI><A HREF="#TheCGIExplored">
The CGI Explored</A>
<LI><A HREF="#CGIandYourServer">
CGI and Your Server</A>
<UL>
<LI><A HREF="#TheDirectories">
The Directories</A>
</UL>
<LI><A HREF="#PrinciplesofCGIProgramming">
Principles of CGI Programming</A>
<LI><A HREF="#HTTPHeaders">
HTTP Headers</A>
<LI><A HREF="#HTMLForms">
HTML Forms</A>
<LI><A HREF="#EnvironmentalVariables">
Environmental Variables</A>
<UL>
<LI><A HREF="#StandardInputltSTDINgt">
Standard Input <STDIN></A>
<LI><A HREF="#ClientSpecificEnvironmentalVariables">
Client-Specific Environmental Variables</A>
</UL>
<LI><A HREF="#Conclusion">
Conclusion</A>
</UL>
</UL>
<HR>
<P>
With the HTTP protocol, web browsers have access to several Internet
services, but not to all of them. On its own the browser is also
limited in its ability to deal with anything more than static
HTML files. One of the ways to bypass this limitation is to use
a gateway. A <I>gateway </I>provides a client with an interface
that makes files and extension services appear as readable HTML
documents. This gives the user the ability both to access other
services on your Web server, and to input data to the Web server
through HTTP.<BR>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
<BLOCKQUOTE>
<I>The only kind of relationship that the CGI is interested in when dealing with Web sites and related structures is the client/server model of computer communications. The computer that makes requests is the client, and the computer that answers these
requests is the server. Web sites are stored on server computers, or machines</I>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Gateways sit on the server where they take a client's user input
and then output data to the client in a usable format, like an
HTML document or URL. The gateway itself does not deal with satisfying
the client's request, but finds the files, programs, or scripts
on the server that can.
<P>
One of the ways to get more dynamic pages is to use Server Side
Includes (SSIs) in your Web sites. These are different from the
CGI, and will be explained in the next chapter. SSIs can work
with-or without-a gateway.
<H2><A NAME="TheCGIExplored"><FONT SIZE=5 COLOR=#FF0000>
The CGI Explored</FONT></A></H2>
<P>
The CGI is the specification for the way in which a server's gateway
communicates with the Web server. When data comes in from a client's
Web browser (discussed later in this chapter under HTTP Headers)
that contains a query or an HTML form using the GET or POST request
methods, then the Internet service, or "inets," starts
up the http service, or https, to deal with the HTTP data that
is arriving. The https then sends a message using the CGI specifications
to the server's gateway program. The gateway receives this data
from the browser either as standard input, or as environmental
variables.
<P>
Using this data, the gateway initiates whichever response is necessary
by parsing and processing the client data. <I>Parsing </I>is the
procedure the computer puts the data through, figuring out all
its syntax and storing the variables, if necessary, so it is ready
to run.
<P>
This output goes back to the https as HTML, or some other data
format that HTTP can handle; then the https sends this on to the
client's Web browser. There may be no response from the gateway
if the data it has received is only for storage or input to a
database or file folder on the server.
<H2><A NAME="CGIandYourServer"><FONT SIZE=5 COLOR=#FF0000>
CGI and Your Server</FONT></A></H2>
<P>
Before going into the interior of CGI (or, at least, the epidermis),
a quick look at some of the ways your server organizes its information
will shed some light on how CGI operates with your server. To
work effectively, CGI has to know where things are. And to enable
various functions, keep track of user access, and debug your CGI
scripts, you have to know where things are. These are all organized
in various common directories on your server. You are never restricted
to the directory names used here, but these are the commonly used
file names for directories performing the purpose outlined. <BR>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
<BLOCKQUOTE>
<I>One common confusion when dealing with a Windows NT-based server occurs because most resources dealing with CGI and other networking concerns are written from a generic UNIX background and use generic UNIX terms and concepts that do not translate easily
(or sometimes at all) to a Windows NT format</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<I>To make using your system easier, this book uses the proper Windows NT terminology where possible, as well as translating UNIX terms you will encounter in other networking books. For example, consider the term daemon, which is commonly used in most
networking texts. Daemons are programs that deal with providing network services to clients. With Windows NT, daemons are simply called services, or sometimes, network services, and sometimes just servers. This should not be confused with the computer that
hosts these services, also commonly called a server. Little details like these can cause you hours of grief when you are dealing with the Internet, so be sure to keep this terminology issue in mind.</I>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="TheDirectories">
The Directories</A></H3>
<P>
Your NT server works by relying on several things, one of which
is a well-organized directory system. Proper data management is
crucial to the swift operation of a server. If your services cannot
find the data requested of them, they cannot work properly. A
badly organized server that forces the various services to go
through a series of directories and subdirectories before they
can find what they are looking for is just as troublesome. The
following directories are used by the server to operate the CGI
programs. These are all directories to which you must have access
for successful gateway programming. If you are not also a system
administrator, you will need to discuss these issues with the
person who is.
<H4>The Server Root</H4>
<P>
Many files that determine where CGI programs can operate and what
they are allowed to do are stored in the server's root directory.
That directory is usually found on the C: drive on the server's
computer.
<P>
Inside the server's root there are two important areas: the Log
directory and the Registry directory. If you don't have access
to these directories you can still use Perl scripts, but you will
have to consult the system administrator for any additional information,
or special access, that you may need.
<P>
The Log directory is where all the log files are stored, including
logs on errors, security, system, and applications. The most important
of these files are the error logs, where all the errors involving
your HTML documents, CGI programs, and SSIs (Server Side Includes)
are noted. The error logs are necessary for debugging your system.
You can view these logs in Windows NT using Event Viewer.
<P>
On the Windows NT server, the security log is the log that keeps
track of who is using (and has used) what on your server. This
log is very useful when tracking the hits your various Web pages
receive. The security log employs two icons when used by Event
Viewer to speed up your search. A key is used to symbolize a successful
action, and a lock signifies an unsuccessful one.
<P>
There are two other logs that Windows NT runs as part of its server
setup. These are the system log, which records all events that
occur in the Windows NT system, and the application log, which
records events that occur during application runs on Windows NT.
Error calls affecting applications can be found in this log.
<P>
You can have logs that keep track of any kind of information.
If you have a complicated site that makes use of a lot of the
newer HTML tags created by Netscape (like <TABLE>), or you
are using Java, you may want to have alternate HTML documents
for browsers, such as Mosaic, that do not support the tags that
Netscape does.
<P>
You can use a log, in conjunction with a Perl script, that counts
the different kinds of browsers accessing your server. This data
can be used to reorganize your site so that you can direct each
different browser to the Web pages that were especially built
for that browser.
<P>
The Registry directory houses configuration and initialization
data from the Registry that is controlled by using the applications
Registry Editor, Control Panel, User Manager, or File Manager.
The data in the Registry sets up what can happen, like permissions,
and how it happens, like using environmental variables, on your
server. Permissions are controlled by using the "Permissions"
options under the Security menu in File Manager. You can turn
on SSIs, so that your Web pages can take advantage of their functions,
and also inform your server about new file extensions not covered
by MIME specifications, such as the x-parsed-html-type, by using
the Associate option under the File menu in File Manager. It is
important to keep your server up-to-date with new MIME specifications
so that these different file types will be handled properly on
your server.
<P>
Some of these new file extensions are used to determine which
files your server will search for SSIs. Files with the extension
.shtml will be parsed by the server as SSIs. Also, you must check
what file types your server allows by examining the file name
extensions recognized by your system. You can do this by using
Registry Editor and looking in the HKEY_LOCAL_MACHINE / SOFTWARE
subtree in the Classes directory folder. A full listing of the
file names recognized by your system is listed by its name, that
is, the text file name information is stored in a folder titled
.txt.
<P>
Microsoft recommends that you use read-only when using Registry
Editor, and then make any changes through the appropriate Control
Panel application or through File Manager, each of which has the
proper procedure to make changes to the Registry already built
in. To add the Perl file name extension, .pl, you can use the
Associate feature in File Manager, which is found under the File
menu.
<P>
Conversely, you could have a very well laid out plan for your
memory management that restricts the parsing of certain types,
but your server might be set up to parse all documents-thus hindering
all of your hard work. Information about the commands that affect
each directory tree is found in the HTML form tag here:
<BLOCKQUOTE>
<PRE>
<DIRECTORY directory_path>
</PRE>
</BLOCKQUOTE>
<P>
where the directory_path is all the files and directories included
under that directory tree. The command set shown above is
ended with the </DIRECTORY> tag.
<P>
To control the data in your log, use the tools that Event Viewer
contains, like the sorting or filtering options. You can sort
to list the order of the entries, or events, from oldest to newest
or newest to oldest, by choosing one of the options under the
View menu in Event Viewer. Oldest to newest is the recommended
setting.
<P>
The size of event logs can become a problem very quickly on a
busy server. Filtering helps to reduce this problem. When events
are filtered, you determine the start and end of when events are
listed and the types of events logged. The log can start at the
first entry, or be assigned a specific date. The log can be listed
until the last event, or have a specific end date. It is recommended
that you choose a specific period of events to view because the
entire list can become very long, very quickly.
<P>
The different types of events include:
<UL>
<LI>Information-for descriptions of successful infrequent significant
events involving NT server services.
<LI>Warning-for noncritical errors. These are important to note
future problems.
<LI>Error-for critical errors that cause data loss or major functions
to fail.
<LI>Success Audit-for the audit events of successful action executions.
<LI>Failure Audit-for the audit events of failed action executions.
</UL>
<P>
To look for specific events, Event Viewer uses the Find function
under the View menu. The parameters here are very helpful for
locating a specific type of log event, whether it be an error
for which you are looking, or to determine whether an application
launched successfully.
<P>
You can save, or archive, any of these event logs in Event Viewer,
where they will be stored using the .evt file extension. Logs
are stored with the following fields of data: Date, Time, Source,
Type, Category, Event, User, Computer, and Description. These
fields are stored as comma-delimited files, which means that you
can import this data into most spreadsheet and database programs.
<P>
Use the Registry Editor to control your configuration files. This
is the NT application that edits the Registry, which is where
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -