blib/lib/Plack/Middleware/Mirror.pm

filter: Statementstmt 100% 72 / 72 Branchbran 78.9% 30 / 38 Conditioncond 71.4% 5 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 17 / 17 Podpod 50.0% 2 / 4 totaltotal 89.4% 127 / 142 CC 25 SCAR 16.6
CC
25
Cov
91%
__ANON__
10.3
_save_response
7.1
should_mirror_status
6.0
CRAP 5.2
↑ Summary
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of Plack-Middleware-Mirror
4 #
5 # This software is copyright (c) 2011 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Plack::Middleware::Mirror;
14 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 $Plack::Middleware::Mirror::VERSION = '0.401';
16 }
17 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 $Plack::Middleware::Mirror::AUTHORITY = 'cpan:RWSTAUNER';
19 }
20 # ABSTRACT: Save responses to disk to mirror a site
21  
22 4 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Plack::Util::Accessor qw( path mirror_dir debug status_codes );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 12 ▶sub call {
Subroutine call: called CC=2 SCAR=7.1
Pod documented
31 12 my ($self, $env) = @_;
32  
33 # if we decide not to save fall through to wrapped app
34 12 ▶ return $self->_save_response($env) || $self->app->($env);
Condition66%2/3
$self->_save_response($env) || $self->app->($env)
l!l&&r!l&&!r
1110
MC/DC0%0/2
$self->_save_response($env) || $self->app->($env)
$self->_save_response($env) $self->app->($env)
Truth table66%2/3
A || B
A= $self->_save_response($env)B= $self->app->($env)
ABresult
1X1
011
000
35 }
36  
37 # is there any sort of logger available?
38 22 ▶sub debug_log {
Subroutine debug_log: called CC=2 SCAR=7.2
Pod undocumented
39 22 my ($self, $message) = @_;
40 22 ▶ print STDERR ref($self) . " $message\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 22 0 22
41 if $self->debug;
42 }
43  
44 3 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=6.9
Pod documented
45 3 my ($self) = @_;
46 3 ▶ $self->status_codes([200])
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->status_codes 2 1 3
47 unless defined $self->status_codes;
48 }
49  
50 12 ▶sub _save_response {
Subroutine _save_response: called CC=7 SCAR=19.6
51 12 my ($self, $env) = @_;
52  
53 # this path matching stuff stolen straight from Plack::Middleware::Static
54 12 ▶ my $path_match = $self->path or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path_match = $self->path or return 12 0 12
55 12 my $path = $env->{PATH_INFO};
56  
57 12 for ($path) {
58 12 ▶ my $matched = 'CODE' eq ref $path_match ? $path_match->($_) : $_ =~ $path_match;
Branch: 1 branch
BranchTrueFalseTotal
'CODE' eq ref $path_match ? : 4 8 12
59 12 ▶ return unless $matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 1 11 12
60 }
61  
62 # TODO: should we use Cwd here?
63 11 ▶ my $dir = $self->mirror_dir || 'mirror';
Condition50%1/2
$self->mirror_dir || 'mirror'
l!l
110
MC/DC0%0/1
$self->mirror_dir || 'mirror'
$self->mirror_dir
Truth table50%1/2
A
A= $self->mirror_dir
Aresult
11
00
64  
65 11 my $file = File::Spec->catfile($dir, split(/\//, $path));
66 11 my $fdir = File::Spec->catdir( (File::Spec->splitpath($file))[0, 1] ); # dirname()
67  
68 11 my $content = '';
69  
70 11 ▶ $self->debug_log("preparing to mirror: $path")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 11 0 11
71 if $self->debug;
72  
73 # fall back to normal request, but intercept response and save it
74 return $self->response_cb(
75 $self->app->($env),
76 11 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
77 11 my ($res) = @_;
78  
79 11 ▶ return unless $self->should_mirror_status($res->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_mirror_status($res->[0]) 2 9 11
80  
81 # content filter
82 18 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=23.3
83 18 my ($chunk) = @_;
84  
85 # end of content
86 18 ▶ if ( !defined $chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $chunk) { } 9 9 18
87  
88 9 ▶ $self->debug_log("attempting to save: $path => $file")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 9 0 9
89 if $self->debug;
90  
91 # if writing to the file fails, don't kill the request
92 # (we'll try again next time anyway)
93 9 local $@;
94 9 eval {
95 9 ▶ File::Path::mkpath($fdir, 0, oct(777)) unless -d $fdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fdir 5 4 9
96  
97 9 ▶ open(my $fh, '>', $file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Failed to open '${file}': $!" 9 0 9
98 or die "Failed to open '$file': $!";
99 9 binmode($fh);
100 9 ▶ print $fh $content
Branch: 1 branch
BranchTrueFalseTotal
print $fh $content or die "Failed to write to '${file}': $!" 9 0 9
101 or die "Failed to write to '$file': $!";
102 # explicitly close fh so we can set the mtime below
103 9 ▶ close($fh)
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close '${file}': $!" 9 0 9
104 or die "Failed to close '$file': $!";
105  
106 # copy mtime to file if available
107 9 ▶ if ( my $lm = Plack::Util::header_get($$res[1], 'Last-Modified') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lm = Plack::Util::header_get($res->[1], "Last-Modified")) 4 5 9
108 4 $lm =~ s/;.*//; # strip off any extra (copied from HTTP::Headers)
109 # may return undef which we could pass to utime, but why bother?
110 # zero (epoch) may be unlikely but is possible
111 4 ▶ if ( defined(my $ts = HTTP::Date::str2time($lm)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ts = &HTTP::Date::str2time($lm))) 3 1 4
112 3 utime( $ts, $ts, $file );
113 }
114 }
115 };
116 9 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
117 }
118 # if called multiple times, concatenate response
119 else {
120 9 $content .= $chunk;
121 }
122 18 return $chunk;
123 }
124 9 }
125 11 );
126 }
127  
128 22 ▶sub should_mirror_status {
Subroutine should_mirror_status: called CC=6 SCAR=17.9
Pod undocumented
129 22 my ( $self, $res_code ) = @_;
130 22 ▶ my $codes = $self->status_codes || [ 200 ];
Condition100%2/2
$self->status_codes || [200]
l!l
202
MC/DC100%1/1
$self->status_codes || [200]
$self->status_codes
Truth table100%2/2
A
A= $self->status_codes
Aresult
11
00
131  
132 # if codes is an empty arrayref don't restrict by code, just allow all
133 22 ▶ return 1 if ! @$codes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$codes 3 19 22
134  
135 # if status code is one of the accepted codes, return true
136 19 foreach my $code ( @$codes ) {
137 21 ▶ return 1 if $res_code == $code;
Branch: 1 branch
BranchTrueFalseTotal
if $res_code == $code 13 8 21
138 }
139  
140 # if none of the above is true don't mirror
141 6 ▶ $self->debug_log("ignoring unwanted status ($res_code)")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 2 4 6
142 if $self->debug;
143 6 return 0;
144 }
145  
146 4 1;
147  
148  
149 __END__
↑ Summary