?? ch01_02.htm
字號:
<?label 1.2. Introduction to CGI?><html><head><title>Introduction to CGI (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="ch01_01.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="ch01_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">1.2. Introduction to CGI</h2><p>CGI can do so much because it is so simple. CGI is a very lightweightinterface; it is essentially the minimum that the web server needs toprovide in order to allow external processes to create web pages.Typically, when a web server gets a request for a static web page,the web server finds the corresponding HTML file on its filesystem.When a web server gets a request for a CGI script, the web serverexecutes the CGI script as another process (i.e., a separateapplication); the server passes this process some parameters andcollects its output, which it then returns to the client just as ifhad been fetched from a static file (see <a href="ch01_02.htm#ch01-81668">Figure 1-1</a>).</p><a name="ch01-81668" /><div class="figure"><img width="456" src="figs/cgi2.0101.gif" height="159" alt="Figure 1-1" /></div><h4 class="objtitle">Figure 1-1. How a CGI application is executed</h4><p><a name="INDEX-11" /><a name="INDEX-12" />So how does the whole interface work?We'll spend the remainder of the book answering this questionin more detail, but let's take a basic look now.</p><p>Web browsers request dynamic resources such as CGI scripts the sameway they request any other resource on the Web: they send a messageformatted according to the <em class="firstterm">Hypertext TransportProtocol</em>, or <em class="emphasis">HTTP</em>. We'lldiscuss HTTP in <a href="ch02_01.htm">Chapter 2, "The Hypertext Transport Protocol "</a>. An HTTPrequest includes a <em class="firstterm">Universal Resource Locator</em>,or <em class="firstterm">URL</em>, and by looking at the URL, the webserver determines which resource to return. Typically, CGI scriptsshare a common directory, like <em class="filename">/cgi</em>, or afilename extension, like <em class="filename">.cgi</em>. If the web serverrecognizes that the request is for a CGI script, it executes thescript.</p><p>Say you wanted to visit the URL,<em class="emphasis">http://www.mikesmechanics.com/cgi/welcome.cgi</em>. Atits most basic, <a href="ch01_02.htm#ch01-80955">Example 1-1</a> shows a sample HTTPrequest your web browser might send.</p><a name="ch01-80955" /><div class="example"><h4 class="objtitle">Example 1-1. Sample HTTP Request </h4><blockquote><pre class="code">GET /cgi/welcome.cgi HTTP/1.1Host: www.mikesmechanics.com</pre></blockquote></div><p>This GET request identifies the resource to retrieve as <em class="emphasis">/cgi/welcome.cgi</em>. Assuming our server recognizes all files in the<em class="emphasis">/cgi</em> directory tree as CGI scripts, it understands that it shouldexecute the <em class="filename">welcome.cgi</em> script instead ofreturning its contents directly to the browser.</p><p>CGI programs get their input from <a name="INDEX-13" />standard input (<tt class="literal">STDIN</tt>)and <a name="INDEX-14" /><a name="INDEX-15" />environment variables. These<a name="INDEX-16" />variables containinformation such as the identity of the remote host and user, thevalue of form elements submitted (if any), etc. They also store theserver name, the communication protocol, and the name of the softwarerunning the server. We'll look at each one of these in moredetail in <a href="ch03_01.htm">Chapter 3, "The Common Gateway Interface"</a>.</p><p>Once the CGI program starts running, it sends its output back to theweb server via <a name="INDEX-17" /> <a name="INDEX-18" /><a name="INDEX-19" />standard output (<tt class="literal">STDOUT</tt>).In <a name="INDEX-20" /><a name="INDEX-21" />Perl, this is easy to do because bydefault, anything you <tt class="function">print</tt> goes to<tt class="literal">STDOUT</tt>. <a name="INDEX-22" />CGI scripts can either return theirown output as a new document or provide a new URL to forward therequest elsewhere. CGI scripts print a special line formattedaccording to<a name="INDEX-23" />HTTP headers to indicate this tothe web server. We'll look at these headers in the nextchapter, but here is a sample of what a CGI script returning HTMLwould output:</p><blockquote><pre class="code">Content-type: text/html</pre></blockquote><p>CGI scripts actually can return extra header lines if they choose, soto indicate that it has finished sending headers, a CGI script printsa blank line. Finally, if it is outputting a document, it prints thecontents of that document, too.</p><p>The web server takes the output of the CGI script and adds its ownHTTP headers before sending it back to the browser of the user whorequested it. <a href="ch01_02.htm#ch01-94693">Example 1-2</a> shows a sample<a name="INDEX-24" /><a name="INDEX-25" /> <a name="INDEX-26" />response that a web browser wouldreceive from the web server.</p><a name="ch01-94693" /><div class="example"><h4 class="objtitle">Example 1-2. Sample HTTP Response </h4><blockquote><pre class="code">HTTP/1.1 200 OKDate: Sat, 18 Mar 2000 20:35:35 GMTServer: Apache/1.3.9 (Unix)Last-Modified: Wed, 20 May 1998 14:59:42 GMTETag: "74916-656-3562efde"Content-Length: 2000Content-Type: text/html<HTML><HEAD> <TITLE>Welcome to Mike's Mechanics Database</TITLE></HEAD><BODY BGCOLOR="#ffffff"> <IMG SRC="/images/mike.jpg" ALT="Mike's Mechanics"> <P>Welcome from dyn34.my-isp.net! What will you find here? You'll find a list of mechanics from around the country and the type of service to expect -- based on user input and suggestions.</P> <P>What are you waiting for? Click <A HREF="/cgi/list.cgi">here</A> to continue.</P> <HR> <P>The current time on this server is: Sat Mar 18 10:28:00 2000.</P> <P>If you find any problems with this site or have any suggestions, please email <A HREF="mailto:webmaster@mikesmechanics.com"> webmaster@mikesmechanics.com</A>.</P></BODY></HTML></pre></blockquote></div><p>The <a name="INDEX-27" /><a name="INDEX-28" />header contains thecommunication protocol, the date and time of the response, the servername and version, the last time the document was modified, an entitytag used for caching, the length of the response, and the<a name="INDEX-29" /><a name="INDEX-30" />media type of the document -- in thiscase, a text document formatted with HTML. Headers like these arereturned with all responses from web servers, and we'll look atHTTP headers in more detail in the next chapter. However, note thatnothing here indicates to the browser whether this response came fromthe contents of a <a name="INDEX-31" /> <a name="INDEX-32" /> <a name="INDEX-33" />static HTML file or whether it was generateddynamically by a CGI script. This is as it should be; the browserasked the web server for a resource, and it received a resource. Itdoesn't care where the document came from or how the web servergenerated it.</p><p>CGI allows you to generate output that doesn't look anydifferent to the end user than other responses on the Web. Thisflexibility allows you to generate anything with a CGI script thatthe web server could get from a file, including HTML documents, plaintext documents, PDF files, or even images like PNGs or GIFs.We'll look at how to create dynamic images in <a href="ch13_01.htm">Chapter 13, "Creating Graphics on the Fly"</a>.</p><a name="ch01-2-fm2xml" /><div class="sect2"><h3 class="sect2">1.2.1. Sample CGI</h3><p>Let's look at a sample <a name="INDEX-34" />CGI application, written in Perl,that creates the dynamic output we just saw in <a href="ch01_02.htm#ch01-94693">Example 1-2</a>. This program, shown in <a href="ch01_02.htm#ch01-77893">Example 1-3</a>, determines where the user is connecting fromand then creates a simple <a name="INDEX-35" />HTML documentcontaining this information, along with the current time. In the nextseveral chapters, we'll see how to use various CGI modules tomake creating such an application even easier; for now, however, wewill keep it straightforward.</p><a name="ch01-77893" /><div class="example"><h4 class="objtitle">Example 1-3. welcome.cgi </h4><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -