blib/lib/Egg/Plugin/Cache/UA.pm

filter: Statementstmt 30.3% 27 / 89 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 47.0% 8 / 17 Podpod 100% 1 / 1 totaltotal 17.0% 36 / 211 CC 37 SCAR 43.2
CC
37
Cov
16%
get
210.0
_setup
56.9
output
56.0
CRAP 75.4
↑ Summary
1 package Egg::Plugin::Cache::UA;
2 #
3 # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4 #
5 # $Id: UA.pm 306 2008-03-07 10:55:58Z lushe $
6 #
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use base qw/ Egg::Plugin::LWP /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '1.01';
12  
13 0 ▶sub _setup {
Subroutine _setup: not called CC=8 SCAR=40.4
14 0 my($e)= @_;
15 0 ▶ my $conf= $e->config->{plugin_cache_ua} ||= {};
Condition0%0/2
$e->config->{"plugin_cache_ua"} ||= {}
l!l
00
MC/DC0%0/1
$e->config->{"plugin_cache_ua"} ||= {}
$e->config->{"plugin_cache_ua"}
Truth table0%0/2
A
A= $e->config->{"plugin_cache_ua"}
Aresult
11
00
16 0 ▶ $conf->{content_type} ||= 'text/html';
Condition0%0/2
$conf->{"content_type"} ||= "text/html"
l!l
00
MC/DC0%0/1
$conf->{"content_type"} ||= "text/html"
$conf->{"content_type"}
Truth table0%0/2
A
A= $conf->{"content_type"}
Aresult
11
00
17 0 ▶ $conf->{content_type_error} ||= 'text/html';
Condition0%0/2
$conf->{"content_type_error"} ||= "text/html"
l!l
00
MC/DC0%0/1
$conf->{"content_type_error"} ||= "text/html"
$conf->{"content_type_error"}
Truth table0%0/2
A
A= $conf->{"content_type_error"}
Aresult
11
00
18 0 ▶ $conf->{cache_name} || die q{ I want setup 'cache_name'. };
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"cache_name"} or die " I want setup 'cache_name'. " 0 0 0
19 0 ▶ $conf->{cache_expires} ||= undef;
Condition0%0/2
$conf->{"cache_expires"} ||= undef
l!l
00
MC/DC0%0/1
$conf->{"cache_expires"} ||= undef
$conf->{"cache_expires"}
Truth table0%0/2
A
A= $conf->{"cache_expires"}
Aresult
11
00
20 0 ▶ my $allows= $conf->{allow_hosts} || die q{ I want setup 'allow_hosts' };
Condition0%0/2
$conf->{'allow_hosts'} || die(q[ I want setup 'allow_hosts' ])
l!l
00
MC/DC0%0/1
$conf->{'allow_hosts'} || die(q[ I want setup 'allow_hosts' ])
$conf->{'allow_hosts'}
Truth table0%0/2
A
A= $conf->{'allow_hosts'}
Aresult
11
00
21 my $regex = join '|',
22 0 ▶ map{quotemeta}(ref($allows) eq 'ARRAY' ? @$allows: $allows);
Branch: 1 branch
BranchTrueFalseTotal
ref $allows eq 'ARRAY' ? : 0 0 0
23  
24 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 0 ▶ *Egg::Plugin::Cache::UA::handler::referer_check= sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
26 0 my($self)= @_;
27 0 ▶ my $referer= $self->e->request->referer || return 1;
Condition0%0/2
$self->e->request->referer || (return 1)
l!l
00
MC/DC0%0/1
$self->e->request->referer || (return 1)
$self->e->request->referer
Truth table0%0/2
A
A= $self->e->request->referer
Aresult
11
00
28 0 ▶ $referer=~m{^https?\://(?:$regex)} ? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$referer =~ m[^https?\://(?:$regex)] ? : 0 0 0
29 0 };
30  
31 0 $e->next::method;
32 }
33 0 ▶sub cache_ua {
Subroutine cache_ua: not called CC=2 SCAR=17.9
Pod documented
34 $_[0]->{cache_ua} ||= Egg::Plugin::Cache::UA::handler->new
35 0 ▶ ($_[0], $_[0]->config->{plugin_cache_ua});
Condition0%0/3
$_[0]{"cache_ua"} ||= "Egg::Plugin::Cache::UA::handler"->new($_[0], $_[0]->config->{"plugin_cache_ua"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"cache_ua"} ||= "Egg::Plugin::Cache::UA::handler"->new($_[0], $_[0]->config->{"plugin_cache_ua"})
$_[0]{"cache_ua"} "Egg::Plugin::Cache::UA::handler"->new($_[0], $_[0]->config->{"plugin_cache_ua"})
Truth table0%0/3
A ||= B
A= $_[0]{"cache_ua"}B= "Egg::Plugin::Cache::UA::handler"->new($_[0], $_[0]->config->{"plugin_cache_ua"})
ABresult
1X1
011
000
36 }
37  
38 package Egg::Plugin::Cache::UA::handler;
39 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use base qw/ Egg::Base /;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 *remove= \&delete;
45  
46 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
47 0 my $self= shift->SUPER::new(@_);
48 0 $self->{cache}= {};
49 0 $self;
50 }
51 0 ▶sub get {
Subroutine get: not called CC=14 SCAR=53.5
52 0 my($self, $url, $option)= __get_args(@_);
53 0 ▶ $self->referer_check || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->referer_check or return 0 0 0 0
54 0 ▶ my $result= $self->cache($option->{cache_name})->get($url) || do {
Condition0%0/3
$self->cache($option->{'cache_name'})->get($url) || do { my %attr; my $method = uc $option->{'request_method'} || 'GET'; if (my $res = $self->e->ua->request($method, $url)) { if ($res->is_success) { $attr{'is_success'} = 1; if (my $status = $res->status_line) { $attr{'status'} = $status unless $status =~ /^200/; } ; my(@content_type) = $res->header('content_type') || ''; $attr{'content_type'} = $content_type[0] || $option->{'content_type'}; $attr{'content'} = $res->content || ''; } else { $attr{'status'} = $res->status_line || '403 Forbidden'; $attr{'error'} = " Error in $url : " . $res->status_line; } ; } else { $attr{'status'} = '408 Request Time-out'; $attr{'error'} = " $url doesn't return the response. "; } ; $attr{'content_type'} ||= $option->{'content_type_error'}; $attr{'content'} ||= ''; $self->cache->set($url, \%attr, $option->{'cache_expires'}); $attr{'no_hit'} = 1; \%attr }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cache($option->{'cache_name'})->get($url) || do { my %attr; my $method = uc $option->{'request_method'} || 'GET'; if (my $res = $self->e->ua->request($method, $url)) { if ($res->is_success) { $attr{'is_success'} = 1; if (my $status = $res->status_line) { $attr{'status'} = $status unless $status =~ /^200/; } ; my(@content_type) = $res->header('content_type') || ''; $attr{'content_type'} = $content_type[0] || $option->{'content_type'}; $attr{'content'} = $res->content || ''; } else { $attr{'status'} = $res->status_line || '403 Forbidden'; $attr{'error'} = " Error in $url : " . $res->status_line; } ; } else { $attr{'status'} = '408 Request Time-out'; $attr{'error'} = " $url doesn't return the response. "; } ; $attr{'content_type'} ||= $option->{'content_type_error'}; $attr{'content'} ||= ''; $self->cache->set($url, \%attr, $option->{'cache_expires'}); $attr{'no_hit'} = 1; \%attr }
$self->cache($option->{'cache_name'})->get($url) do { my %attr; my $method = uc $option->{'request_method'} || 'GET'; if (my $res = $self->e->ua->request($method, $url)) { if ($res->is_success) { $attr{'is_success'} = 1; if (my $status = $res->status_line) { $attr{'status'} = $status unless $status =~ /^200/; } ; my(@content_type) = $res->header('content_type') || ''; $attr{'content_type'} = $content_type[0] || $option->{'content_type'}; $attr{'content'} = $res->content || ''; } else { $attr{'status'} = $res->status_line || '403 Forbidden'; $attr{'error'} = " Error in $url : " . $res->status_line; } ; } else { $attr{'status'} = '408 Request Time-out'; $attr{'error'} = " $url doesn't return the response. "; } ; $attr{'content_type'} ||= $option->{'content_type_error'}; $attr{'content'} ||= ''; $self->cache->set($url, \%attr, $option->{'cache_expires'}); $attr{'no_hit'} = 1; \%attr }
Truth table0%0/3
A || B
A= $self->cache($option->{'cache_name'})->get($url)B= do { my %attr; my $method = uc $option->{'request_method'} || 'GET'; if (my $res = $self->e->ua->request($method, $url)) { if ($res->is_success) { $attr{'is_success'} = 1; if (my $status = $res->status_line) { $attr{'status'} = $status unless $status =~ /^200/; } ; my(@content_type) = $res->header('content_type') || ''; $attr{'content_type'} = $content_type[0] || $option->{'content_type'}; $attr{'content'} = $res->content || ''; } else { $attr{'status'} = $res->status_line || '403 Forbidden'; $attr{'error'} = " Error in $url : " . $res->status_line; } ; } else { $attr{'status'} = '408 Request Time-out'; $attr{'error'} = " $url doesn't return the response. "; } ; $attr{'content_type'} ||= $option->{'content_type_error'}; $attr{'content'} ||= ''; $self->cache->set($url, \%attr, $option->{'cache_expires'}); $attr{'no_hit'} = 1; \%attr }
ABresult
1X1
011
000
55 0 my %attr;
56 0 ▶ my $method= uc $option->{request_method} || 'GET';
Condition0%0/2
uc $option->{'request_method'} || 'GET'
l!l
00
MC/DC0%0/1
uc $option->{'request_method'} || 'GET'
uc $option->{'request_method'}
Truth table0%0/2
A
A= uc $option->{'request_method'}
Aresult
11
00
57 0 ▶ if (my $res= $self->e->ua->request( $method => $url )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->e->ua->request($method, $url)) { } 0 0 0
58 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
59 0 $attr{is_success}= 1;
60 0 ▶ if (my $status= $res->status_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = $res->status_line) 0 0 0
61 0 ▶ $attr{status}= $status if $status!~/^200/;
Branch: 1 branch
BranchTrueFalseTotal
unless $status =~ /^200/ 0 0 0
62 }
63 0 ▶ my @content_type= $res->header('content_type') || "";
Condition0%0/2
$res->header('content_type') || ''
l!l
00
MC/DC0%0/1
$res->header('content_type') || ''
$res->header('content_type')
Truth table0%0/2
A
A= $res->header('content_type')
Aresult
11
00
64 $attr{content_type}= $content_type[0]
65 0 ▶ || $option->{content_type};
Condition0%0/3
$content_type[0] || $option->{'content_type'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$content_type[0] || $option->{'content_type'}
$content_type[0] $option->{'content_type'}
Truth table0%0/3
A || B
A= $content_type[0]B= $option->{'content_type'}
ABresult
1X1
011
000
66 0 ▶ $attr{content}= $res->content || "";
Condition0%0/2
$res->content || ''
l!l
00
MC/DC0%0/1
$res->content || ''
$res->content
Truth table0%0/2
A
A= $res->content
Aresult
11
00
67 } else {
68 0 ▶ $attr{status}= $res->status_line || '403 Forbidden';
Condition0%0/2
$res->status_line || '403 Forbidden'
l!l
00
MC/DC0%0/1
$res->status_line || '403 Forbidden'
$res->status_line
Truth table0%0/2
A
A= $res->status_line
Aresult
11
00
69 0 $attr{error} = " Error in $url : ". $res->status_line;
70 }
71 } else {
72 0 $attr{status}= "408 Request Time-out";
73 0 $attr{error} = " $url doesn't return the response. ";
74 }
75 0 ▶ $attr{content_type} ||= $option->{content_type_error};
Condition0%0/3
$attr{"content_type"} ||= $option->{"content_type_error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr{"content_type"} ||= $option->{"content_type_error"}
$attr{"content_type"} $option->{"content_type_error"}
Truth table0%0/3
A ||= B
A= $attr{"content_type"}B= $option->{"content_type_error"}
ABresult
1X1
011
000
76 0 ▶ $attr{content} ||= "";
Condition0%0/2
$attr{"content"} ||= ""
l!l
00
MC/DC0%0/1
$attr{"content"} ||= ""
$attr{"content"}
Truth table0%0/2
A
A= $attr{"content"}
Aresult
11
00
77 0 $self->cache->set($url, \%attr, $option->{cache_expires});
78 0 $attr{no_hit}= 1;
79 0 \%attr;
80 };
81 }
82 0 ▶sub output {
Subroutine output: not called CC=7 SCAR=40.3
83 0 my($self, $url, $option)= __get_args(@_);
84 my $cache= $self->get($url, $option) || {
85 no_hit => 1,
86 status => '500 Internal Server Error',
87 content_type => $option->{content_type_error},
88 0 ▶ error => ' referer is illegal.',
Condition0%0/2
$self->get($url, $option) || {'no_hit', 1, 'status', '500 Internal Server Error', 'content_type', $option->{'content_type_error'}, 'error', ' referer is illegal.'}
l!l
00
MC/DC0%0/1
$self->get($url, $option) || {'no_hit', 1, 'status', '500 Internal Server Error', 'content_type', $option->{'content_type_error'}, 'error', ' referer is illegal.'}
$self->get($url, $option)
Truth table0%0/2
A
A= $self->get($url, $option)
Aresult
11
00
89 };
90 0 my $response= $self->e->response;
91 $response->headers->header('X-CACHE-UA'=> 'hit')
92 0 ▶ unless $cache->{no_hit};
Branch: 1 branch
BranchTrueFalseTotal
unless $cache->{"no_hit"} 0 0 0
93 $response->is_expires($option->{expires})
94 0 ▶ if $option->{expires};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"expires"} 0 0 0
95 $response->last_modified($option->{last_modified})
96 0 ▶ if $option->{last_modified};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"last_modified"} 0 0 0
97 0 ▶ $response->status($cache->{status}) if $cache->{status};
Branch: 1 branch
BranchTrueFalseTotal
if $cache->{"status"} 0 0 0
98 0 $response->content_type($cache->{content_type});
99 0 ▶ $cache->{content}= $cache->{error} if $cache->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $cache->{"error"} 0 0 0
100 0 $response->body(\$cache->{content});
101 }
102 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
103 0 my $self= shift;
104 0 ▶ my($name, $url)= @_
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
@_ > 1 ? : 0 0 0
105 ? (@_ > 1 ? @_: (undef, shift)): croak q{ I want url. };
106 0 $self->cache($name)->remove($url);
107 }
108 0 ▶sub cache {
Subroutine cache: not called CC=3 SCAR=24.8
109 0 my $self= shift;
110 0 ▶ my $name= shift || $self->param('cache_name');
Condition0%0/3
shift() || $self->param('cache_name')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->param('cache_name')
shift() $self->param('cache_name')
Truth table0%0/3
A || B
A= shift()B= $self->param('cache_name')
ABresult
1X1
011
000
111 0 ▶ $self->{cache}{$name} ||= $self->e->model($name);
Condition0%0/3
$self->{"cache"}{$name} ||= $self->e->model($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"cache"}{$name} ||= $self->e->model($name)
$self->{"cache"}{$name} $self->e->model($name)
Truth table0%0/3
A ||= B
A= $self->{"cache"}{$name}B= $self->e->model($name)
ABresult
1X1
011
000
112 }
113 0 ▶sub __get_args {
Subroutine __get_args: not called CC=4 SCAR=30.0
114 0 my $self = shift;
115 0 ▶ my $url = shift || croak q{ I want URL. };
Condition0%0/2
shift() || croak(' I want URL. ')
l!l
00
MC/DC0%0/1
shift() || croak(' I want URL. ')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
116 my %option= (
117 0 %{$self->params},
118 0 ▶ %{ $_[1] ? {@_}: ($_[0] || {}) },
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
Condition0%0/2
$_[0] or {}
l!l
00
MC/DC0%0/1
$_[0] or {}
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
119 );
120 0 ($self, $url, \%option);
121 }
122  
123 2 1;
124  
125 __END__
↑ Summary