Writing Apache Modules with Perl and C
By:   Lincoln Stein and Doug MacEachern
Published:   O'Reilly & Associates, Inc.  - March 1999

Copyright © 1999 by O'Reilly & Associates, Inc.


 


   Show Contents   Previous Page   Next Page

Chapter 7 - Other Request Phases
Customizing the Fixup Phase

The fixup phase is sandwiched between the type checking phase and the response phase. It gives modules a last-minute chance to add information to the environment or to modify the request record before the content handler is invoked. For instance, the standard mod_usertrack module implements the Cookie-Tracking directive in this phase, adding a user-tracking cookie to the outgoing HTTP headers and recording a copy of the incoming cookie to the notes table for logging purposes.

As an example of a useful Perl-based fixup handler, we'll look at Apache::HttpEquiv, a module written by Rob Hartill and used here with his permission. The idea of Apache::HttpEquiv is simple. The module scans the requested HTML file for any <META> tags containing the HTTP-EQUIV and CONTENT attributes. The information is then added to the outgoing HTTP headers.

For example, if the requested file contains this HTML:

<HTML>
<HEAD><TITLE>My Page</TITLE>
<META HTTP-EQUIV="Expires" CONTENT="Wed, 31 Jul 1998 16:40:00 GMT">
<META HTTP-EQUIV="Set-Cookie" CONTENT="open=sesame">

the handler will convert the <META> tags into these response headers:

Expires: Wed, 31 Jul 1998 16:40:00 GMT
Set-Cookie: open=sesame

Example 7-7 gives the succinct code for Apache::HttpEquiv. The handler() routine begins by testing the current request for suitability. It returns with a status code of DECLINED if any of the following are true:

  • The request is a subrequest.
  • The requested document's MIME type is something other than text/html.
  • The requested file cannot be opened.

The second item is the main reason that this module has to be run as a fixup handler. Prior to this phase, the MIME type of the document is not known because the MIME type checker hasn't yet run.

Next the handler scans through the requested file, line by line, looking for suitable <META> tags. If any are found, the request object's header_out() method is called to set the indicated header. To gain a little bit of efficiency, the subroutine aborts the search early when a <BODY> or </HEAD> tag is encountered.

Once the file is completely scanned, the subroutine closes and return an OK status code.

To configure Apache::HttpEquiv, add the following line to your configuration file:

<Location /httpequiv>
PerlFixupHandler Apache::HttpEquiv
</Location>

Example 7-7. Apache::HttpEquiv Turns <META> Tags into HTTP Headers

package Apache::HttpEquiv;
# file: Apache/HttpEquiv.pm
use strict;
use Apache::Constants qw(:common);
sub handler {
  my $r = shift;
  local(*FILE);
   return DECLINED if # don't scan the file if..
      !$r->is_main # a subrequest
          || $r->content_type ne "text/html" # it isn't HTML
              || !open(FILE, $r->filename); # we can't open it
   while(<FILE>) {
      last if m!<BODY>|</HEAD>!i; # exit early if in BODY
      if (m/META HTTP-EQUIV="([^"]+)"\s+CONTENT="([^"]+)"/i) {
$r->header_out($1 => $2); } } close(FILE); return OK; }
1;
__END__
   Show Contents   Previous Page   Next Page
Copyright © 1999 by O'Reilly & Associates, Inc.

HIVE: All information for read only. Please respect copyright!
Hosted by hive КГБ: Киевская городская библиотека