script/proxyforurl

filter: Statementstmt 45.1% 28 / 62 Branchbran 15.3% 4 / 26 Conditioncond 28.0% 7 / 25 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 31.2% 5 / 16 Podpod n/a totaltotal 32.1% 46 / 143 CC 19 SCAR 29.7
CC
19
Cov
35%
__ANON__
56.0
__ANON__
30.0
__ANON__
12.0
CRAP 19.5
↑ Summary blib/lib/App/proxyforurl.pm »
1 #!/usr/bin/env perl
2 2 ▶use Mojolicious::Lite -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::Util qw(network_contains);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use NetAddr::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Socket qw(AF_INET AF_INET6 inet_ntop getaddrinfo unpack_sockaddr_in unpack_sockaddr_in6);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 get
9 '/' => {layout => 'pac'},
10 'index';
11  
12 2 get '/pac', [format => [qw(js)]], => 'pac';
13  
14 0 ▶post '/v1/gethostbyname' => sub ($c) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
15 0 ▶ return $c->render(text => 'Host missing.', status => 400) unless my $host = $c->param('host');
Branch: 1 branch
BranchTrueFalseTotal
unless my $host = $c->param("host") 0 0 0
16 0 ▶ return $c->render(text => "Invalid host: $host", status => 400) unless $host =~ /[A-Za-z:\.]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /[A-Za-z:\.]/u 0 0 0
17  
18 0 $c->res->headers->cache_control('max-age=600');
19 0 ▶ return $c->render_later->host_to_ip_p($host)->then(sub ($addr) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
20 0 ▶ return $c->render(text => "No IP found for $host", status => 400) unless $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 0 0
21 0 return $c->render(text => $addr);
22 })->catch(
23 0 ▶ sub ($err, @) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 return $c->render(text => exception_to_text($err), status => 500);
25 }
26 0 );
27 2 };
28  
29 0 ▶post '/v1/is-in-net' => sub ($c) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
30 0 my ($ip, $net, $mask) = map { $c->param($_) } qw(ip net mask);
31 return $c->render(text => 'IP or Net is missing.', status => 400)
32 0 ▶ unless 2 == grep { $_ && $_ =~ /[:\.]/ } $ip, $net;
Branch: 1 branch
BranchTrueFalseTotal
unless 2 == grep({$_ and $_ =~ /[:\.]/u;} $ip, $net) 0 0 0
Condition0%0/3
$_ && $_ =~ /[:\.]/u
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && $_ =~ /[:\.]/u
$_ $_ =~ /[:\.]/u
Truth table0%0/3
A && B
A= $_B= $_ =~ /[:\.]/u
ABresult
0X0
100
111
33 0 ▶ return $c->render(text => 'Mask is invalid or missing.', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $mask and $mask =~ /^\d{1,3}$/u 0 0 0
Condition0%0/3
$mask and $mask =~ /^\d{1,3}$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$mask and $mask =~ /^\d{1,3}$/u
$mask $mask =~ /^\d{1,3}$/u
Truth table0%0/3
A and B
A= $maskB= $mask =~ /^\d{1,3}$/u
ABresult
0X0
100
111
34 unless $mask and $mask =~ /^\d{1,3}$/;
35  
36 0 $c->res->headers->cache_control('max-age=600');
37 0 ▶ return $c->render_later->host_to_ip_p($ip)->then(sub ($resolved) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
38 0 ▶ $c->render(text => $resolved && network_contains("$net/$mask", $resolved) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$resolved && network_contains("$net/$mask", $resolved) ? : 0 0 0
Condition0%0/3
$resolved and network_contains("$net/$mask", $resolved)
!ll&&!rl&&r
000
MC/DC0%0/2
$resolved and network_contains("$net/$mask", $resolved)
$resolved network_contains("$net/$mask", $resolved)
Truth table0%0/3
A and B
A= $resolvedB= network_contains("$net/$mask", $resolved)
ABresult
0X0
100
111
39 })->catch(
40 0 ▶ sub ($err, @) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 $c->render(text => exception_to_text($err), status => 500);
42 }
43 0 );
44 2 };
45  
46 2 get '/v1/template' => {template => 'template'};
47  
48 0 ▶helper host_to_ip_p => sub ($c, $host) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 ▶ return Mojo::IOLoop->subprocess->run_p(sub (@) {
Subroutine __ANON__: not called CC=7 SCAR=40.3
50 0 ▶ local $SIG{ALRM} = sub { die 'Timeout!' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 alarm 2;
52 0 my ($err, @info) = getaddrinfo $host;
53 0 ▶ return undef if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
54  
55 0 @info = grep { $_->{family} & (AF_INET6 | AF_INET) } @info;
56 0 for my $item (@info) {
57 0 ▶ $item->{pri} = $item->{family} == AF_INET6 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$item->{'family'} == 10 ? : 0 0 0
58 @$item{qw(port ip)}
59 = $item->{family} == AF_INET6
60 ? unpack_sockaddr_in6($item->{addr})
61 0 ▶ : unpack_sockaddr_in($item->{addr});
Branch: 1 branch
BranchTrueFalseTotal
$item->{'family'} == 10 ? : 0 0 0
62 0 $item->{ip} = inet_ntop @$item{qw(family ip)};
63 }
64  
65 # IPv4 before IPv6
66 0 @info = sort { $b->{pri} <=> $a->{pri} } @info;
67  
68 0 ▶ return @info && $info[0]{ip} || undef;
Condition0%0/5
@info && $info[0]{'ip'} || undef
l!l
00
@info and $info[0]{"ip"}
!ll&&!rl&&r
000
MC/DC0%0/2
@info && $info[0]{'ip'} || undef
@info $info[0]{"ip"}
Truth table0%0/3
A and B
A= @infoB= $info[0]{"ip"}
ABresult
111
0X0
100
69 })->catch(
70 0 ▶ sub ($err, @) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
71 0 ▶ return $err =~ m!Timeout! ? undef : Mojo::Promise->reject($err);
Branch: 1 branch
BranchTrueFalseTotal
$err =~ /Timeout/u ? : 0 0 0
72 }
73 0 );
74 2 };
75  
76 2 ▶if (my $env_base = $ENV{PROXYFORURL_X_REQUEST_BASE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env_base = $ENV{"PROXYFORURL_X_REQUEST_BASE"}) 1 1 2
77 1 ▶ $env_base = '' unless $env_base =~ m!^http!;
Branch: 1 branch
BranchTrueFalseTotal
unless $env_base =~ /^http/u 0 1 1
78 1 ▶ hook before_dispatch => sub ($c) {
Subroutine __ANON__: called CC=3 SCAR=13.1
79 1 ▶ return unless my $base = $env_base || $c->req->headers->header('X-Request-Base');
Branch: 1 branch
BranchTrueFalseTotal
unless my $base = $env_base || $c->req->headers->header("X-Request-Base") 0 1 1
Condition33%1/3
$env_base || $c->req->headers->header('X-Request-Base')
l!l&&r!l&&!r
100
MC/DC0%0/2
$env_base || $c->req->headers->header('X-Request-Base')
$env_base $c->req->headers->header('X-Request-Base')
Truth table33%1/3
A || B
A= $env_baseB= $c->req->headers->header('X-Request-Base')
ABresult
1X1
011
000
80 1 $c->req->url->base(Mojo::URL->new($base));
81 1 };
82 }
83  
84 2 ▶app->renderer->paths([$ENV{PROXYFORURL_TEMPLATES} || 'templates']);
Condition50%1/2
$ENV{'PROXYFORURL_TEMPLATES'} || 'templates'
l!l
02
MC/DC0%0/1
$ENV{'PROXYFORURL_TEMPLATES'} || 'templates'
$ENV{'PROXYFORURL_TEMPLATES'}
Truth table50%1/2
A
A= $ENV{'PROXYFORURL_TEMPLATES'}
Aresult
11
00
85 2 ▶app->static->paths([$ENV{PROXYFORURL_PUBLIC} || 'public']);
Condition50%1/2
$ENV{'PROXYFORURL_PUBLIC'} || 'public'
l!l
02
MC/DC0%0/1
$ENV{'PROXYFORURL_PUBLIC'} || 'public'
$ENV{'PROXYFORURL_PUBLIC'}
Truth table50%1/2
A
A= $ENV{'PROXYFORURL_PUBLIC'}
Aresult
11
00
86 2 ▶app->config(brand_name => $ENV{PROXYFORURL_BRAND_NAME} || 'ProxyForURL');
Condition100%2/2
$ENV{'PROXYFORURL_BRAND_NAME'} || 'ProxyForURL'
l!l
11
MC/DC100%1/1
$ENV{'PROXYFORURL_BRAND_NAME'} || 'ProxyForURL'
$ENV{'PROXYFORURL_BRAND_NAME'}
Truth table100%2/2
A
A= $ENV{'PROXYFORURL_BRAND_NAME'}
Aresult
11
00
87 2 ▶app->config(brand_url => $ENV{PROXYFORURL_BRAND_URL} || '/');
Condition100%2/2
$ENV{'PROXYFORURL_BRAND_URL'} || '/'
l!l
11
MC/DC100%1/1
$ENV{'PROXYFORURL_BRAND_URL'} || '/'
$ENV{'PROXYFORURL_BRAND_URL'}
Truth table100%2/2
A
A= $ENV{'PROXYFORURL_BRAND_URL'}
Aresult
11
00
88 2 app->start;
89  
90 0 ▶sub exception_to_text ($err) {
Subroutine exception_to_text: not called CC=1 SCAR=6.9
91 0 $err =~ s!\sat\s\S+.*?line.*!!s;
92 0 return $err;
93 }
94  
95 __DATA__
↑ Summary blib/lib/App/proxyforurl.pm »