⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch2.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>

<HEAD>
   <TITLE>Chapter 2 -- The CGI Specification</TITLE>
   <META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 2</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>The CGI Specification</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="#CGIOverview" >CGI Overview</A>
<LI><A HREF="#Languages" >Languages</A>
<LI><A HREF="#CGIMethods" >CGI Methods</A>
<UL>
<LI><A HREF="#GETMethod" >GET Method</A>
<LI><A HREF="#POSTMethod" >POST Method</A>
<LI><A HREF="#HEADMethod" >HEAD Method</A>
</UL>
<LI><A HREF="#InterfaceSpecification" >Interface Specification</A>
<UL>
<LI><A HREF="#EnvironmentVariables" >Environment Variables</A>
<LI><A HREF="#CommandLine" >Command Line</A>
<LI><A HREF="#StandardInput" >Standard Input</A>
<LI><A HREF="#StandardOutput" >Standard Output</A>
</UL>
<LI><A HREF="#Examples" >Examples</A>
<LI><A HREF="#MoreInformation" >More Information</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
The <I>Common Gateway Interface (CGI)</I> is an accepted standard
for interfacing Web servers and external applications. Web servers
were originally designed to serve static HTML documents along
with other associated static files. A Web browser that communicates
with a Web server that limits its functionality to serving static
pages displays only documents whose contents will not change between
requests or during page visualization.
<P>
A Web server is generally installed on a powerful computer, and
it would be very frustrating not to be able to offer many more
interesting and dynamic things to remote users, using the computer
power available. The CGI specifications were created to answer
this problem. CGI establishes a standard way of information exchange
between Web servers and browsers (also called clients). It allows
the passing of information between a browser or server to an external
program that performs some actions and then outputs its results
back to the user's browser. The external program is generally
know as the <I>CGI program</I>, <I>CGI script</I>, <I>CGI application,</I>
or simply <I>gateway, </I>because it makes use of the CGI specification
and is specially designed for functioning on a Web platform. It
is executed in real-time, by initiative of the user (even if sometimes
nothing is noticed), and it can output dynamic on-the-fly information.
<H2><A NAME="CGIOverview"><FONT SIZE=5 COLOR=#FF0000>CGI Overview</FONT></A>
</H2>
<P>
CGI is an interface specification. It does not define how a Web
server works or how a program is expected to produce results,
but it establishes a set of guidelines that both must follow in
order to interoperate.
<P>
Let's look at an example. Imagine that you have a product database
on your system that you would like users on the Web to use, but
your Web server does not understand the database internals. You
must link both the Web server and the database by using a CGI
program. This special purpose program may be developed by you
or provided by the database vendor and will be responsible for
the database queries on one hand, and the communication with the
Web server on the other hand. This last functionality works only
because the Web server and the program have established rules
for communication between the two. The rules make them able to
interface-they are called the Common Gateway Interface. See Figure
2.1 for a representation of this example.
<P>
<A HREF="f2-1.gif" ><B>Figure 2.1:</B> <I>Accessing a database using a Web browser.</I></A>
<P>
Another example could be the access to an Internet service-e-mail
mailboxes, for example-that is not originally intended to work
over the World Wide Web. One could implement a program that handles
mailboxes and interacts with the Web server (and consequently,
the Web browser) through CGI.
<P>
In fact, a CGI program may be a simple or a complex program and
can perform any task a program is able to. The difference is that
the program communicates with &quot;the real world&quot; by using
the CGI &quot;language.&quot;
<P>
CGI applications are often used to produce HTML pages on-the-fly
(whose contents may change at each request). They are also often
used to process the information introduced in HTML forms.
<P>
The CGI specification is implemented on Web servers, as well as
on programs built for use over the Web. It is not part of the
HyperText Transfer Protocol (HTTP), but most Web servers choose
to implement this useful feature. Therefore, you are able to use
CGI applications in most known Web servers, including ncSA httpd,
CERN httpd, Apache httpd, and many other commercial servers.
<P>
These Web servers are usually distributed with a set of general
purpose CGI programs that reside in a directory called cgi-bin,
within the Web server root directory. This is the directory commonly
used for CGI program storage, but the Webmaster is able to define
other locations (and a security-addict Webmaster will probably
do that). We suggest that you take a look at these examples available
with one public domain Web server.
<H2><A NAME="Languages"><FONT SIZE=5 COLOR=#FF0000>Languages</FONT></A>
</H2>
<P>
CGI applications can be written in any language that can be executed
on a computer-in particular, a Web platform. In fact, you can
choose any of the common languages for your CGI applications.
Your choice depends on what you have to do because different languages
may be specialized for different purposes. Perl, for instance,
is great for string and file manipulation, while C is better for
bigger, more complex programs. Perl and C are probably the most
used languages for CGI programming. Feel free to choose from the
following languages:
<UL>
<LI><FONT COLOR=#000000>C</FONT>
<LI><FONT COLOR=#000000>C++</FONT>
<LI><FONT COLOR=#000000>Perl</FONT>
<LI><FONT COLOR=#000000>Tcl</FONT>
<LI><FONT COLOR=#000000>Python</FONT>
<LI><FONT COLOR=#000000>Shell scripts (UNIX)</FONT>
<LI><FONT COLOR=#000000>Visual Basic</FONT>
<LI><FONT COLOR=#000000>Applescript</FONT>
</UL>
<P>
These languages, as well as many others, provide the programmer
with the means to comply with the CGI specification and use it
to its fullest potential.
<H2><A NAME="CGIMethods"><FONT SIZE=5 COLOR=#FF0000>CGI Methods</FONT></A>
</H2>
<P>
A <I>method</I> is a way of invoking a CGI program. In fact, to
execute the program, you make a request to the server using a
method, which defines how the program receives the data. There
are three main methods, as shown in the following sections.
<H3><A NAME="GETMethod"><TT><FONT SIZE=4 FACE="Courier">GET</FONT></TT><FONT SIZE=4>
Method</FONT></A></H3>
<P>
When you use this method, the CGI program receives the data in
the <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT> environment
variable. The program must parse (process) the string in order
to interpret the data and execute the needed actions. The <TT><FONT FACE="Courier">GET</FONT></TT>
method should be used when you want to obtain data from the server
and you will not change any data on the server. Exceptions may
appear when the data transmitted is very long so that eventual
problems in the size of the variables are prevented. In this case,
the <TT><FONT FACE="Courier">POST</FONT></TT> method is preferred.
<H3><A NAME="POSTMethod"><TT><FONT SIZE=4 FACE="Courier">POST</FONT></TT><FONT SIZE=4>
Method</FONT></A></H3>
<P>
When you use the <TT><FONT FACE="Courier">POST</FONT></TT> method,
the Web server transmits the data to the CGI program through the
<TT><FONT FACE="Courier">stdin</FONT></TT> (standard input). The
server does not mark the end of the data with an EOF character,
so the program must use the <TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
value in order to read the <TT><FONT FACE="Courier">stdin</FONT></TT>
correctly. You should use the <TT><FONT FACE="Courier">POST</FONT></TT>
method when the data you send will alter any data on the Web server
or when you want to send large amounts of data to the CGI program
(usually, more than 1024 bytes, the length limit of a URL).
<H3><A NAME="HEADMethod"><TT><FONT SIZE=4 FACE="Courier">HEAD</FONT></TT><FONT SIZE=4>
Method</FONT></A></H3>
<P>
The <TT><FONT FACE="Courier">HEAD</FONT></TT> method is similar
to the <TT><FONT FACE="Courier">GET</FONT></TT> method, except
that with the <TT><FONT FACE="Courier">HEAD</FONT></TT> method,
only the HTTP headers (and not the data itself) are sent by the
Web server to the browser.
<H2><A NAME="InterfaceSpecification"><FONT SIZE=5 COLOR=#FF0000>Interface
Specification</FONT></A></H2>
<P>
The following sections present the four major methods of communication
between a Web server and a CGI program:
<UL>
<LI><FONT COLOR=#000000>Environment variables</FONT>
<LI><FONT COLOR=#000000>Command line</FONT>
<LI><FONT COLOR=#000000>Standard input</FONT>
<LI><FONT COLOR=#000000>Standard output</FONT>
</UL>
<P>
This presentation is based on the current version (1.1) of the
Common Gateway Interface specification. You can, however, expect
future versions to be backward compatible.
<H3><A NAME="EnvironmentVariables">Environment Variables</A></H3>
<P>
The environment variables are system specific variables set by
the Web server when it executes a CGI application.
<P>
The following sections list the environment variables that are
available. Note, however, that some servers may include some extra
proprietary variables.
<H4><TT><FONT FACE="Courier">AUTH_TYPE</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">AUTH_TYPE</FONT></TT> gives the type
of authentication used if the server supports authentication and
the script is protected.
<H4><TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT> gives the
length, in bytes, of the data sent to the CGI program using the
<TT><FONT FACE="Courier">POST</FONT></TT> method. The <TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
variable is empty if the <TT><FONT FACE="Courier">GET</FONT></TT>
method is used.
<H4><TT><FONT FACE="Courier">CONTENT_TYPE</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">CONTENT_TYPE</FONT></TT> gives the MIME
type of data sent to a CGI program invoked by the <TT><FONT FACE="Courier">POST</FONT></TT>
method. When using the <TT><FONT FACE="Courier">GET</FONT></TT>
method, the <TT><FONT FACE="Courier">CONTENT_TYPE</FONT></TT>
variable is empty. Sample usage: <TT><FONT FACE="Courier">application/x-www-form-urlencoded</FONT></TT>.
<H4><TT><FONT FACE="Courier">GATEWAY_INTERFACE</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">GATEWAY_INTERFACE</FONT></TT> provides
the name and version of the CGI specification being used. Sample
usage: <TT><FONT FACE="Courier">CGI/1.1</FONT></TT>.
<H4><TT><FONT FACE="Courier">PATH_INFO</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">PATH_INFO</FONT></TT> gives the extra
path information that follows the name of the CGI program on a
URL.
<H4><TT><FONT FACE="Courier">PATH_TRANSLATED</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">PATH_TRANSLATED</FONT></TT> is the physical
path of the CGI program, which is usually the Web root directory,
along with the script name and extra path information.
<H4><TT><FONT FACE="Courier">QUERY_STRING</FONT></TT></H4>
<P>
<TT><FONT FACE="Courier">QUERY_STRING</FONT></TT> is the information
that follows the <TT><FONT FACE="Courier">?</FONT></TT> character
in the URL that referenced the CGI program. Using the <TT><FONT FACE="Courier">GET</FONT></TT>
method, <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT> will
contain the input to the CGI program. Using the <TT><FONT FACE="Courier">POST</FONT></TT>
method, <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT> will
be empty, unless something follows the CGI program name and the
attached <TT><FONT FACE="Courier">?</FONT></TT> character on the
URL.
<H4><TT><FONT FACE="Courier">REMOTE_ADDR</FONT></TT></H4>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -