Show Contents Previous Page Next Page Chapter 4 - Content Handlers In this section...
If you are using Apache::Registry is similar in concept to the content filters we created earlier in this chapter, but instead of performing simple string substitutions on the contents of the requested file, We created a typical configuration file entry for Apache::Registry in Chapter 2. Let's examine it in more detail now. Alias /perl/ /usr/local/apache/perl/ <Location /perl> SetHandler perl-script PerlHandler Apache::Registry PerlSendHeader On Options +ExecCGI </Location> The Alias directive simply maps the physical directory /usr/local/apache/perl/ to a virtual directory named /perl. The <Location> section is more interesting. It uses SetHandler to make perl-script the content handler for this directory and sets Apache::Registry to be the module to handle requests for files within this part of the document tree.
The Option +ExecCGI ordinarily tells Apache that it's all right for the directory to contain CGI scripts. In this case the flag is required by Apache::Registry to confirm that you really know what you're doing. In addition, all scripts located in directories handled by Apache::Registry must be executable--another check against accidentally leaving wayward nonscript files in the directory. When you use Apache::Registry, you can program in either of two distinct styles. You can choose to ignore the Apache Perl API entirely and act as if your script were executed within a CGI environment, or you can ignore the CGI compatibility features and make Apache API calls. You can also combine both programming styles in a single script, although you run the risk of confusing yourself and anyone else who needs to maintain your code! A typical example of the first style is the hello.pl script (Example 4-12), which you also saw in Chapter 2. The interesting thing about this script is that there's nothing Apache-specific about it. The same script will run as a standard CGI script under Apache or any other web server. Any library modules that rely on the CGI environment will work as well. Example 4-12. An Apache::Registry Script That Uses CGI-Compatibility Mode #!/usr/local/bin/perl # file: hello.pl print "Content-Type: text/html\n\n"; print <<END; <HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello $ENV{REMOTE_HOST}</H1> Who would take this book seriously if the examples didn't say "hello world" in at least four different ways? Example 4-13 shows the same script rewritten more compactly by taking advantage of the various shortcuts provided by the CGI.pm module. Example 4_13. An Apache::Registry Script That Uses CGI.pm #!/usr/local/bin/perl # file: hello2.pl use CGI qw(:standard); print header, start_html('Hello There'), h1('Hello',remote_host()), 'Who would take this book seriously if the examples', 'didn\'t say "hello world" in at least four different ways?', end_html; In contrast, Example 4-14 shows the script
written in the Apache Perl API style. If you compare the script to Example 4-7,
which used the vanilla API to define its own content handler, you'll see that
the contents of this script (with the exception of the
There are also some subtle differences between $r->status(404); # forbidden Strictly speaking, it isn't necessary to call send_http_header() if you have PerlSendHeader On. However, it is good practice to do so, and it won't lead to redundant headers being printed. Alternatively, you can use the CGI compatibility mode to set the status by printing out an HTTP header that contains a Status: field: print "Status: 404 Forbidden\n\n";
Another subtle difference is that at least one of the command-line switches that may be found on the topmost
Since Apache::Registry scripts can do double duty as normal CGI scripts and as Example 4-14. An Apache::Registry Script That Uses the Apache API #!/usr/local/bin/perl # file: hello3.pl use strict; my $r = Apache->request; $r->content_type('text/html'); $r->send_http_header; return OK if $r->header_only; my $host = $r->get_remote_host; $r->print(<<END); <HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello $host</H1> Enough with the "Hello worlds" already! </BODY> </HTML> ENDShow Contents Go to Top Previous Page Next Page Copyright © 1999 by O'Reilly & Associates, Inc. |
HIVE: All information for read only. Please respect copyright! |