📄 ch05.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Without a title - Title</TITLE>
</HEAD>
<BODY BACKGROUND="r2harch.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/r2harch.gif" TEXT="#000000" BGCOLOR="#FFFFFF">
<P ALIGN="CENTER"><A HREF="ch04.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch04.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42"
HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><A HREF="ch06.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch06.htm"><IMG SRC="blannext.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blannext.gif"
WIDTH="45" HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A>
<H2 ALIGN="CENTER"><BR>
<FONT COLOR="#0000AA">5</FONT><BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">Putting It All Together<BR>
</FONT>
<HR>
</H2>
<UL>
<LI><A HREF="#Heading1">Putting It All Together</A>
<UL>
<LI><A HREF="#Heading2">HTTP Transactions</A>
<UL>
<LI><A HREF="#Heading4">The Connection</A>
<LI><A HREF="#Heading6">The Request</A>
<LI><A HREF="#Heading10">What Happens after the Request</A>
<LI><A HREF="#Heading11">The Response</A>
</UL>
<LI><A HREF="#Heading14">Perl5 and the WWW Libraries in Action</A>
<UL>
<LI><A HREF="#Heading15">CGI.pm versus Individual libwww Modules</A>
</UL>
<LI><A HREF="#Heading16">URL:</A>
<LI><A HREF="#Heading17">Example 1: Creating Forms with CGI.pm</A>
<LI><A HREF="#Heading19">Listing 5.1. Using CGI.pm with forms</A>
<LI><A HREF="#Heading21">Example 2: Open a New Browser Window</A>
<LI><A HREF="#Heading22">Listing 5.2. Open a new browser window</A>
<LI><A HREF="#Heading24">Example 3: Upload a File to the Server</A>
<LI><A HREF="#Heading25">Listing 5.3. Upload a file to the server</A>
<LI><A HREF="#Heading27">Example 4: Using CGI::Carp to Debug Scripts</A>
<LI><A HREF="#Heading28">Listing 5.4. Using CGI::Carp to debug scripts</A>
<LI><A HREF="#Heading30">Using the HTTP::Request Module Directly</A>
<LI><A HREF="#Heading31">Summary</A>
</UL>
</UL>
<P>
<HR>
</P>
<UL>
<LI>HTTP Transactions
<P>
<LI>Perl5 and the WWW Libraries in Action
<P>
<LI>Example 1: Creating Forms with CGI.pm
<P>
<LI>Example 2: Open a New Browser Window
<P>
<LI>Example 3: Upload a File to the Server
<P>
<LI>Example 4: Using CGI::Carp to Debug Scripts
<P>
<LI>Using the HTTP::Request Module Directly
</UL>
<P>The first step to mastering CGI programming and creating useful and dynamic programs
is understanding what goes on when your CGI script is run. The transaction that occurs
between the Web server and the browser before your script is run--and the data that
is transferred back and forth during this transaction--is the only input your script
has. Thus understanding exactly how something occurs and what occurs during this
transaction is crucial.</P>
<P>After looking at all that happens during the transaction, we'll focus on how Perl5
modular libraries such as CGI.pm and the libwww modules can be used to greatly simplify
and enhance <BR>
programming CGI scripts. CGI.pm and the libwww modules provide simple APIs (applications
programming interfaces) which allow you to supply a few simple arguments to a supported
Web-related function. The module then generates appropriate HTML output for you.
Tasks such as creating HTML forms, imagemaps, and headers are just a few of the tasks
enhanced by using these Perl5 modules in your program.
<H3 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">HTTP Transactions</FONT></H3>
<P>HTTP transactions are stateless; that is, neither the browser nor the server store
the "status" or state of the other. During an HTTP transaction, a client,
such as Netscape, establishes a connection to a remote server, then issues a request
in the form of a URI. The remote server then processes the request, returns a response,
and closes the connection.</P>
<P>Figure 5.1 depicts the stages in an HTTP transaction that include a CGI script.
<BR>
<BR>
<A HREF="06wpp01.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/06wpp01.jpg"><TT><B>Figure 5.1.</B></TT></A> HTTP transactions
with CGI scripts.
<DL>
<DT></DT>
</DL>
<H3 ALIGN="CENTER">
<HR WIDTH="84%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
<BLOCKQUOTE>
<P>A URI, or Uniform Resource Identifier, simply refers to the formatted string that
references a specific network resource. URIs have been known by many names:<BR>
<BR>
<FONT COLOR="#000000">WWW Address</FONT></P>
<P><FONT COLOR="#000000">Netsite</FONT></P>
<P><FONT COLOR="#000000">Uniform Resource Locator (URL)</FONT></P>
<P><FONT COLOR="#000000">Uniform Resource Name (URN)<BR>
Universal Document Identifiers</FONT>
</BLOCKQUOTE>
<PRE></PRE>
<BLOCKQUOTE>
<P>Throughout this book, the "URI" notation will be used.<BR>
<HR>
</BLOCKQUOTE>
<P>Certain data and variables are passed between the browser and the server during
each step of an HTTP transaction. A transaction is the process that begins with typing
a URI (<TT>http://www.coolsite.com/index.html</TT>) into a Web browser and ends with
the Web server sending the appropriate response. That response is usually just a
Web page or a GIF image but can be anything from the output of a CGI Perl5 script
to a stream of video. The data and variables, or messages, passed back and forth
between the browser and the server during the transaction are called HTTP headers.
Understanding the details of the HTTP transaction and being aware of the syntax and
content of these headers is critical. The content of these headers is made available
by the Web server in the form of environment variables to your CGI. Using information
obtained from these environment variables, you could, for example, send out different
Web pages based on what kind of Web browser someone is using. Let's now explore the
interaction between the browser, also known as the client, and the Web server during
a typical HTTP transaction.
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">The Connection</FONT></H4>
<P>On the Internet, all communication takes place over a TCP/IP (Transport Control
Protocol/Internet Protocol) connection. TCP/IP is the method or protocol used to
transport data across the Internet. TCP/IP connections are established via a three-way
handshake. The browser, or client, sends a connection request, the server responds,
and then the client acknowledges the response.</P>
<P>The first step in an HTTP transaction begins when a client sends a connection
request to the HTTP port on a server.</P>
<P>Port 80 is usually configured as the HTTP port on most Web servers. The Web server
then responds to the connect request with a connect response. The client acknowledges
the connect response and proceeds to send the first part of the request.
<DL>
<DT></DT>
</DL>
<H3 ALIGN="CENTER">
<HR WIDTH="84%">
<BR>
<FONT COLOR="#000077">TIP:</FONT></H3>
<BLOCKQUOTE>
<P>Though port 80 is typically used for a Web server (also known as an httpd), different
ports can be used. The port number can be configured on a UNIX-based server in the
<BR>
/etc/services file, or through Netscape's configuration server. You can actually
have multiple Web servers, each using different ports, running on the same machine
simultaneously. Browsers will always default to connecting to port 80, however. This
behavior can be changed by adding a <TT>:portnumber</TT> to the end of the URI. For
example:</P>
<PRE><FONT COLOR="#0066FF">http://www.coolsite.com</FONT></PRE>
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>will default to port 80, and thus is identical to</P>
<PRE><FONT COLOR="#0066FF">http://www.coolsite.com:80</FONT></PRE>
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>To connect to a Web server configured to run on port 81, you must explicitly specify
the port number in the URI:</P>
<PRE><FONT COLOR="#0066FF">http://www.coolsite.com:81</FONT></PRE>
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>Sites that use Netscape server software typically are actually running two Web
servers. One on port 80, which serves Web pages, and one on port 81, which serves
as a configuration server. So, to configure a Netscape Web server, you actually connect
to a separate Web server (HTTPD) running on a separate port.<BR>
<HR>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">The Request</FONT></H4>
<P>The server acknowledges the first part of the request. The client then sends the
second part, and without waiting for a response, follows up with the third and final
part of the request.
<H3 ALIGN="CENTER">
<HR WIDTH="83%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
<BLOCKQUOTE>
<P>If you have an external modem, take note of the send and receive lights the next
time you click on a link in your Web browser. TCP/IP handshaking overhead is responsible
for much of the activity.<BR>
<HR>
</BLOCKQUOTE>
<P>At this point, a TCP/IP connection has been established between your browser and
the Web server, and the Web server has received an HTTP request from the browser.
Contrary to what a casual observer sees, this request contains more than just what
is typed in the URI field in your Web browser.</P>
<P>The browser takes the URI that you give it, and from it derives the address of
the server to contact, contacts it, and sends it a request. There are actually two
types of requests a browser can send: the older SimpleRequest and the newer FullRequest.
Both types of requests have the same basic format, but the FullRequest is much more
versatile. The SimpleRequest has the following format:</P>
<PRE><FONT COLOR="#0066FF">GET <URI> CrLf
</FONT></PRE>
<P>where <TT>GET</TT> is a method that basically asks the server to retrieve whatever
data is identified by the <TT><URI></TT> element. It is important to note that
if the <TT><URI></TT> element points to a CGI script or some other data-producing
executable process, the output generated by running the script or the <BR>
process will be returned to the browser, not the contents of the script or the source
code. The <TT>CrLf</TT> is your basic carriage return followed by a linefeed sequence.</P>
<P>The <TT>GET</TT> and <TT>POST</TT> methods are by far the most commonly used methods
and will be discussed in more detail in the "Methods" section. With the
new FullRequest scheme, methods other than <TT>GET</TT> and <TT>POST</TT> are now
possible. Also, additional headers may be appended to the request. The FullRequest
has the following format:</P>
<PRE><FONT COLOR="#0066FF">METHOD <URI> "HTTP/1.1" CrLf
name: value CrLf
name: value CrLf
</FONT></PRE>
<P>where <TT>METHOD</TT> is one of the methods discussed in the "Methods"
section and <TT><URI> </TT>is the specified URI. The <TT>name: value</TT> header
will likely be one of the headers discussed below. <B><TT>Methods</TT></B> The methods
in Table 5.1 are included, or proposed for inclusion (at the time of this writing),
in the current HTTP specification in a FullRequest.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -