?? ch03_01.htm
字號:
<?label 3. The Common Gateway Interface?><html><head><title>The Common Gateway Interface (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch02_07.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="ch03_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h1 class="chapter">Chapter 3. The Common Gateway Interface</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch03_01.htm">The CGI Environment</a><br><a href="ch03_02.htm">Environment Variables</a><br><a href="ch03_03.htm">CGI Output</a><br><a href="ch03_04.htm">Examples</a><br></p></div><p>Now that we have <a name="INDEX-501" />explored HTTP in general, we canreturn to our discussion of CGI and see how our scripts interact withHTTP servers to produce dynamic content. After you have read thischapter, you'll understand how to write basic CGI scripts andfully understand all of our previous examples. Let's getstarted by looking at a script now.</p><p>This script displays some basic information, including CGI and HTTPrevisions used for this transaction and the name of the serversoftware:</p><blockquote><pre class="code">#!/usr/bin/perl -wTprint <<END_OF_HTML;Content-type: text/html<HTML><HEAD> <TITLE>About this Server</TITLE></HEAD><BODY><H1>About this Server</H1><HR><PRE> Server Name: $ENV{SERVER_NAME} Listening on Port: $ENV{SERVER_PORT} Server Software: $ENV{SERVER_SOFTWARE} Server Protocol: $ENV{SERVER_PROTOCOL} CGI Version: $ENV{GATEWAY_INTERFACE}</PRE><HR></BODY></HTML>END_OF_HTML</pre></blockquote><p>When you request the URL for this CGI script, it produces the outputshown in <a href="ch03_01.htm#ch03-93144">Figure 3-1</a>.</p><a name="ch03-93144" /><div class="figure"><img width="448" src="figs/cgi2.0301.gif" height="207" alt="Figure 3-1" /></div><h4 class="objtitle">Figure 3-1. Output from server_info.cgi</h4><p>This simple example demonstrates the basics about how scripts workwith <a name="INDEX-502" />CGI:</p><ul><li><p>The web server passes information to CGI scripts via<a name="INDEX-503" />environment variables, which the scriptaccesses via the <tt class="literal">%ENV</tt> hash.</p></li><li><p>CGI scripts produce output by printing an HTTP message on STDOUT.</p></li><li><p>CGI scripts do not need to output full HTTP headers. This scriptoutputs only one HTTP header, <em class="emphasis">Content-type</em>.</p></li></ul><p>These details define what we will call the <em class="firstterm">CGIenvironment</em><a name="INDEX-504" /><a name="INDEX-505" />. Let's explore this environment inmore detail.</p><div class="sect1"><a name="ch03-71120" /><h2 class="sect1">3.1. The CGI Environment</h2><p>CGI establishes a particular environment in which CGI scriptsoperate. This environment includes such things as what currentworking directory the script starts in, what variables are preset forit, where the standard file handles are directed, and so on. Inreturn, CGI requires that scripts be responsible for defining thecontent of the HTTP response and at least a minimal set of HTTPheaders.</p><p>When CGI scripts are executed, their current working<a name="INDEX-506" />directory istypically the directory in which they reside on the web server; atleast this is the recommended behavior according to the CGI standard,though it is not supported by all web servers (e.g.,Microsoft's IIS). CGI scripts are generally executed withlimited permissions. On Unix systems, CGI scripts execute with thesame permission as the web server which is generally a special usersuch as <em class="emphasis">nobody</em>, <em class="emphasis">web</em>, or<em class="emphasis">www</em>. On other operating systems, the web serveritself may need to be configured to set the<a name="INDEX-507" />permissions that CGI scripts have. In anyevent, CGI scripts should not be able to read and write to all areasof the file system. You may think this is a problem, but it isactually a good thing as you will learn in our security discussion in<a href="ch08_01.htm">Chapter 8, "Security"</a>.</p><a name="ch03-1-fm2xml" /><div class="sect2"><h3 class="sect2">3.1.1. File Handles</h3><p>Perl scripts generally start with three standard<a name="INDEX-508" /> <a name="INDEX-509" /> <a name="INDEX-510" /> <a name="INDEX-511" /> <a name="INDEX-512" />filehandles predefined: STDIN, STDOUT, and STDERR. CGI Perl scripts areno different. These file handles have particular meaning within a CGIscript, however.</p><a name="ch03-2-fm2xml" /><div class="sect3"><h3 class="sect3">3.1.1.1. STDIN</h3><p>When a web server receives an HTTP request directed to a CGI script,it reads the HTTP headers and passes the content body of the messageto the CGI script on STDIN. Because the headers have already beenremoved, STDIN will be empty for <a name="INDEX-513" /> <a name="INDEX-514" />GET requests that have no body andcontain the encoded form data for POST requests. Note that there isno end-of-file marker, so if you try to read more data than isavailable, your CGI script will hang, waiting for more data on STDINthat will never come (eventually, the web server or browser shouldtime out and kill this CGI script but this wastes system resources).Thus, you should never try to read from STDIN for GET requests. For<a name="INDEX-515" /><a name="INDEX-516" />POST requests, you should alwaysrefer to the value of the <em class="emphasis">Content-Length</em> headerand read only that many bytes. We'll see how to read thisinformation in <a href="ch04_01.htm">Chapter 4, "Forms and CGI"</a> in <a href="ch04_01.htm">Chapter 4, "Forms and CGI"</a>.</p></div><a name="ch03-3-fm2xml" /><div class="sect3"><h3 class="sect3">3.1.1.2. STDOUT</h3><p>Perl CGI scripts return their output to the web server by<a name="INDEX-517" /> <a name="INDEX-518" /> <a name="INDEX-519" /> <a name="INDEX-520" />printing to STDOUT. This may include someHTTP headers as well as the content of the response, if present. Perlgenerally buffers output on STDOUT and sends it to the web server inchunks. The web<a name="INDEX-521" />server itself may wait until theentire output of the script has finished before sending it onto theclient. For example, the <a name="INDEX-522" /><a name="INDEX-523" />iPlanet (formerly Netscape) Enterprise Serverbuffers output, while <a name="INDEX-524" /><a name="INDEX-525" />Apache(1.3 and higher) does not.</p></div><a name="ch03-4-fm2xml" /><div class="sect3"><h3 class="sect3">3.1.1.3. STDERR</h3><p>CGI does not designate how web servers should handle output toSTDERR, and servers implement this in different ways, but they almostalways produces a <em class="emphasis">500 Internal ServerError</em><a name="INDEX-526" /><a name="INDEX-527" /><a name="INDEX-528" /> reply. Some web servers,like Apache, append STDERR output to the web server's errorlog, which includes other errors such as authorization failures andrequests for documents not on the server. This is very helpful for<a name="INDEX-529" /><a name="INDEX-530" />debugging errors in CGIscripts.</p><p>Other servers, such as those by iPlanet, do not distinguish betweenSTDOUT and STDERR; they capture both as output from the script andreturn them to the client. Nevertheless, outputting data to STDERRwill typically produce a server error because<a name="INDEX-531" />Perl does not buffer STDERR, so dataprinted to STDERR often arrives at the web server before data printedto STDOUT. The web server will then report an error because itexpects the output to start with a valid header, not the errormessage. On iPlanet, only the server's error message, and notthe complete contents of STDERR, is then logged.</p><p>We'll discuss strategies for handling STDERR output in ourdiscussion of CGI script debugging <a name="INDEX-532" /> <a name="INDEX-533" />in <a href="ch15_01.htm">Chapter 15, "Debugging CGI Applications"</a>.</p></div></div></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch02_07.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch03_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">2.7. Summary</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">3.2. Environment Variables</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -