AnyPAN/ReverseProxy.pm

filter: Statementstmt 41.1% 49 / 119 Branchbran 0.0% 0 / 18 Conditioncond 9.0% 1 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 57.6% 15 / 26 Podpod 50.0% 3 / 6 totaltotal 36.3% 68 / 187 CC 14 SCAR 23.6
CC
14
Cov
34%
call
30.0
proxt_to_storage_or_sources
30.0
new
12.0
CRAP 10.5
« AnyPAN/CLI.pm ↑ Summary AnyPAN/Agent.pm »
1 package AnyPAN::ReverseProxy;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use parent qw/Plack::Component/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Plack::Util::Accessor qw/storage logger source_cache/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use AnyPAN;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use AnyPAN::Merger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use AnyPAN::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use AnyPAN::SourceCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use AnyPAN::RetryPolicy::NoRetry;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use AnyPAN::PackageInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use AnyPAN::Logger::Stderr;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶our $DEFAULT_LOGGER = AnyPAN::Logger::Stderr->new(level => $ENV{ANYPAN_REVERSE_PROXY_LOG_LEVEL} || 'warn');
Condition50%1/2
$ENV{'ANYPAN_REVERSE_PROXY_LOG_LEVEL'} || 'warn'
l!l
01
MC/DC0%0/1
$ENV{'ANYPAN_REVERSE_PROXY_LOG_LEVEL'} || 'warn'
$ENV{'ANYPAN_REVERSE_PROXY_LOG_LEVEL'}
Truth table50%1/2
A
A= $ENV{'ANYPAN_REVERSE_PROXY_LOG_LEVEL'}
Aresult
11
00
23 1 our $DEFAULT_REQUEST_TIMEOUT = 10;
24 1 our $DEFAULT_RETRY_POLICY = AnyPAN::RetryPolicy::NoRetry->instance();
25  
26 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
27 0 my $class = shift;
28 0 my $self = $class->SUPER::new(@_);
29 0 $self->{sources} = [];
30  
31 # set default
32 0 ▶ $self->{logger} ||= $DEFAULT_LOGGER;
Condition0%0/3
$self->{"logger"} ||= $AnyPAN::ReverseProxy::DEFAULT_LOGGER
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"logger"} ||= $AnyPAN::ReverseProxy::DEFAULT_LOGGER
$self->{"logger"} $AnyPAN::ReverseProxy::DEFAULT_LOGGER
Truth table0%0/3
A ||= B
A= $self->{"logger"}B= $AnyPAN::ReverseProxy::DEFAULT_LOGGER
ABresult
1X1
011
000
33 0 ▶ $self->{source_cache} ||= _get_default_source_cache($self->{logger});
Condition0%0/3
$self->{"source_cache"} ||= _get_default_source_cache($self->{"logger"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"source_cache"} ||= _get_default_source_cache($self->{"logger"})
$self->{"source_cache"} _get_default_source_cache($self->{"logger"})
Truth table0%0/3
A ||= B
A= $self->{"source_cache"}B= _get_default_source_cache($self->{"logger"})
ABresult
1X1
011
000
34  
35 0 return $self;
36 }
37  
38 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=3 SCAR=24.8
Pod documented
39 0 my $self = shift;
40 0 ▶ unless ($self->storage) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->storage) 0 0 0
41 0 die "storage is required";
42 }
43  
44 0 ▶ unless (@{ $self->{sources} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"sources"};}) 0 0 0
45 0 die "sources are required";
46 }
47 }
48  
49 0 ▶sub add_source {
Subroutine add_source: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my ($self, $source_url) = @_;
51 0 my $source = AnyPAN::Source->new($source_url);
52 0 $self->logger->debug("add @{[ $source->name ]} as source");
53 0 push @{ $self->{sources} } => $source;
54 }
55  
56 0 ▶sub call {
Subroutine call: not called CC=5 SCAR=34.0
Pod documented
57 0 my ($self, $env) = @_;
58 0 my $req = Plack::Request->new($env);
59  
60 # GET/HEAD request only
61 0 ▶ unless ($req->method eq 'GET' || $req->method eq 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->method eq "GET" or $req->method eq "HEAD") 0 0 0
Condition0%0/3
$req->method eq "GET" or $req->method eq "HEAD"
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->method eq "GET" or $req->method eq "HEAD"
$req->method eq "GET" $req->method eq "HEAD"
Truth table0%0/3
A or B
A= $req->method eq "GET"B= $req->method eq "HEAD"
ABresult
1X1
011
000
62 0 return $self->_res_405()->finalize();
63 }
64  
65 # path routing
66 0 ▶ if ($req->path =~ m!^/authors/id/([A-Z])/(\1[A-Z])/\2[-A-Z0-9]*/.+$!o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->path =~ m[^/authors/id/([A-Z])/(\1[A-Z])/\2[-A-Z0-9]*/.+$]o) { } 0 0 0
elsif ($req->path eq '/modules/02packages.details.txt.gz') { } 0 0 0
67 0 return $self->proxt_to_storage_or_sources($req)->finalize();
68 } elsif ($req->path eq '/modules/02packages.details.txt.gz') {
69 0 return $self->proxy_to_storage($req)->finalize();
70 }
71  
72 0 return $self->_res_404()->finalize();
73 }
74  
75 0 ▶sub proxt_to_storage_or_sources {
Subroutine proxt_to_storage_or_sources: not called CC=5 SCAR=34.0
Pod undocumented
76 0 my ($self, $req) = @_;
77  
78 # try proxy to storage
79 0 my $res = $self->proxy_to_storage($req);
80 0 ▶ return $res if $res->status == 200;
Branch: 1 branch
BranchTrueFalseTotal
if $res->status == 200 0 0 0
81  
82 # remove "/authors/id/" (e.g. D/DU/DUMMY/Foo.tar.gz)
83 0 my $path = substr $req->path, length "/authors/id/";
84 0 my $package_info = AnyPAN::PackageInfo->new(path => $path);
85 0 my $content_type = Plack::MIME->mime_type($path);
86 0 for my $source (@{ $self->{sources} }) {
87 # fetch from source
88 0 my $package_path = eval {
89 0 $self->source_cache->get_or_fetch_package($source, $package_info)
90 };
91 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
92 0 ▶ if (AnyPAN::Agent::Exception::NotFound->caught($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("AnyPAN::Agent::Exception::NotFound"->caught($e)) 0 0 0
93 0 $self->logger->debug("skip package $path on @{[ $source->name ]}");
94 0 next; # skip it
95 }
96 0 $self->logger->error("failed to fetch package $path on @{[ $source->name ]}");
97 0 die $e;
98 }
99  
100 0 $self->logger->debug("found @{[ $req->path ]} from @{[ $source->name ]}");
101  
102 # save to storage
103 0 my $save_key = $source->package_path($package_info->canonicalized_path);
104 0 $self->storage->copy($package_path, $save_key);
105  
106 # create response
107 0 my $fh = $package_path->openr_raw();
108 0 my $res = Plack::Response->new(200, [
109 'Content-Type' => $content_type,
110 'Content-Length' => $package_path->stat->size,
111 'Cache-Control' => 'private',
112 ], $fh);
113 0 return $res;
114 }
115  
116 0 return $self->_res_404();
117 }
118  
119 0 ▶sub proxy_to_storage {
Subroutine proxy_to_storage: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my ($self, $req) = @_;
121 0 my $storage_key = substr $req->path, 1; # remove first slash
122 0 my $content_type = Plack::MIME->mime_type($storage_key);
123  
124 # check from storage
125 0 my $storage_path = $self->storage->fetch($storage_key);
126 0 ▶ return $self->_res_404() unless $storage_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $storage_path 0 0 0
127  
128 # create response
129 0 $self->logger->debug("found $storage_key from storage");
130 0 my $fh = $storage_path->openr_raw();
131 0 my $res = Plack::Response->new(200, [
132 'Content-Type' => $content_type,
133 'Content-Length' => $storage_path->stat->size,
134 'Cache-Control' => 'private',
135 ], $fh);
136 0 return $res;
137 }
138  
139 0 ▶sub _res_404 { shift->_res_simple(404, 'Not Found') }
Subroutine _res_404: not called CC=1 SCAR=6.9
140 0 ▶sub _res_405 { shift->_res_simple(405, 'Method Not Allowed') }
Subroutine _res_405: not called CC=1 SCAR=6.9
141  
142 0 ▶sub _res_simple {
Subroutine _res_simple: not called CC=1 SCAR=6.9
143 0 my ($self, $status, $content) = @_;
144 0 return Plack::Response->new($status, [
145 'Content-Type' => 'text/plain',
146 'Content-Length' => length $content,
147 'Cache-Control' => 'no-cache,no-store',
148 ], $content);
149 }
150  
151 0 ▶sub _get_default_source_cache {
Subroutine _get_default_source_cache: not called CC=1 SCAR=6.9
152 0 my $logger = shift;
153 0 return AnyPAN::SourceCache->new(
154 cache_dir => $AnyPAN::Merger::DEFAULT_SOURCE_CACHE_DIR,
155 index_cache_timeout => $AnyPAN::Merger::DEFAULT_SOURCE_INDEX_CACHE_TIMEOUT,
156 agent => _get_default_agent($logger),
157 logger => $logger,
158 );
159 }
160  
161 0 ▶sub _get_default_agent {
Subroutine _get_default_agent: not called CC=1 SCAR=6.9
162 0 my $logger = shift;
163 0 return AnyPAN::Agent->new(
164 agent => __PACKAGE__."/$AnyPAN::VERSION",
165 timeout => $DEFAULT_REQUEST_TIMEOUT,
166 logger => $logger,
167 retry_policy => $DEFAULT_RETRY_POLICY,
168 );
169 }
170  
171 1 1;
172 __END__
« AnyPAN/CLI.pm ↑ Summary AnyPAN/Agent.pm »