Proxy/FilterStack.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 90.9% 20 / 22 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 12 / 12 Podpod 100% 10 / 10 totaltotal 97.5% 120 / 123 CC 20 SCAR 13.8
CC
20
Cov
97%
select_filters
7.0
filter_last
5.0
filter
4.0
CRAP 4.0
« Proxy/BodyFilter/simple.pm ↑ Summary Proxy/Engine.pm »
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
l!l
119119
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
19 body => shift || 0,
20 filters => [],
21 buffers => [],
22 current => undef,
23 };
24 238 ▶ $self->{type} = $self->{body} ? "HTTP::Proxy::BodyFilter"
Branch: 1 branch
BranchTrueFalseTotal
$self->{'body'} ? : 119 119 238
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
BranchTrueFalseTotal
$_->[1]->isa($self->{"type"}) or croak(qq[$_ is not a $self->{"type"}]) 1 2 3
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
BranchTrueFalseTotal
$_->[1]->isa($self->{"type"}) or croak(qq[$_ is not a $self->{"type"}]) 151 2 153
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
BranchTrueFalseTotal
if (not defined $self->{"current"}) 124 7 131
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
BranchTrueFalseTotal
if ($self->{"body"}) 64 60 124
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
BranchTrueFalseTotal
if ($_->can('begin')) { } 3 75 78
elsif ($_->can('start')) { } 0 75 75
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
BranchTrueFalseTotal
$self->{'body'} ? : 64 60 124
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
BranchTrueFalseTotal
if ($self->{'body'}) { } 64 59 123
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
BranchTrueFalseTotal
unless $self->{"body"} 0 30 30
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
BranchTrueFalseTotal
if $_->can("end") 1 4 5
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__
« Proxy/BodyFilter/simple.pm ↑ Summary Proxy/Engine.pm »