📄 ch05.htm
字号:
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>502</TT></TD>
<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><TT>Bad Gateway</TT></TD>
<TD ALIGN="LEFT">The server received an invalid response from the gateway or an upstream server.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>503</TT></TD>
<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><TT>Service Unavailable</TT></TD>
<TD ALIGN="LEFT">The server cannot process the request due to a high load or maintenance of the server.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">504</FONT></TD>
<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">Gateway Timeout</FONT></TD>
<TD ALIGN="LEFT"><FONT COLOR="#000000">The response did not return within a time that the gateway
was prepared to wait. Similar to Internal error 500 but has more diagnostic value.</FONT></TD>
</TR>
</TABLE>
In the next section, we will look at how the <TT>header()</TT> method in CGI.pm can
be used in your script to set the <TT>Status</TT> response header. <B><TT>Content-Type
Response Header</TT></B> The most common response header is <TT>Content-Type</TT>,
which contains the MIME type of the object being returned (usually "text/html"),
and <TT>Content-Length</TT>, which indicates the size of the object. <B><TT>Location
Response Header</TT></B> If you did not want to return a file but instead wanted
your script to send the browser to another URI, the <TT>Location</TT> response header
can be used.</P>
<P>The headers are terminated by an empty line and followed by the "body"
of the message. The "body" refers to the text or HTML or other MIME compliant
message in the object.</P>
<P>So, bringing all this together, here is the syntax of a basic HTTP response header:</P>
<PRE><FONT COLOR="#0066FF">"HTTP/1.0" result-code message CrLf
Header: Value CrLf
Header: Value CrLf
CrLf
BODY
Connection closed by foreign host.
</FONT></PRE>
<P>Here is a real-world example of the complete response generated by a Netscape
Communications Server. This response was generated after a simple <TT>GET</TT> request
was sent for an HTML file. A similar response could be generated by your CGI script
using the <TT>header()</TT> method in CGI.pm.</P>
<PRE><FONT COLOR="#0066FF">HTTP/1.0 200 OK
Server: Netscape-Communications/1.12
Date: Monday, 21-Oct-96 01:42:01 GMT
Last-modified: Thursday, 10-Oct-96 17:17:20 GMT
Content-length: 1048
Content-type: text/html
<HTML>
<HEAD>
<TITLE>Welcome to My Web Page</TITLE>
</HEAD>
<BODY>
Here is my web page. <b>Do you like it?</b>
</BODY>
</HTML>
Connection closed by foreign host.
</FONT></PRE>
<P>Notice that after the response headers are sent, a blank line (CrLf) is sent,
which is followed by the body of the response. The body is the Web page, or the data
your script outputs. After the data has been sent (the body), the server drops the
connection.</P>
<P>When generating your own response headers for NPH scripts (highly recommended),
the contents of these response headers are easily modified using CGI.pm, or the HTTP::Response
class modules. In the next section, I will show you how CGI.pm or the HTTP::Response
class can be used to easily generate response headers.
<CENTER>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Perl5 and the WWW Libraries in
Action</FONT></H3>
</CENTER>
<P>Perl, with the help of CGI.pm or the WWW libraries, allows complete control over
every aspect of the client/server HTTP transaction. Using the CGI::Request WWW library
in your scripts to manage and process variables passed between the client and the
server during a transaction makes your code more secure, easier to maintain, and
easier to write.
<CENTER>
<H4><A NAME="Heading15"></A><FONT COLOR="#000077">CGI.pm versus Individual libwww
Modules</FONT></H4>
</CENTER>
<P>CGI.pm and the modular WWW libraries are very similar in function but very different
in purpose. CGI.pm includes all of the functionality of the individual modules, and
in most cases, more. In fact, Lincoln Stein, the author of CGI.pm and the WWW modules,
developed the CGI.pm module in parallel with the CGI, URL, and HTML modules. The
individual modules are designed to provide a very optimized solution to about nine
different aspects of programming with Perl for the Web. Modules exist for the Web
functions listed in Table 5.4. <TT>Table 5.4. Function of libwww modules. </TT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Module</B></TD>
<TD ALIGN="LEFT"><B>Function</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">CGI::</TD>
<TD ALIGN="LEFT">Allows control over HTTP request and response headers. Mini HTTP server, CGI-based
imagemapping, "smart" forms.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">File::</TD>
<TD ALIGN="LEFT">Parsing of directory listings and a persistent counter.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Font::</TD>
<TD ALIGN="LEFT">Interface to Adobe Font Metrics Files.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">HTML::</TD>
<TD ALIGN="LEFT">Creation and parsing of HTML elements. Format HTML as text or PostScript.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">HTTP::</TD>
<TD ALIGN="LEFT">Generation of HTTP requests, responses, and headers. Content negotiation and status
code processing.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">LWP::</TD>
<TD ALIGN="LEFT">Low-level I/O, socket interface, file type recognition.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">MIME::</TD>
<TD ALIGN="LEFT">Encoding and decoding Base64 and QP.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">URI::</TD>
<TD ALIGN="LEFT">Filtering, processing of URIs, elimination of unsafe characters in strings.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">WWW::</TD>
<TD ALIGN="LEFT">Parse robot.txt files.</TD>
</TR>
</TABLE>
Think of each of the individual modules as a very focused and optimized solution
to a specific task. Most of the time, you will be writing scripts that use features
from more than one of the preceding modules. If you have to load any more than three
modules, it is actually more efficient to just load the entire CGI.pm. Loading each
of the modules separately actually is more resource intensive due to the overhead
associated with loading multiple files. The CGI.pm module also has a more robust
feature set than the individual modules.</P>
<P>Anything your CGI prints to STDOUT gets sent back to the Web server and out to
the browser, unless you are using nph-CGIs, in which case, STDOUT goes directly to
the browser without being parsed by the server. Traditionally, before Perl5 and CGI.pm,
HTML forms were created by CGI scripts by printing the HTML form elements to STDOUT.
Traditionally, the name/value pairs would have to be URI-decoded from either the
URI (<TT>GET</TT> method) or STDIN (<TT>POST</TT> method). Back in the days of Perl
4, include scripts such as cgi-lib.pl were written to deal with these nuisances.
However, there were still problems. Using cgi-lib.pl, the state of a form (the <BR>
contents of the variables) was lost from invocation to invocation because neither
the browser nor the server retained data from a previous transaction, debugging was
difficult, namespace was trampled, and the code was messy and would emit errors before
the HTTP response headers were printed if Perl was run in strict mode.
<DL>
<DT></DT>
</DL>
<CENTER>
<H3><A NAME="Heading16"></A><FONT COLOR="#000077">URL:</FONT></H3>
</CENTER>
<BLOCKQUOTE>
<P>If you still aren't convinced, a complete rundown on all of the benefits of using
CGI.pm and Perl5 over the old methods such as cgi-lib.pl can be found at the following
URL:
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">http://perl.com/perl/info/www/!cgi-lib.html
</FONT></PRE>
<P>CGI.pm was created to address these problems and build a wide and extensible bridge
between your CGI Perl script and the CGI interface. With CGI.pm, HTML forms can be
generated easily by making a few Perl function calls. When you use CGI.pm to generate
HTML forms, the values of the input fields from previous queries (if there were any)
are automatically used to initialize the form, so the state of the form is preserved
from invocation to invocation. URI encoding and <BR>
decoding of special characters is handled transparently by CGI.pm. <TT>GET</TT> and
<TT>POST</TT> methods are handled appropriately. If your script is run from the UNIX
shell, the option of inputting variables from the command line is given. In short,
CGI.pm takes care of almost all of the gory details of CGI and lets you focus on
writing your program.</P>
<P>The easiest way to learn to use CGI.pm or any of the modules is to simply start
using them in your scripts.
<CENTER>
<H3><A NAME="Heading17"></A><FONT COLOR="#000077">Example 1: Creating Forms with
CGI.pm</FONT></H3>
</CENTER>
<P>The example in Listing 5.1 introduces the usage of CGI.pm in a typical script
that generates and displays an HTML form. When run, the script parses the input from
<TT>GET</TT> and <TT>POST</TT> methods, then prints the HTML output for a Web page
with a form on it. If the script finds any data in the input stream (a previously
filled-out form), it will be printed at the bottom of the page. The output is shown
in Figure 5.3.
<DL>
<DT></DT>
</DL>
<CENTER>
<H3>
<HR WIDTH="83%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
</CENTER>
<BLOCKQUOTE>
<P>The line numbers in Listing 5.1 are for reference only and are not to be typed
in.<BR>
<HR>
</BLOCKQUOTE>
<CENTER>
<H3><A NAME="Heading19"></A><FONT COLOR="#000077">Listing 5.1. Using CGI.pm with
forms</FONT></H3>
</CENTER>
<PRE><FONT COLOR="#0066FF">1 #!/usr/local/bin/perl
2 use CGI;
3 $query = new CGI;
4
5 print $query->header;
6
7 print $query->start_html("Using CGI.pm with Forms");
8 print "<H2> Example 1: Using CGI.pm with Forms</H2>
9 <HR>\n";
10
11 &print_prompt($query);
12 &do_work($query);
13 print $query->end_html;
14
15 # Subroutines ###
16
17 sub print_prompt {
18 my($query) = @_;
19 print $query->start_multipart_form;
20
21 print "
22 <table border=0 cellpadding=0 cellspacing=0 width=550>
23 <tr><td>",
24 $query->textfield(-name=>`first_name',
25 -default=>`John',
26 -size=>25),
27 "<BR>First Name</td><td>",
28 $query->textfield(-name=>`mi', -size=>2, -default=>`A'),
29 "<BR>M.I.</td><td>",
30 $query->textfield(-name=>`last_name',
31 -default=>`Doe',
32 -size=>25),
33 "<BR>Last Name</tr></td>
34 <tr><td colspan=3>",
35 $query->textfield(-name=>`address', -size=>55),
36 "<BR>Street Address</td></tr>
37 <tr><td>",
38 $query->textfield(-name=>`city',
39 -size=>25),
40 "<BR>City</td><td>",
41 $query->textfield(-name=>`state', -size=>2),
42 "<BR>State</td><td>",
43 $query->textfield(-name=>`zip',
44 -size=>10),
45 "<BR>Zip Code</tr></td>
46 </table>";
47
48 print "<HR><table border=0 cellspacing=0
49 cellpadding=0 width=550>
50 <tr><td><EM>What Opera
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -