blib/lib/PAGI/App/GoldenCage.pm

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