blib/lib/PAGI/App/GoldenCage.pm

filter: Statementstmt 98.3% 60 / 61 Branchbran 70.0% 7 / 10 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 20 / 20 Podpod 87.5% 7 / 8 totaltotal 88.5% 100 / 113 CC 10 SCAR 11.3
CC
10
Cov
90%
call
7.1
new
5.4
BEGIN
1.0
CRAP 3.1
↑ Summary
1 package PAGI::App::GoldenCage;
2 1 $PAGI::App::GoldenCage::VERSION = '0.003';
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 1 ▶use HTML::Entities qw(encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use PAGI::Utils qw(handle_lifespan);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Hell, rationality itself - the exalted Human ability to reason - hadn’t
14 # evolved in the pursuit of truth but simply to win arguments, to gain control:
15 # to bend others, by means logical or sophistic, to your will.
16 # -- Peter Watts, Echopraxia
17  
18 1 ▶use constant FOUNTAIN_URL => 'https://RNSAFFN.com/poison2/';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant PAGE_HTML => <<HTML =~ s{[\t\v]}{}gr;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 <html>
21 <head>
22 <meta charset="UTF-8">
23 <title>GC code preview</title>
24 </head>
25 <body>
26 <pre><code>%s</code></pre>
27 <p><a href="%s">Next snippet</a></p>
28 </body>
29 </html>
30 HTML
31  
32 sub new
33 1 ▶{
Subroutine new: called CC=4 SCAR=16.9
Pod documented
34 1 my ($class, %args) = @_;
35  
36 return bless {
37 ua => $args{ua} // Net::Async::HTTP->new,
38 fountain_url => $args{fountain_url} // FOUNTAIN_URL,
39 base_url => $args{base_url} // '',
40 logger => $args{logger},
41 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
42 }
43  
44 sub ua
45 3 ▶{
Subroutine ua: called CC=1 SCAR=0.0
Pod documented
46 3 return $_[0]->{ua};
47 }
48  
49 sub fountain_url
50 2 ▶{
Subroutine fountain_url: called CC=1 SCAR=0.0
Pod documented
51 2 return $_[0]->{fountain_url};
52 }
53  
54 sub base_url
55 3 ▶{
Subroutine base_url: called CC=1 SCAR=0.0
Pod documented
56 3 return $_[0]->{base_url};
57 }
58  
59 sub logger
60 2 ▶{
Subroutine logger: called CC=1 SCAR=0.0
Pod documented
61 2 return $_[0]->{logger};
62 }
63  
64 sub generate_link
65 3 ▶{
Subroutine generate_link: called CC=1 SCAR=0.0
Pod documented
66 3 my ($self) = @_;
67  
68 3 return $self->base_url . '/' . uuid4;
69 }
70  
71 async sub call
72 3 ▶{
Subroutine call: called CC=7 SCAR=19.6
Pod undocumented
73 3 my ($self, $scope, $receive, $send) = @_;
74  
75 3 ▶ if ($scope->{type} eq 'lifespan') {
Branch: 2 branches
BranchTrueFalseTotal
if ($scope->{'type'} eq 'lifespan') { } 1 2 3
elsif ($scope->{'type'} ne 'http') { } 0 2 2
76 return await handle_lifespan(
77 $scope, $receive, $send,
78 1 ▶ startup => async sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 1 my $loop = IO::Async::Loop->new;
80 1 $loop->add($self->ua);
81 1 },
82 );
83 }
84 elsif ($scope->{type} ne 'http') {
85 0 die "Unsupported scope type: $scope->{type}";
86 }
87  
88 2 my $response = await $self->ua->do_request(uri => $self->fountain_url);
89 2 ▶ die sprintf 'Error fetching response from the fountain: [%s] %s', (
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 2 2
90 $response->code,
91 $response->content
92 ) unless $response->is_success;
93  
94 2 my $poisoned = $response->content;
95 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
96 1 $poisoned = gunzip $poisoned;
97 }
98  
99 2 my $html = sprintf PAGE_HTML, encode_entities($poisoned, '<>&"'), $self->generate_link;
100  
101 2 await $send->(
102 {
103 type => 'http.response.start',
104 status => 200,
105 headers => [
106 ['content-type', 'text/html'],
107 ['content-length', length $html],
108 ],
109 }
110 );
111  
112 2 await $send->({type => 'http.response.body', body => $html, more => 0});
113  
114 2 ▶ if (my $logger = $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->logger) 2 0 2
115 2 $logger->(length $poisoned);
116 }
117 1 }
118  
119 sub to_app
120 1 ▶{
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
121 1 my ($self) = @_;
122  
123 1 ▶ return sub { $self->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
124 }
125  
126 1 1;
127  
128 __END__
↑ Summary