modperl_module_example_regex_as_PerlHandler

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Subject: RE: stripping CRLF on the way out?
From: Geoffrey Young <gyoung@laserlink.net>
Date: Thu, 22 Jun 2000 10:41:44 -0400

I wrote a quick handler that implements a regex as a PerlHandler

maybe this will help to strip out comments:

(oh, and if anyone would like to see this as an official module, I can clean
it up and release it - I didn't really think there would be much interest in
it when I wrote it...)


package Custom::Regex;

#---------------------------------------------------------------------
# usage: PerlHandler Custom::Regex
#
#        the following variables will be entered into a regex as:
#        s/$RegexChange/$RegexTo/eeg
#
#        PerlSetVar RegexChange "change this"
#        PerlSetVar RegexTo  "to that"
#
#        PerlSetVar Filter On       # optional - will work within 
#                                   # Apache::Filter
#---------------------------------------------------------------------

use Apache::Constants qw( OK DECLINED SERVER_ERROR );
use Apache::File;
use Apache::Log;
use strict;

$Custom::Regex::VERSION = '0.01';

sub handler {
#---------------------------------------------------------------------
# initialize request object and variables
#---------------------------------------------------------------------
  
  my $r         = shift;
  my $log       = $r->server->log;

  my $change    = $r->dir_config('RegexChange') || undef;
  my $to        = $r->dir_config('RegexTo') || undef;

  # make Apache::Filter aware
  my $filter    = $r->dir_config('Filter') =~ m/On/i ? 1 : 0;

  my ($fh, $status, $output);      

#---------------------------------------------------------------------
# do some preliminary stuff...
#---------------------------------------------------------------------

  $log->info("Using Custom::Regex");

  unless ($r->content_type eq 'text/html') {
    $log->info("\trequest is not for an html document - skipping..."); 
    $log->info("Exiting Custom::Regex");  
    return DECLINED; 
  }

#---------------------------------------------------------------------
# get the filehandle
#---------------------------------------------------------------------

  if ($filter) {
    $log->info("\tgetting input from Apache::Filter");
    ($fh, $status) = $r->filter_input;
  } else {
    $log->info("\tgetting input from requested Apache::File");
    $fh = Apache::File->new($r->filename);
  }

  if (!$fh || $status ne OK) {
    $log->warn("\tcannot open request! $!");
    $log->info("Exiting Custom::Regex");
    return DECLINED;
  }

#---------------------------------------------------------------------
# do the regex on the request
#---------------------------------------------------------------------
  
  if ($change && $to) {

    $log->info("\tsubstituting $to for $change");

    while (<$fh>) {

      my $output;
      eval { ($output = $_) =~ s/$change/$to/eeg };
      if ($@) {
        $log->error("\tsubstitution error: $@");
        $log->info("Exiting Custom::Regex");
        return SERVER_ERROR;
      } else {
        print $output;
      }
    }
  }

#---------------------------------------------------------------------
# wrap up...
#---------------------------------------------------------------------

  $log->info("Exiting Custom::Regex");

  return OK;
}

1;

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu