blib/lib/CGI/Application/Plugin/CaptureIO.pm

filter: Statementstmt 33.6% 38 / 113 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 42.8% 9 / 21 Podpod 100% 4 / 4 totaltotal 23.0% 51 / 221 CC 29 SCAR 34.0
CC
29
Cov
21%
_store_capture
72.0
__ANON__
56.0
capture_init
42.0
CRAP 30.0
↑ Summary
1 package CGI::Application::Plugin::CaptureIO;
2  
3 =pod
4  
5 =head1 NAME
6  
7 CGI::Application::Plugin::CaptureIO - Plug-in capture cache
8  
9 =head1 VERSION
10  
11 0.01
12  
13 =head1 SYNOPSIS
14  
15 use Your::App;
16 use CGI::Application::Plugin::CaptureIO;
17  
18 =head1 DESCRIPTION
19  
20 When all output contents are preserved in the cache, and the same next request is transmitted,
21  
22 it is a plug-in that outputs contents preserved in the cache.
23  
24 The effect is demonstrated as a load measures on the screen where the update is not very frequent
25  
26 though there are a lot of requests.
27  
28 =cut
29  
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 our(
41 1 $CAPTURE_CLEAR,
42 $CAPTURE_DEPTH,
43 $CAPTURE_DIR,
44 $CAPTURE_MODE,
45 $CAPTURE_PREFIX,
46 $CAPTURE_SEPARATOR,
47 $CAPTURE_TTL,
48 @EXPORT,
49 $VERSION
50 );
51  
52 1 $CAPTURE_CLEAR = 0;
53 1 $CAPTURE_DEPTH = 4;
54 1 $CAPTURE_DIR = "/tmp/cgiapp-capture/";
55 1 $CAPTURE_MODE = "_capture_output";
56 1 $CAPTURE_PREFIX = "cgiapp_capture-";
57 1 $CAPTURE_SEPARATOR = ":";
58 1 $CAPTURE_TTL = 60;
59 1 @EXPORT = qw(
60 add_non_capture_runmodes
61 capture_init
62 current_url
63 delete_non_capture_runmodes
64 );
65 1 $VERSION = 0.01;
66  
67 0 ▶sub import {
Subroutine import: not called CC=1 SCAR=6.9
68  
69 0 my $pkg = caller;
70  
71 0 $pkg->add_callback("init", \&_capture_init);
72 0 $pkg->add_callback("prerun", \&_restore_capture);
73 0 $pkg->add_callback("postrun", \&_store_capture);
74  
75 0 goto &Exporter::import;
76 }
77  
78 =pod
79  
80 =head1 METHOD
81  
82 =head2 add_non_capture_runmodes
83  
84 Runmode that doesn't preserve contents in the cache is set.
85  
86 Example:
87  
88 sub setup {
89
90 my $self = shift;
91 $self->run_modes(
92 mode => "mode1",
93 mode2 => "mode2",
94 non_capture => "non_capture",
95 non_capture2 => "non_capture2",
96 );
97 # Neither runmode non_capture nor non_capture2 cache contents.
98 $self->add_non_capture_runmodes(qw(non_capture non_capture2));
99 }
100  
101 =cut
102  
103 0 ▶sub add_non_capture_runmodes {
Subroutine add_non_capture_runmodes: not called CC=1 SCAR=6.9
Pod documented
104  
105 0 my($self, @runmodes) = @_;
106 0 map { $self->{__CAP_CAPTUREIO_CONFIG}->{NON_CAPTURE_RM}->{$_} = 1 } @runmodes;
107 }
108  
109 =pod
110  
111 =head2 capture_init
112  
113 It sets it in the early preserving capture.
114  
115 Option:
116  
117 capture_clear : The contents capture file and the directory not referred to whenever capture_init is executed when setting
118 it to "1" are deleted.
119 The file and the directory from which several or more are not accessed are deleted at
120 the second set with capture_ttl option.
121 defualt 0
122  
123 capture_ttl : Contents are acquired, and output from the contents capture file in second when the
124 contents capture file specified that it preserves contents once survival time (The unit: second)
125 with capture_ttl number's passing.
126 default 60
127  
128 capture_dir : Directory that preserves contents capture file. default /tmp/cgiapp-capture/
129  
130 non_capture_rm : Runmode that doesn't preserve contents in the cache is set.
131  
132  
133 Example:
134  
135 sub cgiapp_init {
136
137 my $self = shift;
138 $self->capture_init(
139 capture_clear => 0,
140 capture_ttl => 60,
141 capture_dir => "/home/akira/myapp/tmp",
142 non_capture_rm => [qw(non_capture1 non_capture2)],
143 );
144 }
145  
146 =cut
147  
148 0 ▶sub capture_init {
Subroutine capture_init: not called CC=6 SCAR=37.4
Pod documented
149  
150 0 my($self, %args) = @_;
151  
152 $self->{__CAP_CAPTUREIO_CONFIG} = {
153 CAPTURE_CLEAR => $args{capture_clear} || $CAPTURE_CLEAR,
154 CAPTURE_DIGEST => _create_digest($self),
155 CAPTURE_TTL => $args{capture_ttl} || $CAPTURE_TTL,
156 CAPTURE_DIR => $args{capture_dir} || $CAPTURE_DIR,
157 0 ▶ NON_CAPTURE_RM => (ref($args{non_capture_rm}) eq "ARRAY") ? { map { $_ => 1 } @{$args{non_capture_rm}} } : {},
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'non_capture_rm'} eq 'ARRAY' ? : 0 0 0
Condition0%0/9
$args{'capture_clear'} || $CGI::Application::Plugin::CaptureIO::CAPTURE_CLEAR
l!l&&r!l&&!r
000
$args{'capture_ttl'} || $CGI::Application::Plugin::CaptureIO::CAPTURE_TTL
l!l&&r!l&&!r
000
$args{'capture_dir'} || $CGI::Application::Plugin::CaptureIO::CAPTURE_DIR
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'capture_clear'} || $CGI::Application::Plugin::CaptureIO::CAPTURE_CLEAR
$args{'capture_clear'} $CGI::Application::Plugin::CaptureIO::CAPTURE_CLEAR
MC/DC0%0/2
$args{'capture_ttl'} || $CGI::Application::Plugin::CaptureIO::CAPTURE_TTL
$args{'capture_ttl'} $CGI::Application::Plugin::CaptureIO::CAPTURE_TTL
MC/DC0%0/2
$args{'capture_dir'} || $CGI::Application::Plugin::CaptureIO::CAPTURE_DIR
$args{'capture_dir'} $CGI::Application::Plugin::CaptureIO::CAPTURE_DIR
Truth table0%0/3
A || B
A= $args{'capture_clear'}B= $CGI::Application::Plugin::CaptureIO::CAPTURE_CLEAR
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $args{'capture_ttl'}B= $CGI::Application::Plugin::CaptureIO::CAPTURE_TTL
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $args{'capture_dir'}B= $CGI::Application::Plugin::CaptureIO::CAPTURE_DIR
ABresult
1X1
011
000
158 };
159  
160 0 ▶ if($self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_CLEAR}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__CAP_CAPTUREIO_CONFIG"}{"CAPTURE_CLEAR"}) 0 0 0
161 # capture auto clear
162 0 ▶ find(sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
163
164 0 my $ttl = $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_TTL};
165 0 my $st = stat($_);
166 0 ▶ if(-d $_ && (time - $st->atime) > $ttl){
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_ and time - $st->atime > $ttl) { } 0 0 0
elsif (-e $_ and -B $_) { } 0 0 0
Condition0%0/3
-d $_ and time - $st->atime > $ttl
!ll&&!rl&&r
000
MC/DC0%0/2
-d $_ and time - $st->atime > $ttl
-d $_ time - $st->atime > $ttl
Truth table0%0/3
A and B
A= -d $_B= time - $st->atime > $ttl
ABresult
0X0
100
111
167 0 rmtree($File::Find::dir, 0);
168 ▶ }elsif(-e $_ && -B $_){
Condition0%0/3
-e $_ and -B $_
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_ and -B $_
-e $_ -B $_
Truth table0%0/3
A and B
A= -e $_B= -B $_
ABresult
0X0
100
111
169  
170 0 ▶ if($_ =~ /^$CAPTURE_PREFIX/ && (time - $st->atime) > $ttl){
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^$CGI::Application::Plugin::CaptureIO::CAPTURE_PREFIX/ and time - $st->atime > $ttl) 0 0 0
Condition0%0/3
$_ =~ /^$CGI::Application::Plugin::CaptureIO::CAPTURE_PREFIX/ and time - $st->atime > $ttl
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /^$CGI::Application::Plugin::CaptureIO::CAPTURE_PREFIX/ and time - $st->atime > $ttl
$_ =~ /^$CGI::Application::Plugin::CaptureIO::CAPTURE_PREFIX/ time - $st->atime > $ttl
Truth table0%0/3
A and B
A= $_ =~ /^$CGI::Application::Plugin::CaptureIO::CAPTURE_PREFIX/B= time - $st->atime > $ttl
ABresult
0X0
100
111
171 0 unlink $File::Find::name;
172 }
173 }
174 },
175 0 $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_DIR});
176  
177 }
178 }
179  
180 =pod
181  
182 =head2 current_url
183  
184 Current URL is returned.
185  
186 Example:
187  
188 sub mode1 {
189
190 my $self = shift;
191  
192 # when current url is http://www.hogehoge.hoge/app?mode=mode1, http://www.hogehoge.hoge/app?mode=mode1 is stored in $current_url
193 my $current_url = $self->current_url;
194 }
195  
196 =cut
197  
198 # copy from Sledge Web Application Framework
199 0 ▶sub current_url {
Subroutine current_url: not called CC=3 SCAR=24.8
Pod documented
200  
201 0 my($self, $schema) = @_;
202 0 ▶ $schema ||= ($ENV{HTTPS}) ? "https" : "http";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HTTPS'} ? : 0 0 0
Condition0%0/3
$schema ||= $ENV{"HTTPS"} ? "https" : "http"
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema ||= $ENV{"HTTPS"} ? "https" : "http"
$schema $ENV{"HTTPS"} ? "https" : "http"
Truth table0%0/3
A ||= B
A= $schemaB= $ENV{"HTTPS"} ? "https" : "http"
ABresult
1X1
011
000
203 0 return sprintf "%s://%s%s", $schema, $ENV{HTTP_HOST}, $ENV{REQUEST_URI};
204 }
205  
206 =pod
207  
208 =head2 delete_non_capture_runmodes
209  
210 Runmode that doesn't preserve the capture contents set with add_non_capture_runmodes and
211  
212 capture_init is released.
213  
214 Example:
215  
216 $self->delete_non_capture_runmodes(qw(capture_mode1 capture_mode2));
217  
218 =cut
219  
220 0 ▶sub delete_non_capture_runmodes {
Subroutine delete_non_capture_runmodes: not called CC=2 SCAR=17.9
Pod documented
221  
222 0 my($self, @runmodes) = @_;
223 map {
224 0 ▶ if(exists $self->{__CAP_CAPTUREIO_CONFIG}->{NON_CAPTURE_RM}->{$_}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"__CAP_CAPTUREIO_CONFIG"}{"NON_CAPTURE_RM"}{$_}) 0 0 0
225 0 delete $self->{__CAP_CAPTUREIO_CONFIG}->{NON_CAPTURE_RM}->{$_};
226 }
227 } @runmodes;
228 }
229  
230 # ============================================================= #
231 # add_callback "init" #
232 # ============================================================= #
233 0 ▶sub _capture_init {
Subroutine _capture_init: not called CC=2 SCAR=17.9
234  
235 0 my $self = shift;
236 0 ▶ $self->capture_init if !$self->{__CAP_CAPTUREIO_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"__CAP_CAPTUREIO_CONFIG"} 0 0 0
237 }
238  
239 # ============================================================= #
240 # add_callback "prerun" #
241 # ============================================================= #
242 0 ▶sub _restore_capture {
Subroutine _restore_capture: not called CC=5 SCAR=34.0
243  
244 0 my($self, $rm) = @_;
245 0 ▶ return if $ENV{REQUEST_METHOD} eq "POST";
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"REQUEST_METHOD"} eq "POST" 0 0 0
246  
247 0 my $digest = $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_DIGEST};
248 0 my $ttl = $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_TTL};
249 0 my $capture_dir = _capture_dir($self);
250 0 my $capture = File::Spec->catfile($capture_dir, $CAPTURE_PREFIX . $digest);
251  
252 0 ▶ if(-e $capture && -B $capture){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $capture and -B $capture) 0 0 0
Condition0%0/3
-e $capture and -B $capture
!ll&&!rl&&r
000
MC/DC0%0/2
-e $capture and -B $capture
-e $capture -B $capture
Truth table0%0/3
A and B
A= -e $captureB= -B $capture
ABresult
0X0
100
111
253  
254 0 my $st = stat($capture);
255 0 ▶ if((time - $st->mtime) <= $ttl){
Branch: 1 branch
BranchTrueFalseTotal
if (time - $st->mtime <= $ttl) { } 0 0 0
256  
257 0 my $ref = Storable::retrieve($capture);
258 0 ▶ $self->run_modes( $CAPTURE_MODE => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
259
260 0 my $self = shift;
261 0 $self->header_props(%{$ref->{header}});
262 0 return ${$ref->{body}};
263 0 });
264 0 $self->prerun_mode($CAPTURE_MODE);
265 0 $self->add_non_capture_runmodes($ref->{rm});
266  
267 }else{
268  
269 0 unlink $capture;
270 }
271 }
272  
273 }
274  
275 # ============================================================= #
276 # add_callback "postrun" #
277 # ============================================================= #
278 0 ▶sub _store_capture {
Subroutine _store_capture: not called CC=8 SCAR=42.8
279  
280 0 my($self, $scalarref) = @_;
281 0 my $rm = $self->get_current_runmode;
282  
283 # POST REQUEST or $CAPTURE_MODE(_capture_output) or non capture mode is non capture
284 0 ▶ return if $ENV{REQUEST_METHOD} eq "POST";
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"REQUEST_METHOD"} eq "POST" 0 0 0
285 0 ▶ return if $CAPTURE_MODE eq $rm;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Application::Plugin::CaptureIO::CAPTURE_MODE eq $rm 0 0 0
286 0 ▶ return if exists $self->{__CAP_CAPTUREIO_CONFIG}->{NON_CAPTURE_RM}->{$rm};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"__CAP_CAPTUREIO_CONFIG"}{"NON_CAPTURE_RM"}{$rm} 0 0 0
287  
288 0 my %props = $self->header_props;
289 my %header = (
290 -type => $props{-type} || "text/html",
291 0 ▶ -charset => $props{-charset} || "utf-8",
Condition0%0/4
$props{'-type'} || 'text/html'
l!l
00
$props{'-charset'} || 'utf-8'
l!l
00
MC/DC0%0/1
$props{'-type'} || 'text/html'
$props{'-type'}
MC/DC0%0/1
$props{'-charset'} || 'utf-8'
$props{'-charset'}
Truth table0%0/2
A
A= $props{'-type'}
Aresult
11
00
Truth table0%0/2
A
A= $props{'-charset'}
Aresult
11
00
292 );
293 0 my $capture_dir = _capture_dir($self);
294 0 my $capture = File::Spec->catfile($capture_dir, $CAPTURE_PREFIX . $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_DIGEST});
295  
296 # create capture directory
297 0 ▶ eval { mkpath($capture_dir, 0) } if not -e $capture_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $capture_dir 0 0 0
298 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
299 0 croak("Can not create directory $capture_dir [$@]");
300 }
301  
302 Storable::nstore({
303 0 body => $scalarref,
304 header => \%header,
305 rm => $rm,
306 }, $capture);
307 }
308  
309  
310 0 ▶sub _create_digest {
Subroutine _create_digest: not called CC=2 SCAR=17.9
311  
312 0 my $self = shift;
313 0 my $sha1 = Digest::SHA1->new;
314 0 ▶ $sha1->add(join $CAPTURE_SEPARATOR, $self->current_url, $ENV{HTTP_USER_AGENT} || "nonbrowser/$VERSION");
Condition0%0/2
$ENV{'HTTP_USER_AGENT'} || "nonbrowser/$CGI::Application::Plugin::CaptureIO::VERSION"
l!l
00
MC/DC0%0/1
$ENV{'HTTP_USER_AGENT'} || "nonbrowser/$CGI::Application::Plugin::CaptureIO::VERSION"
$ENV{'HTTP_USER_AGENT'}
Truth table0%0/2
A
A= $ENV{'HTTP_USER_AGENT'}
Aresult
11
00
315 0 return $sha1->hexdigest;
316 }
317  
318 0 ▶sub _capture_dir {
Subroutine _capture_dir: not called CC=2 SCAR=17.9
319  
320 0 my $self = shift;
321 0 my $capture_dir;
322 0 my $len = 2;
323 0 my @dirs;
324 0 my($digest) = $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_DIGEST};
325
326 0 push @dirs, $self->{__CAP_CAPTUREIO_CONFIG}->{CAPTURE_DIR};
327  
328 0 ▶ for(my $i = 0;$i < $CAPTURE_DEPTH;$i += $len){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $CGI::Application::Plugin::CaptureIO::CAPTURE_DEPTH) 0 0 0
329 0 push @dirs, substr($digest, $i, $len);
330 }
331 0 return File::Spec->catfile(@dirs);
332 }
333  
334 1 1;
335  
336  
337 __END__
↑ Summary