1package HTTP::Proxy::HeaderFilter::simple; 2 3use strict; 4use Carp; 5use HTTP::Proxy::HeaderFilter; 6use vars qw( @ISA ); 7@ISA = qw( HTTP::Proxy::HeaderFilter ); 8 9my $methods = join '|', qw( begin filter end ); 10$methods = qr/^(?:$methods)$/; 11 12sub init { 13 my $self = shift; 14 15 croak "Constructor called without argument" unless @_; 16 if ( @_ == 1 ) { 17 croak "Single parameter must be a CODE reference" 18 unless ref $_[0] eq 'CODE'; 19 $self->{_filter} = $_[0]; 20 } 21 else { 22 $self->{_filter} = sub { }; # default 23 while (@_) { 24 my ( $name, $code ) = splice @_, 0, 2; 25 26 # basic error checking 27 croak "Parameter to $name must be a CODE reference" 28 unless ref $code eq 'CODE'; 29 croak "Unkown method $name" unless $name =~ $methods; 30 31 $self->{"_$name"} = $code; 32 } 33 } 34} 35 36# transparently call the actual methods 37sub begin { goto &{ $_[0]{_begin} }; } 38sub filter { goto &{ $_[0]{_filter} }; } 39sub end { goto &{ $_[0]{_end} }; } 40 41sub can { 42 my ( $self, $method ) = @_; 43 return $method =~ $methods 44 ? $self->{"_$method"} 45 : UNIVERSAL::can( $self, $method ); 46} 47 481; 49 50__END__ 51 52=head1 NAME 53 54HTTP::Proxy::HeaderFilter::simple - A class for creating simple filters 55 56=head1 SYNOPSIS 57 58 use HTTP::Proxy::HeaderFilter::simple; 59 60 # a simple User-Agent filter 61 my $filter = HTTP::Proxy::HeaderFilter::simple->new( 62 sub { $_[1]->header( User_Agent => 'foobar/1.0' ); } 63 ); 64 $proxy->push_filter( request => $filter ); 65 66=head1 DESCRIPTION 67 68HTTP::Proxy::HeaderFilter::simple can create BodyFilter without going 69through the hassle of creating a full-fledged class. Simply pass 70a code reference to the filter() method of your filter to the constructor, 71and you'll get the adequate filter. 72 73=head2 Constructor calling convention 74 75The constructor is called with a single code reference. 76The code reference must conform to the standard filter() signature 77for header filters: 78 79 sub filter { my ( $self, $headers, $message) = @_; ... } 80 81This code reference is used for the filter() method. 82 83=head1 METHODS 84 85This filter "factory" defines the standard HTTP::Proxy::HeaderFilter 86methods, but those are only, erm, "proxies" to the actual CODE references 87passed to the constructor. These "proxy" methods are: 88 89=over 4 90 91=item filter() 92 93=item begin() 94 95=item end() 96 97=back 98 99Two other methods are actually HTTP::Proxy::HeaderFilter::simple methods, 100and are called automatically: 101 102=over 4 103 104=item init() 105 106Initalise the filter instance with the code references passed to the 107constructor. 108 109=item can() 110 111Return the actual code reference that will be run, and not the "proxy" 112methods. If called with any other name than C<begin> and C<filter>, 113it calls UNIVERSAL::can() instead. 114 115=back 116 117=head1 SEE ALSO 118 119L<HTTP::Proxy>, L<HTTP::Proxy::HeaderFilter>. 120 121=head1 AUTHOR 122 123Philippe "BooK" Bruhat, E<lt>book@cpan.orgE<gt>. 124 125=head1 COPYRIGHT 126 127Copyright 2003-2005, Philippe Bruhat. 128 129=head1 LICENSE 130 131This module is free software; you can redistribute it or modify it under 132the same terms as Perl itself. 133 134=cut 135 136