Cover | Table of Contents | Colophon
http://www.imdb.com/) uses
mod_perl to make queries against a vast database
of film and television movies. The system rewrites URLs on the fly in
order to present pages in the language of the user's choice and
to quickly retrieve the results of previously cached searches. In
1998, the site won the coveted Webby award for design and service.http://www.apache.org/.http://www.apache.org/dist/, and
ApacheModulePerl.dll
,
which is mod_perl implemented as a dynamically
loadable module. ApacheModulePerl.dll has been
made available by Jeffrey W. Baker. You can find it on the
Comprehensive Perl Archive Network (CPAN) in the directory
authors/Jeffrey_Baker/. Win32 users with access to the Microsoft
Visual C++ development environment can also compile
ApacheModulePerl.dll from
mod_perl source code.http://www.apache.org/.http://www.apache.org/dist/, and
ApacheModulePerl.dll
,
which is mod_perl implemented as a dynamically
loadable module. ApacheModulePerl.dll has been
made available by Jeffrey W. Baker. You can find it on the
Comprehensive Perl Archive Network (CPAN) in the directory
authors/Jeffrey_Baker/. Win32 users with access to the Microsoft
Visual C++ development environment can also compile
ApacheModulePerl.dll from
mod_perl source code.ftp://prep.ai.mit.edu, in the directory
/pub/gnu, or via the web at http://www.gnu.org/.% cd ~www/build % gunzip -c mod_perl- X.XX.tar.gz | tar xvf - mod_perl-X.XX/t/ mod_perl-X.XX/t/docs/ mod_perl-X.XX/t/docs/env.iphtml mod_perl-X.XX/t/docs/content.shtml mod_perl-X.XX/t/docs/error.txt .... % cd mod_perl- X.XX
PERL5LIB
to a colon-delimited list of
directories to search before Apache starts up or by calling
use
lib
'/
path
/
to
/
look
/
in
'
when the interpreter is first launched. The first technique is most
convenient to use in conjunction with the
PerlSetEnv
directive, which sets an environment
variable. Place this directive somewhere early in your server
configuration file:PerlSetEnv PERL5LIB /my/lib/perl:/other/lib/perl
#!/usr/local/bin/perl
# modify the include path before we do anything else
BEGIN {
use Apache ();
use lib Apache->server_root_relative('lib/perl');
}
# commonly used modules
use Apache::Registry ();
use Apache::Constants();
use CGI qw(-compile :all);
use CGI::Carp ();
# put any other common modules here
# use Apache::DBI ();
# use LWP ();
# use DB_File ();
1;http://www.modperl.com/book/source/).#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
/* file: mod_hello.c */
/* here's the content handler */
static int hello_handler(request_rec *r) {
const char* hostname;
r->content_type = "text/html";
ap_send_http_header(r);
hostname = ap_get_remote_host(r->connection,r->per_dir_config,REMOTE_NAME);
ap_rputs("<HTML>\n" ,r);
ap_rputs("<HEAD>\n" ,r);
ap_rputs("<TITLE>Hello There</TITLE>\n" ,r);
ap_rputs("</HEAD>\n" ,r);
ap_rputs("<BODY>\n" ,r);
ap_rprintf(r,"<H1>Hello %s</H1>\n" ,hostname);
ap_rputs("Who would take this book seriously if the first example didn't\n",r);
ap_rputs("say \"hello world\"?\n" ,r);
ap_rputs("</BODY>\n" ,r);
ap_rputs("</HTML>\n" ,r);
return OK;
}
/* Make the name of the content handler known to Apache */
static handler_rec hello_handlers[] =
{
{"hello-handler", hello_handler},
{NULL}
};
/* Tell Apache what phases of the transaction we handle */
module MODULE_VAR_EXPORT hello_module =
{
STANDARD_MODULE_STUFF,
NULL, /* module initializer */
NULL, /* per-directory config creator */
NULL, /* dir config merger */
NULL, /* server config creator */
NULL, /* server config merger */
NULL, /* command table */
hello_handlers, /* [9] content handlers */
NULL, /* [2] URI-to-filename translation */
NULL, /* [5] check/validate user_id */
NULL, /* [6] check user_id is valid *here* */
NULL, /* [4] check access by host address */
NULL, /* [7] MIME type checker/setter */
NULL, /* [8] fixups */
NULL, /* [10] logger */
NULL, /* [3] header parser */
NULL, /* process initialization */
NULL, /* process exit/cleanup */
NULL /* [1] post read_request handling */
};CFLAGS
environment variable before running the configure
script:% CFLAGS=-g ./configure ...
% gdb httpd (gdb) run -X -f ~www/conf/httpd.conf
(gdb) prompt will return and tell
you which function caused the crash. Now that you have an idea of
where the problem is coming from, a breakpoint can be set to step
through and see exactly what is wrong. If we were debugging
sub handler { my $r = shift; # do something return SOME_STATUS_CODE; }
$r. The handler retrieves whatever
information it needs from the request object, does some processing,
and possibly modifies the object to suit its needs. The handler then
returns a numeric status code as its function result, informing
Apache of the outcome of its work. We discuss the list of status
codes and their significance in the next section.($$)
indicating that the subroutine takes
two scalar arguments, the Perl API treats the handler as an
object-oriented method call. In this case, the handler will receive
two arguments. The handler's class (package) name or an object
reference will be the first argument, and the Apache request object
reference will be the second. This allows handlers to take advantage
of class inheritance, polymorphism, and other useful object-oriented
features. Handlers that use this feature are called "method
handlers" and have the following structure:request_rec
.
Included among its various fields are pointers to a
server_rec and a
conn_rec
structure, which correspond to the Perl API's
Apache::Server and
Apache::Connection objects. We have much more to
say about using the request_rec in Chapters Chapter 10 and Chapter 11 when we
discuss the C-language API in more detail.request_rec
in
C) is the primary conduit for the transfer of information between
modules and the server. Handlers can use the request object to
perform several types of operations:|
Physical Directory
|
Translated Filename |
|---|
OK
status code if header_only() returns true (this
is a slight improvement over the original Chapter 2
version of the program). We call get_remote_host(
)
to get the DNS
name of the remote host machine, and incorporate the name into a
short HTML document that we transmit using the request object's
print( ) method. At the end of the handler, we
return OK.REDIRECT
result code. A complete functional
example using mod_perl is only a few lines
(Example 4.8). This module, named
Apache::GoHome
, redirects users to the hardcoded URI
http://www.ora.com/. When the user selects a
document or a portion of the document tree that this content handler
has been attached to, the browser will immediately jump to that URI.REDIRECT error
code from Apache::Constants
(REDIRECT isn't among the standard set of
result codes imported with :common). The
handler() method then adds the desired location
to the outgoing headers by calling
Apache::
header_out( ).
header_out( )
can take one or two
arguments. Called with one argument, it returns the current value of
the indicated HTTP header field. Called with two arguments, it sets
the field indicated by the first argument to the value indicated by
the second argument. In this case, we use the two-argument form to
set the HTTP Location field to the desired URI.REDIRECT result
code. There's no need to generate an HTML body, since most
HTTP-compliant browsers will take you directly to the
Location URI. However, Apache adds an
appropriate body automatically in order to be HTTP-compliant. You can
see the header and body message using telnet:% telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
? character:http://your.site/uri/path?name1=val1&name2=val2&name3=val3...
Alias /perl/ /usr/local/apache/perl/ <Location /perl> SetHandler perl-script PerlHandler Apache::Registry PerlSendHeader On Options +ExecCGI </Location>