blib/lib/PAGI/App/GoldenCage.pm

filter: Statementstmt 100% 52 / 52 Branchbran 62.5% 5 / 8 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 17 / 17 Podpod 87.5% 7 / 8 totaltotal 87.8% 87 / 99 CC 9 SCAR 10.7
CC
9
Cov
90%
call
6.1
new
5.4
BEGIN
1.0
CRAP 2.9
↑ Summary
1 package PAGI::App::GoldenCage;
2 1 $PAGI::App::GoldenCage::VERSION = '0.001';
3 1 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Gzip::Faster;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use UUID qw(uuid4);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Hell, rationality itself - the exalted Human ability to reason - hadn’t
12 # evolved in the pursuit of truth but simply to win arguments, to gain control:
13 # to bend others, by means logical or sophistic, to your will.
14 # -- Peter Watts, Echopraxia
15  
16 1 ▶use constant FOUNTAIN_URL => 'https://RNSAFFN.com/poison2/';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant PAGE_HTML => <<HTML =~ s{[\t\v]}{}gr;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 <html>
19 <head>
20 <title>GC code preview</title>
21 </head>
22 <body>
23 <code>%s</code>
24 <a href="%s">Next Page</a>
25 </body>
26 </html>
27 HTML
28  
29 sub new
30 1 ▶{
Subroutine new: called CC=4 SCAR=16.9
Pod documented
31 1 my ($class, %args) = @_;
32  
33 return bless {
34 ua => $args{ua} // Net::Async::HTTP->new,
35 fountain_url => $args{fountain_url} // FOUNTAIN_URL,
36 base_url => $args{base_url} // '',
37 logger => $args{logger},
38 1 ▶ }, $class;
Condition42%3/7
$args{"ua"} // "Net::Async::HTTP"->new
l!l&&r!l&&!r
100
$args{"fountain_url"} // "https://RNSAFFN.com/poison2/"
l!l
01
$args{"base_url"} // ""
l!l
10
MC/DC0%0/2
$args{"ua"} // "Net::Async::HTTP"->new
$args{"ua"} "Net::Async::HTTP"->new
MC/DC0%0/1
$args{"fountain_url"} // "https://RNSAFFN.com/poison2/"
$args{"fountain_url"}
MC/DC0%0/1
$args{"base_url"} // ""
$args{"base_url"}
Truth table33%1/3
A // B
A= $args{"ua"}B= "Net::Async::HTTP"->new
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{"fountain_url"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"base_url"}
Aresult
11
00
39 }
40  
41 sub ua
42 3 ▶{
Subroutine ua: called CC=1 SCAR=0.0
Pod documented
43 3 return $_[0]->{ua};
44 }
45  
46 sub fountain_url
47 2 ▶{
Subroutine fountain_url: called CC=1 SCAR=0.0
Pod documented
48 2 return $_[0]->{fountain_url};
49 }
50  
51 sub base_url
52 3 ▶{
Subroutine base_url: called CC=1 SCAR=0.0
Pod documented
53 3 return $_[0]->{base_url};
54 }
55  
56 sub logger
57 2 ▶{
Subroutine logger: called CC=1 SCAR=0.0
Pod documented
58 2 return $_[0]->{logger};
59 }
60  
61 sub generate_link
62 3 ▶{
Subroutine generate_link: called CC=1 SCAR=0.0
Pod documented
63 3 my ($self) = @_;
64  
65 3 return $self->base_url . '/' . uuid4;
66 }
67  
68 async sub call
69 2 ▶{
Subroutine call: called CC=6 SCAR=18.0
Pod undocumented
70 2 my ($self, $scope, $receive, $send) = @_;
71  
72 2 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "http" 0 2 2
73  
74 2 my $response = await $self->ua->do_request(uri => $self->fountain_url);
75 2 ▶ die sprintf 'Error fetching response from the fountain: [%s] %s', (
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 2 2
76 $response->code,
77 $response->content
78 ) unless $response->is_success;
79  
80 2 my $poisoned = $response->content;
81 2 ▶ if (lc($response->header('Content-Encoding') // '') eq 'gzip') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($response->header("Content-Encoding") // "") eq "gzip") 1 1 2
Condition100%2/2
$response->header("Content-Encoding") // ""
l!l
11
MC/DC100%1/1
$response->header("Content-Encoding") // ""
$response->header("Content-Encoding")
Truth table100%2/2
A
A= $response->header("Content-Encoding")
Aresult
11
00
82 1 $poisoned = gunzip $poisoned;
83 }
84  
85 2 my $html = sprintf PAGE_HTML, $poisoned, $self->generate_link;
86  
87 2 await $send->(
88 {
89 type => 'http.response.start',
90 status => 200,
91 headers => [
92 ['content-type', 'text/html'],
93 ['content-length', length $html],
94 ],
95 }
96 );
97  
98 2 await $send->({type => 'http.response.body', body => $html, more => 0});
99  
100 2 ▶ if (my $logger = $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->logger) 2 0 2
101 2 $logger->(length $poisoned);
102 }
103 1 }
104  
105 sub to_app
106 1 ▶{
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
107 1 my ($self) = @_;
108  
109 1 my $loop = IO::Async::Loop->new;
110 1 $loop->add($self->ua);
111  
112 1 ▶ return sub { $self->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
113 }
114  
115 1 1;
116  
117 __END__
↑ Summary