| 1 | package HTTP::Proxy::FilterStack; | ||||||||||||||
| 2 | 67 | $HTTP::Proxy::FilterStack::VERSION = '0.304'; | |||||||||||||
| 3 | # Here's a description of the class internals | ||||||||||||||
| 4 | # - filters: the list of (sub, filter) pairs that match the message, | ||||||||||||||
| 5 | # and through which it must go | ||||||||||||||
| 6 | # - current: the actual list of filters, which is computed during | ||||||||||||||
| 7 | # the first call to filter() | ||||||||||||||
| 8 | # - buffers: the buffers associated with each (selected) filter | ||||||||||||||
| 9 | # - body : true if it's a HTTP::Proxy::BodyFilter stack | ||||||||||||||
| 10 | |||||||||||||||
| 11 | 67 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 67 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | # new( $isbody ) | ||||||||||||||
| 15 | # $isbody is true only for response-body filters stack | ||||||||||||||
| 16 | 238 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 17 | 238 | my $class = shift; | |||||||||||||
| 18 | 238 | ▶ | my $self = { | ||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 19 | body => shift || 0, | ||||||||||||||
| 20 | filters => [], | ||||||||||||||
| 21 | buffers => [], | ||||||||||||||
| 22 | current => undef, | ||||||||||||||
| 23 | }; | ||||||||||||||
| 24 | 238 | ▶ | $self->{type} = $self->{body} ? "HTTP::Proxy::BodyFilter" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | : "HTTP::Proxy::HeaderFilter"; | ||||||||||||||
| 26 | 238 | return bless $self, $class; | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | # | ||||||||||||||
| 30 | # insert( $index, [ $matchsub, $filter ], ...) | ||||||||||||||
| 31 | # | ||||||||||||||
| 32 | 3 | ▶ | sub insert { | ||||||||||||
|
Subroutine
insert: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 33 | 3 | my ( $self, $idx ) = ( shift, shift ); | |||||||||||||
| 34 | 3 | ▶ | $_->[1]->isa( $self->{type} ) or croak("$_ is not a $self->{type}") for @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 1 | splice @{ $self->{filters} }, $idx, 0, @_; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | # | ||||||||||||||
| 39 | # remove( $index ) | ||||||||||||||
| 40 | # | ||||||||||||||
| 41 | 1 | ▶ | sub remove { | ||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 42 | 1 | my ( $self, $idx ) = @_; | |||||||||||||
| 43 | 1 | splice @{ $self->{filters} }, $idx, 1; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | # | ||||||||||||||
| 47 | # push( [ $matchsub, $filter ], ... ) | ||||||||||||||
| 48 | # | ||||||||||||||
| 49 | 152 | ▶ | sub push { | ||||||||||||
|
Subroutine
push: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 50 | 152 | my $self = shift; | |||||||||||||
| 51 | 152 | ▶ | $_->[1]->isa( $self->{type} ) or croak("$_ is not a $self->{type}") for @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 150 | push @{ $self->{filters} }, @_; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 5 | ▶ | sub all { return @{ $_[0]->{filters} }; } | ||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 56 | 38 | ▶ | sub will_modify { return $_[0]->{will_modify}; } | ||||||||||||
|
Subroutine
will_modify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | # | ||||||||||||||
| 59 | # select the filters that will be used on the message | ||||||||||||||
| 60 | # | ||||||||||||||
| 61 | 131 | ▶ | sub select_filters { | ||||||||||||
|
Subroutine
select_filters: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||
| 62 | 131 | my ($self, $message ) = @_; | |||||||||||||
| 63 | |||||||||||||||
| 64 | # first time we're called this round | ||||||||||||||
| 65 | 131 | ▶ | if ( not defined $self->{current} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | # select the filters that match | ||||||||||||||
| 68 | $self->{current} = | ||||||||||||||
| 69 | 124 | [ map { $_->[1] } grep { $_->[0]->() } @{ $self->{filters} } ]; | |||||||||||||
| 70 | |||||||||||||||
| 71 | # create the buffers | ||||||||||||||
| 72 | 124 | ▶ | if ( $self->{body} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 64 | $self->{buffers} = [ ( "" ) x @{ $self->{current} } ]; | |||||||||||||
| 74 | 64 | $self->{buffers} = [ \( @{ $self->{buffers} } ) ]; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | # start the filter if needed (and pass the message) | ||||||||||||||
| 78 | 124 | for ( @{ $self->{current} } ) { | |||||||||||||
| 79 | 78 | ▶ | if ( $_->can('begin') ) { $_->begin( $message ); } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 80 | elsif ( $_->can('start') ) { | ||||||||||||||
| 81 | 0 | $_->proxy->log( HTTP::Proxy::ERROR(), "DEPRECATION", "The start() filter method is *deprecated* and disappeared in 0.15!\nUse begin() in your filters instead!" ); | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | # compute the "will_modify" value | ||||||||||||||
| 86 | $self->{will_modify} = $self->{body} | ||||||||||||||
| 87 | 124 | ▶ | ? grep { $_->will_modify() } @{ $self->{current} } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | : 0; | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | # | ||||||||||||||
| 93 | # the actual filtering is done here | ||||||||||||||
| 94 | # | ||||||||||||||
| 95 | 123 | ▶ | sub filter { | ||||||||||||
|
Subroutine
filter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 96 | 123 | my $self = shift; | |||||||||||||
| 97 | |||||||||||||||
| 98 | # pass the body data through the filter | ||||||||||||||
| 99 | 123 | ▶ | if ( $self->{body} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 64 | my $i = 0; | |||||||||||||
| 101 | 64 | my ( $data, $message, $protocol ) = @_; | |||||||||||||
| 102 | 64 | for ( @{ $self->{current} } ) { | |||||||||||||
| 103 | 21 | $$data = ${ $self->{buffers}[$i] } . $$data; | |||||||||||||
| 104 | 21 | ${ $self->{buffers}[ $i ] } = ""; | |||||||||||||
| 105 | 21 | $_->filter( $data, $message, $protocol, $self->{buffers}[ $i++ ] ); | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | } | ||||||||||||||
| 108 | else { | ||||||||||||||
| 109 | 59 | $_->filter(@_) for @{ $self->{current} }; | |||||||||||||
| 110 | 59 | $self->eod; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | # | ||||||||||||||
| 115 | # filter what remains in the buffers | ||||||||||||||
| 116 | # | ||||||||||||||
| 117 | 30 | ▶ | sub filter_last { | ||||||||||||
|
Subroutine
filter_last: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 118 | 30 | my $self = shift; | |||||||||||||
| 119 | 30 | ▶ | return unless $self->{body}; # sanity check | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | |||||||||||||||
| 121 | 30 | my $i = 0; | |||||||||||||
| 122 | 30 | my ( $data, $message, $protocol ) = @_; | |||||||||||||
| 123 | 30 | for ( @{ $self->{current} } ) { | |||||||||||||
| 124 | 5 | $$data = ${ $self->{buffers}[ $i ] } . $$data; | |||||||||||||
| 125 | 5 | ${ $self->{buffers}[ $i++ ] } = ""; | |||||||||||||
| 126 | 5 | $_->filter( $data, $message, $protocol, undef ); | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | # call the cleanup routine if needed | ||||||||||||||
| 130 | 30 | ▶ | for ( @{ $self->{current} } ) { $_->end if $_->can('end'); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | |||||||||||||||
| 132 | # clean up the mess for next time | ||||||||||||||
| 133 | 30 | $self->eod; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | # | ||||||||||||||
| 137 | # END OF DATA cleanup method | ||||||||||||||
| 138 | # | ||||||||||||||
| 139 | 120 | ▶ | sub eod { | ||||||||||||
|
Subroutine
eod: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 140 | 120 | $_[0]->{buffers} = []; | |||||||||||||
| 141 | 120 | $_[0]->{current} = undef; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 67 | 1; | |||||||||||||
| 145 | |||||||||||||||
| 146 | __END__ | ||||||||||||||