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

📄 ch11_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>mod_perl Handlers (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch11_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">11.3. mod_perl Handlers</h2><p>To understand <em class="emphasis">mod_perl</em>, you should understandhow the Apache server works. When Apache receives a request, itprocesses it in several stages. First, it translates the URL to theassociated resource (i.e., filename, CGI script, etc.) on the servermachine. Then it checks to see if the user is authorized to accessthat resource, perhaps by requesting and checking an ID and passwordor hostname and IP address. Once the user has passed inspection, theserver figures out the kind of data it's sendingback (e.g., it decides a file ending in <em class="emphasis">.html</em> isprobably a <tt class="literal">text/html</tt> file), creates some headers,and sends those headers back to the client with the resource itself.When all is said and done, the server makes a log entry.</p><p>At each stage of this process, Apache looks for routines to"handle" the request. That is, ifApache doesn't find handlers you'vetold it to use, it knows to use its own. For example, ifyou've enabled CGI programs in<em class="filename">httpd.conf</em>, Apache knows to execute programsthat live in <em class="emphasis">cgi-bin</em> if it encounters the<tt class="literal">cgi-script</tt> directive:</p><blockquote><pre class="code">&lt;Location /cgi-bin&gt;  ...SetHandler cgi-script  ...&lt;/Location&gt;</pre></blockquote><p><em class="emphasis">mod_perl</em> allows you to write your own handlersin Perl by embedding the Perl runtime library directly into theApache <em class="emphasis">httpd</em> server executable. To use<em class="emphasis">mod_perl</em> for CGI (which is all that most peoplewant to do with it), assign the <tt class="literal">SetHandler</tt><a name="INDEX-1720" /><a name="INDEX-1721" /><a name="INDEX-1722" />directive to<tt class="literal">perl-script</tt>, and the<em class="emphasis">mod_perl</em>-specific <tt class="literal">PerlHandler</tt>directive to a special Perl module called Apache::Registry:</p><blockquote><pre class="code">SetHandler perl-scriptPerlHandler Apache::Registry</pre></blockquote><p><tt class="literal">PerlHandler</tt> is the <em class="emphasis">mod_perl</em>handler for the content retrieval stage of the transaction.</p><p>To use other handlers, you don't need to reassign<tt class="literal">SetHandler</tt>. For example, to identify a handler forthe logging stage of the request:</p><blockquote><pre class="code">&lt;Location /snoop/&gt;PerlLogHandler Apache::DumpHeaders&lt;/Location&gt;</pre></blockquote><p>For this to work, <em class="emphasis">mod_perl</em> must have been builtwith the logging hooks enabled (as described in the previoussection)<a name="INDEX-1723" />, and the Apache::DumpHeaders modulemust have been installed. <em class="emphasis">mod_perl</em> looks inApache::DumpHeaders for a routine called <tt class="literal">handler()</tt> and executes it as the logging handler for that resource.</p><p>The following is a list of each of the handler directives that can beenabled by <em class="emphasis">mod_perl</em> and the stages that each isused for. Only <tt class="literal">PerlHandler</tt> is enabled by default.</p><a name="ch11-2-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Handler</p></th><th><p>Purpose</p></th></tr><tr><td><p><tt class="literal">PerlAccessHandler</tt></p></td><td><p>Access stage</p></td></tr><tr><td><p><tt class="literal">PerlAuthenHandler</tt></p></td><td><p>Authentication stage</p></td></tr><tr><td><p><tt class="literal">PerlAuthzHandler</tt></p></td><td><p>Authorization stage</p></td></tr><tr><td><p><tt class="literal">PerlChildInitHandler</tt></p></td><td><p>Child initialization stage</p></td></tr><tr><td><p><tt class="literal">PerlChildExitHandler</tt></p></td><td><p>Child termination stage</p></td></tr><tr><td><p><tt class="literal">PerlCleanupHandler</tt></p></td><td><p>Cleanup stage</p></td></tr><tr><td><p><tt class="literal">PerlFixupHandler</tt></p></td><td><p>Fixup stage</p></td></tr><tr><td><p><tt class="literal">PerlHandler</tt></p></td><td><p>Response stage</p></td></tr><tr><td><p><tt class="literal">PerlHeaderParserHandler</tt></p></td><td><p>Header-parsing stage</p></td></tr><tr><td><p><tt class="literal">PerlInitHandler</tt></p></td><td><p>Initialization</p></td></tr><tr><td><p><tt class="literal">PerlLogHandler</tt></p></td><td><p>Logging stage</p></td></tr><tr><td><p><tt class="literal">PerlPostReadRequestHandler</tt></p></td><td><p>Post-request stage</p></td></tr><tr><td><p><tt class="literal">PerlTransHandler</tt></p></td><td><p>Translation stage</p></td></tr><tr><td><p><tt class="literal">PerlTypeHandler</tt></p></td><td><p>Type-handling stage</p></td></tr></table><p><p>You can write your own handlers for each of these stages. But thereare also dozens of modules that you can download from CPAN, some ofwhich are listed at the end of this chapter.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch11_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">11.2. Installing mod_perl</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">11.4. Running CGI Scripts with mod_perl</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

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