| 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"} ||= {}
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"}
| |||||||||||||||||||||
| 16 | 0 | ▶ | $conf->{content_type} ||= 'text/html'; | ||||||||||||||||||
|
Condition0%0/2
$conf->{"content_type"} ||= "text/html"
MC/DC0%0/1
$conf->{"content_type"} ||= "text/html"
$conf->{"content_type"}
Truth table0%0/2
A
A= $conf->{"content_type"}
| |||||||||||||||||||||
| 17 | 0 | ▶ | $conf->{content_type_error} ||= 'text/html'; | ||||||||||||||||||
|
Condition0%0/2
$conf->{"content_type_error"} ||= "text/html"
MC/DC0%0/1
$conf->{"content_type_error"} ||= "text/html"
$conf->{"content_type_error"}
Truth table0%0/2
A
A= $conf->{"content_type_error"}
| |||||||||||||||||||||
| 18 | 0 | ▶ | $conf->{cache_name} || die q{ I want setup 'cache_name'. }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 0 | ▶ | $conf->{cache_expires} ||= undef; | ||||||||||||||||||
|
Condition0%0/2
$conf->{"cache_expires"} ||= undef
MC/DC0%0/1
$conf->{"cache_expires"} ||= undef
$conf->{"cache_expires"}
Truth table0%0/2
A
A= $conf->{"cache_expires"}
| |||||||||||||||||||||
| 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' ])
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'}
| |||||||||||||||||||||
| 21 | my $regex = join '|', | ||||||||||||||||||||
| 22 | 0 | ▶ | map{quotemeta}(ref($allows) eq 'ARRAY' ? @$allows: $allows); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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)
MC/DC0%0/1
$self->e->request->referer || (return 1)
$self->e->request->referer
Truth table0%0/2
A
A= $self->e->request->referer
| |||||||||||||||||||||
| 28 | 0 | ▶ | $referer=~m{^https?\://(?:$regex)} ? 1: 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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"})
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"})
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||
| 55 | 0 | my %attr; | |||||||||||||||||||
| 56 | 0 | ▶ | my $method= uc $option->{request_method} || 'GET'; | ||||||||||||||||||
|
Condition0%0/2
uc $option->{'request_method'} || 'GET'
MC/DC0%0/1
uc $option->{'request_method'} || 'GET'
uc $option->{'request_method'}
Truth table0%0/2
A
A= uc $option->{'request_method'}
| |||||||||||||||||||||
| 57 | 0 | ▶ | if (my $res= $self->e->ua->request( $method => $url )) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $attr{is_success}= 1; | |||||||||||||||||||
| 60 | 0 | ▶ | if (my $status= $res->status_line) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | ▶ | $attr{status}= $status if $status!~/^200/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 0 | ▶ | my @content_type= $res->header('content_type') || ""; | ||||||||||||||||||
|
Condition0%0/2
$res->header('content_type') || ''
MC/DC0%0/1
$res->header('content_type') || ''
$res->header('content_type')
Truth table0%0/2
A
A= $res->header('content_type')
| |||||||||||||||||||||
| 64 | $attr{content_type}= $content_type[0] | ||||||||||||||||||||
| 65 | 0 | ▶ | || $option->{content_type}; | ||||||||||||||||||
|
Condition0%0/3
$content_type[0] || $option->{'content_type'}
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'}
| |||||||||||||||||||||
| 66 | 0 | ▶ | $attr{content}= $res->content || ""; | ||||||||||||||||||
|
Condition0%0/2
$res->content || ''
MC/DC0%0/1
$res->content || ''
$res->content
Truth table0%0/2
A
A= $res->content
| |||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||
| 68 | 0 | ▶ | $attr{status}= $res->status_line || '403 Forbidden'; | ||||||||||||||||||
|
Condition0%0/2
$res->status_line || '403 Forbidden'
MC/DC0%0/1
$res->status_line || '403 Forbidden'
$res->status_line
Truth table0%0/2
A
A= $res->status_line
| |||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||
| 76 | 0 | ▶ | $attr{content} ||= ""; | ||||||||||||||||||
|
Condition0%0/2
$attr{"content"} ||= ""
MC/DC0%0/1
$attr{"content"} ||= ""
$attr{"content"}
Truth table0%0/2
A
A= $attr{"content"}
| |||||||||||||||||||||
| 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.'}
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)
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 93 | $response->is_expires($option->{expires}) | ||||||||||||||||||||
| 94 | 0 | ▶ | if $option->{expires}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | $response->last_modified($option->{last_modified}) | ||||||||||||||||||||
| 96 | 0 | ▶ | if $option->{last_modified}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | ▶ | $response->status($cache->{status}) if $cache->{status}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | $response->content_type($cache->{content_type}); | |||||||||||||||||||
| 99 | 0 | ▶ | $cache->{content}= $cache->{error} if $cache->{error}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||
| 111 | 0 | ▶ | $self->{cache}{$name} ||= $self->e->model($name); | ||||||||||||||||||
|
Condition0%0/3
$self->{"cache"}{$name} ||= $self->e->model($name)
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)
| |||||||||||||||||||||
| 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. ')
MC/DC0%0/1
shift() || croak(' I want URL. ')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 116 | my %option= ( | ||||||||||||||||||||
| 117 | 0 | %{$self->params}, | |||||||||||||||||||
| 118 | 0 | ▶ | %{ $_[1] ? {@_}: ($_[0] || {}) }, | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$_[0] or {}
MC/DC0%0/1
$_[0] or {}
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||
| 120 | 0 | ($self, $url, \%option); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 2 | 1; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||