Advanced Rewriting

The post-read-request stage of request processing can be used for tasks similar to the Request Rewriting module. For example, a webmaster might want to use referrer checking to make sure that users enter the site through the front page, installing the following code at the post-read-request stage as Rewriting->referrer:

package Rewriting;

use strict;
use Zeus::ModPerl;
use Zeus::ModPerl::Constants qw( :response );

my $host = 'my.web.site';

sub referrer
{
    my $pkg   = shift;
    my $r   = shift;
    my $ref = $r->header_in( 'Referer' );  # HTTP spells this wrongly
    my $re  = qr|^http://\Q$host\E|;

    if( $ref !~ $re )
    {
        my $action = $r->dir_config( 'ReferrerAction' );

        if( defined($action) and (lc($action) eq 'reject') )
        {
            return FORBIDDEN;
        }
        else
        {
            my $uri = $r->uri();
            $uri =~ s,/[^/]*$,,;
            $r->uri( "$uri/denied.html" );
        }
    }

    return DECLINED;
}

1;

When the uri method is called with an argument, it causes a notification to be sent to the web server. In this case, that notification will instruct the server to update its record of the requested URL. The handler returns DECLINED to indicate that other post-read-request handlers may still be run.

As another example, you might wish to have some user-agent-specific pages, installing the following code on the URI Translation hook as Rewriting->user_agent:

sub user_agent
{
    my $pkg = shift;
    my $r   = shift;
    my $ua  = $r->header_in( 'User-Agent' );

    if( $ua =~ /Netscape/ )
    {
        $r->uri     ( $r->uri()      . '.netscape' );
        $r->filename( $r->filename() . '.netscape' );
    }
    elsif( $ua =~ /Explorer/ )
    {
        $r->uri     ( $r->uri()      . '.explorer' );
        $r->filename( $r->filename() . '.explorer' );
    }

    return OK;
}

We use the URI Translation hook here to take advantage of the fact that the web server has already done the hard work of mapping the URI to a filename. However, this means that the handler must keep the physical filename up to date itself using the filename method.

To install either of these examples using the Administration Server user interface, save the source code as $ZEUSHOME/zperl/local/lib/site_perl/Rewriting.pm, enable Perl Extensions in the user interface, add the paths that should be controlled, and add either Rewriting->referrer as a post-read-request handler or Rewriting->user_agent as a URI translation handler as appropriate.

Content Manager [Administrator] 16 December 2005  Permalink  
Download Free Trial

Recent Articles

Other Resources



www.zeus.com