Proxy/BodyFilter/save.pm

filter: Statementstmt 97.8% 93 / 95 Branchbran 92.5% 50 / 54 Conditioncond 93.7% 15 / 16 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 95.3% 184 / 193 CC 35 SCAR 27.2
CC
35
Cov
96%
begin
25.1
init
8.0
filter
3.0
CRAP 15.2
« Proxy.pm ↑ Summary Proxy/HeaderFilter/standard.pm »
1 package HTTP::Proxy::BodyFilter::save;
2 3 $HTTP::Proxy::BodyFilter::save::VERSION = '0.304';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use HTTP::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTTP::Proxy::BodyFilter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 @ISA = qw( HTTP::Proxy::BodyFilter );
8 3 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 ▶sub init {
Subroutine init: called CC=8 SCAR=20.8
Pod documented
14 24 my $self = shift;
15  
16 # options
17 24 my %args = (
18 template => File::Spec->catfile( '%h', '%P' ),
19 no_host => 0,
20 no_dirs => 0,
21 cut_dirs => 0,
22 prefix => '',
23 filename => undef,
24 multiple => 1,
25 keep_old => 0, # no_clobber in wget parlance
26 timestamp => 0,
27 status => [ 200 ],
28 @_
29 );
30 # keep_old and timestamp can't be selected together
31 croak "Can't timestamp and keep older files at the same time"
32 24 ▶ if $args{keep_old} && $args{timestamp};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"keep_old"} and $args{"timestamp"} 1 23 24
Condition100%3/3
$args{"keep_old"} and $args{"timestamp"}
!ll&&!rl&&r
2211
MC/DC100%2/2
$args{"keep_old"} and $args{"timestamp"}
$args{"keep_old"} $args{"timestamp"}
Truth table100%3/3
A and B
A= $args{"keep_old"}B= $args{"timestamp"}
ABresult
0X0
100
111
33 croak "status must be an array reference"
34 23 ▶ unless ref($args{status}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"status"} eq "ARRAY" 1 22 23
35 croak "status must contain only HTTP codes"
36 22 ▶ if grep { !/^[12345]\d\d$/ } @{ $args{status} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /^[12345]\d\d$/;} @{$args{"status"};} 1 21 22
37 croak "filename must be a code reference"
38 21 ▶ if defined $args{filename} && !UNIVERSAL::isa( $args{filename}, 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"filename"} and not UNIVERSAL::isa($args{"filename"}, "CODE") 1 20 21
Condition100%3/3
defined $args{"filename"} and not UNIVERSAL::isa($args{"filename"}, "CODE")
!ll&&!rl&&r
1911
MC/DC100%2/2
defined $args{"filename"} and not UNIVERSAL::isa($args{"filename"}, "CODE")
defined $args{"filename"} not UNIVERSAL::isa($args{"filename"}, "CODE")
Truth table100%3/3
A and not(B)
A= defined $args{"filename"}B= not UNIVERSAL::isa($args{"filename"}, "CODE")
ABresult
0X0
100
111
39  
40 20 $self->{"_hpbf_save_filename_code"} = $args{filename};
41 $self->{"_hpbf_save_$_"} = $args{$_}
42 20 for qw( template no_host no_dirs cut_dirs prefix
43 multiple keep_old timestamp status );
44 }
45  
46 23 ▶sub begin {
Subroutine begin: called CC=25 SCAR=32.2
Pod documented
47 23 my ( $self, $message ) = @_;
48  
49 # internal data initialisation
50 23 delete @{$self}{qw( _hpbf_save_filename _hpbf_save_fh )};
51  
52 23 ▶ my $uri = $message->isa( 'HTTP::Request' )
Branch: 1 branch
BranchTrueFalseTotal
$message->isa('HTTP::Request') ? : 19 4 23
53 ? $message->uri : $message->request->uri;
54  
55 # save only the accepted status codes
56 23 ▶ if( $message->isa( 'HTTP::Response' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->isa("HTTP::Response")) 4 19 23
57 4 my $code = $message->code;
58 4 ▶ return unless grep { $code eq $_ } @{ $self->{_hpbf_save_status} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$code eq $_;} @{$$self{"_hpbf_save_status"};} 1 3 4
59 }
60
61 22 my $file = '';
62 22 ▶ if( defined $self->{_hpbf_save_filename_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_hpbf_save_filename_code'}) { } 4 18 22
63 # use the user-provided callback
64 4 $file = $self->{_hpbf_save_filename_code}->($message);
65 4 ▶ unless ( defined $file and $file ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file and $file ne "") 2 2 4
Condition100%3/3
defined $file and $file ne ""
!ll&&!rl&&r
112
MC/DC100%2/2
defined $file and $file ne ""
defined $file $file ne ""
Truth table100%3/3
A and B
A= defined $fileB= $file ne ""
ABresult
0X0
100
111
66 2 $self->proxy->log( HTTP::Proxy::FILTERS, "HTBF::save",
67 "Filter will not save $uri" );
68 2 return;
69 }
70 }
71 else {
72 # set the template variables from the URI
73 18 my @segs = $uri->path_segments; # starts with an empty string
74 18 shift @segs;
75 splice(@segs, 0, $self->{_hpbf_save_cut_dirs} >= @segs
76 18 ▶ ? @segs - 1 : $self->{_hpbf_save_cut_dirs} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_hpbf_save_cut_dirs'} >= @segs ? : 1 17 18
77 my %vars = (
78 '%' => '%',
79 h => $self->{_hpbf_save_no_host} ? '' : $uri->host,
80 f => $segs[-1] || 'index.html', # same default as wget
81 18 ▶ p => $self->{_hpbf_save_no_dirs} ? $segs[-1] || 'index.html'
Branch: 2 branches
BranchTrueFalseTotal
$self->{'_hpbf_save_no_host'} ? : 4 14 18
$self->{'_hpbf_save_no_dirs'} ? : 3 15 18
Condition100%4/4
$segs[-1] || 'index.html'
l!l
162
$segs[-1] or "index.html"
l!l
21
MC/DC100%1/1
$segs[-1] || 'index.html'
$segs[-1]
MC/DC100%1/1
$segs[-1] or "index.html"
$segs[-1]
Truth table100%2/2
A
A= $segs[-1]
Aresult
11
00
Truth table100%2/2
A
A= $segs[-1]
Aresult
11
00
82 : File::Spec->catfile(@segs),
83 q => $uri->query,
84 );
85 18 pop @segs;
86 $vars{d}
87 18 ▶ = $self->{_hpbf_save_no_dirs} ? ''
Branch: 2 branches
BranchTrueFalseTotal
$self->{'_hpbf_save_no_dirs'} ? : 3 15 18
@segs ? : 9 6 15
88 : @segs ? File::Spec->catfile(@segs)
89 : '';
90 18 ▶ $vars{P} = $vars{p} . ( $vars{q} ? "?$vars{q}" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$vars{'q'} ? : 2 16 18
91
92 # create the filename
93 $file = File::Spec->catfile( $self->{_hpbf_save_prefix} || (),
94 18 ▶ $self->{_hpbf_save_template} );
Condition66%2/3
$self->{'_hpbf_save_prefix'} || ()
l!l&&r!l&&!r
1350
MC/DC0%0/2
$self->{'_hpbf_save_prefix'} || ()
$self->{'_hpbf_save_prefix'} ()
Truth table66%2/3
A || B
A= $self->{'_hpbf_save_prefix'}B= ()
ABresult
1X1
011
000
95 18 $file =~ s/%(.)/$vars{$1}/g;
96 }
97 20 $file = File::Spec->rel2abs( $file );
98  
99 # create the directory
100 20 my $dir = File::Spec->catpath( (File::Spec->splitpath($file))[ 0, 1 ], '' );
101 20 ▶ if( ! -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 5 15 20
102 5 eval { mkpath( $dir ) };
103 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
104 0 $self->proxy->log( HTTP::Proxy::ERROR, "HTBF::save",
105 "Unable to create directory $dir" );
106 0 return;
107 }
108 5 $self->proxy->log( HTTP::Proxy::FILTERS, "HTBF::save",
109 "Created directory $dir" );
110 }
111  
112 # keep old file?
113 20 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 3 17 20
114 3 ▶ if ( $self->{_hpbf_save_timestamp} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_hpbf_save_timestamp'}) { } 0 3 3
elsif ($self->{'_hpbf_save_keep_old'}) { } 1 2 3
115 # FIXME timestamp
116 }
117 elsif ( $self->{_hpbf_save_keep_old} ) {
118 1 $self->proxy->log( HTTP::Proxy::FILTERS, "HPBF::save",
119 "Skip saving $uri" );
120 1 delete $self->{_hpbf_save_fh}; # it's a closed filehandle
121 1 return;
122 }
123 }
124  
125 # open and lock the file
126 19 my ( $ext, $n, $i ) = ( "", 0 );
127 19 my $flags = O_WRONLY | O_EXCL | O_CREAT;
128 19 ▶ while( ! sysopen( $self->{_hpbf_save_fh}, "$file$ext", $flags ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $self->{"_hpbf_save_fh"}, "$file$ext", $flags) 2 19 21
129 2 ▶ $self->proxy->log( HTTP::Proxy::ERROR, "HPBF::save",
Branch: 1 branch
BranchTrueFalseTotal
if $i++ - $n == 10 0 2 2
130 "Too many errors opening $file$ext" ), return
131 if $i++ - $n == 10; # should be ok now
132 2 ▶ if( $self->{_hpbf_save_multiple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_hpbf_save_multiple'}) { } 1 1 2
133 1 ▶ $ext = "." . ++$n while -e $file.$ext;
Branch: 1 branch
BranchTrueFalseTotal
-e $file . $ext and do { $ext = '.' . ++$n } 1 1 2
134 1 next;
135 }
136 else {
137 1 $flags = O_WRONLY | O_CREAT;
138 }
139 }
140  
141 # we have an open filehandle
142 19 $self->{_hpbf_save_filename} = $file.$ext;
143 19 binmode( $self->{_hpbf_save_fh} ); # for Win32 and friends
144 19 $self->proxy->log( HTTP::Proxy::FILTERS, "HPBF::save",
145 "Saving $uri to $file$ext" );
146 }
147  
148 8 ▶sub filter {
Subroutine filter: called CC=3 SCAR=11.0
Pod documented
149 8 my ( $self, $dataref ) = @_;
150 8 ▶ return unless exists $self->{_hpbf_save_fh};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_hpbf_save_fh"} 4 4 8
151  
152 # save the data to the file
153 4 my $res = $self->{_hpbf_save_fh}->syswrite( $$dataref );
154 4 ▶ $self->proxy->log( HTTP::Proxy::ERROR, "HPBF::save", "syswrite() error: $!")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $res 0 4 4
155 if ! defined $res; # FIXME error handling
156 }
157  
158 4 ▶sub end {
Subroutine end: called CC=2 SCAR=6.9
Pod documented
159 4 my ($self) = @_;
160  
161 # close file
162 4 ▶ if( $self->{_hpbf_save_fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_hpbf_save_fh"}) 2 2 4
163 2 $self->{_hpbf_save_fh}->close; # FIXME error handling
164 2 delete $self->{_hpbf_save_fh};
165 }
166 }
167  
168 2 ▶sub will_modify { 0 }
Subroutine will_modify: called CC=1 SCAR=0.0
Pod documented
169  
170 3 1;
171  
172 __END__
« Proxy.pm ↑ Summary Proxy/HeaderFilter/standard.pm »