blib/lib/Discord/Client/HTTPInteractions.pm

filter: Statementstmt 97.0% 230 / 237 Branchbran 79.0% 79 / 100 Conditioncond 64.5% 51 / 79 MC/DCmcdc 34.0% 16 / 47 Subroutinesub 93.3% 42 / 45 Podpod 100% 11 / 11 totaltotal 82.6% 429 / 519 CC 84 SCAR 22.4
CC
84
Cov
87%
new
23.5
handle
20.3
_handle_psgi_request
9.3
CRAP 9.4
« blib/lib/Discord/Client/REST/RateLimiter.pm ↑ Summary blib/lib/Discord/Client.pm »
1 package Discord::Client::HTTPInteractions;
2  
3 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Discord::Client::Interaction::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Discord::Client::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Discord::Client::Interactions;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Discord::Client::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 our $VERSION = '0.003';
18  
19 19 ▶sub new {
Subroutine new: called CC=19 SCAR=31.6
Pod documented
20 19 my ( $class, %args ) = @_;
21  
22 # Allocate independent limits for active handlers, cached replies and retained IDs.
23 19 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition66%2/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
1720
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table66%2/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
24 my $self = bless {
25 loop => $loop,
26 8 ▶ clock => $args{clock} // sub { clock_gettime(CLOCK_MONOTONIC) },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 max_body => $args{max_body} // 1024 * 1024,
28 max_replay => $args{max_replay} // 1024,
29 max_seen => $args{max_seen} // 10000,
30 replay_ttl => $args{replay_ttl} // 900,
31 pending => {},
32 replies => {},
33 seen => {},
34 counters => {},
35 on_error => $args{on_error}
36 19 ▶ }, $class;
Condition90%10/11
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
l!l&&r!l&&!r
1810
$args{"max_body"} // 1048576
l!l
118
$args{"max_replay"} // 1024
l!l
217
$args{"max_seen"} // 10000
l!l
415
$args{"replay_ttl"} // 900
l!l
316
MC/DC0%0/2
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
$args{"clock"} sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
MC/DC100%1/1
$args{"max_body"} // 1048576
$args{"max_body"}
MC/DC100%1/1
$args{"max_replay"} // 1024
$args{"max_replay"}
MC/DC100%1/1
$args{"max_seen"} // 10000
$args{"max_seen"}
MC/DC100%1/1
$args{"replay_ttl"} // 900
$args{"replay_ttl"}
Truth table66%2/3
A // B
A= $args{"clock"}B= sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{"max_body"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_replay"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_seen"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"replay_ttl"}
Aresult
11
00
37  
38 # Validate capacity and hooks before creating verification or transport resources.
39 19 for my $key (qw(max_body max_replay max_seen replay_ttl)) {
40 76 ▶ croak "$key must be a positive integer" unless !ref( $self->{$key} ) && $self->{$key} =~ /\A[1-9]\d*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $self->{$key} and $self->{$key} =~ /\A[1-9]\d*\z/u 0 76 76
Condition33%1/3
not ref $self->{$key} and $self->{$key} =~ /\A[1-9]\d*\z/u
!ll&&!rl&&r
0076
MC/DC0%0/2
not ref $self->{$key} and $self->{$key} =~ /\A[1-9]\d*\z/u
not ref $self->{$key} $self->{$key} =~ /\A[1-9]\d*\z/u
Truth table33%1/3
not(A) and B
A= not ref $self->{$key}B= $self->{$key} =~ /\A[1-9]\d*\z/u
ABresult
0X0
100
111
41 }
42 19 for my $key (qw(clock delay on_error)) {
43 57 ▶ croak "$key must be a code reference" if exists( $args{$key} ) && ref( $args{$key} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} and ref $args{$key} ne "CODE" 0 57 57
Condition66%2/3
exists $args{$key} and ref $args{$key} ne "CODE"
!ll&&!rl&&r
3540
MC/DC0%0/2
exists $args{$key} and ref $args{$key} ne "CODE"
exists $args{$key} ref $args{$key} ne "CODE"
Truth table66%2/3
A and B
A= exists $args{$key}B= ref $args{$key} ne "CODE"
ABresult
0X0
100
111
44 }
45  
46 # Verify signatures independently of command dispatch and token-only followup delivery.
47 $self->{verifier} = Discord::Client::Interaction::Verifier->new(
48 public_key => $args{public_key},
49 ( exists( $args{wall_clock} ) ? ( wall_clock => $args{wall_clock} ) : () ),
50 19 ▶ ( exists( $args{max_age} ) ? ( max_age => $args{max_age} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'wall_clock'} ? : 18 1 19
exists $args{'max_age'} ? : 0 19 19
51 );
52 19 ▶ $self->{rest} = $args{rest} // Discord::Client::REST->new( auth => 0, loop => $loop );
Condition33%1/3
$args{"rest"} // "Discord::Client::REST"->new("auth", 0, "loop", $loop)
l!l&&r!l&&!r
0190
MC/DC0%0/2
$args{"rest"} // "Discord::Client::REST"->new("auth", 0, "loop", $loop)
$args{"rest"} "Discord::Client::REST"->new("auth", 0, "loop", $loop)
Truth table33%1/3
A // B
A= $args{"rest"}B= "Discord::Client::REST"->new("auth", 0, "loop", $loop)
ABresult
1X1
011
000
53 19 $self->{own_rest} = !$args{rest};
54 $self->{commands} = $args{commands} // Discord::Client::Commands->new(
55 rest => $self->{rest},
56 loop => $loop,
57 application_id => $args{application_id}
58 19 ▶ );
Condition33%1/3
$args{"commands"} // "Discord::Client::Commands"->new("rest", $self->{"rest"}, "loop", $loop, "application_id", $args{"application_id"})
l!l&&r!l&&!r
0190
MC/DC0%0/2
$args{"commands"} // "Discord::Client::Commands"->new("rest", $self->{"rest"}, "loop", $loop, "application_id", $args{"application_id"})
$args{"commands"} "Discord::Client::Commands"->new("rest", $self->{"rest"}, "loop", $loop, "application_id", $args{"application_id"})
Truth table33%1/3
A // B
A= $args{"commands"}B= "Discord::Client::Commands"->new("rest", $self->{"rest"}, "loop", $loop, "application_id", $args{"application_id"})
ABresult
1X1
011
000
59 19 $self->{own_commands} = !$args{commands};
60  
61 # Dispatcher error callbacks must not keep the HTTP service alive.
62 19 my $weak = $self;
63 19 weaken($weak);
64 $self->{dispatcher} = Discord::Client::Interactions->new(
65 loop => $loop,
66 commands => $self->{commands},
67 rest => $self->{rest},
68 clock => $self->{clock},
69 ( exists( $args{delay} ) ? ( delay => $args{delay} ) : () ),
70 max_seen => $self->{max_seen},
71 seen_ttl => $self->{replay_ttl},
72 max_pending => $args{max_pending} // 512,
73 5 ▶ on_error => sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
74 5 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 5 5
75 5 ++$weak->{counters}{handler_errors};
76 5 ▶ if ( $weak->{on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak->{'on_error'}) { } 5 0 5
77 5 my $ok = eval {
78 5 $weak->{on_error}->(@_);
79 5 1;
80 };
81 5 ▶ warn "HTTP interaction error handler failed\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 5 5
82 }
83 else {
84 0 warn $_[0]->message . "\n";
85 }
86 }
87 19 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'delay'} ? : 18 1 19
Condition100%2/2
$args{"max_pending"} // 512
l!l
118
MC/DC100%1/1
$args{"max_pending"} // 512
$args{"max_pending"}
Truth table100%2/2
A
A= $args{"max_pending"}
Aresult
11
00
88 19 return $self;
89 }
90  
91 0 ▶sub loop { $_[0]->{loop} }
Subroutine loop: not called CC=1 SCAR=6.9
Pod documented
92 0 ▶sub commands { $_[0]->{commands} }
Subroutine commands: not called CC=1 SCAR=6.9
Pod documented
93 2 ▶sub rest { $_[0]->{rest} }
Subroutine rest: called CC=1 SCAR=0.0
Pod documented
94 3 ▶sub dispatcher { $_[0]->{dispatcher} }
Subroutine dispatcher: called CC=1 SCAR=0.0
Pod documented
95  
96 15 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
97 15 my ( $self, @args ) = @_;
98 15 $self->{commands}->define(@args);
99 15 return $self;
100 }
101  
102 0 ▶sub component {
Subroutine component: not called CC=1 SCAR=6.9
Pod documented
103 0 my ( $self, @args ) = @_;
104 0 $self->{commands}->component(@args);
105 0 return $self;
106 }
107  
108 1075 ▶sub _response {
Subroutine _response: called CC=1 SCAR=0.0
109 1075 my ( $self, $status, $data ) = @_;
110  
111 # Return PSGI bytes and a byte count, not a character count.
112 1075 my $body = JSON::PP->new->utf8->encode($data);
113 1075 return [ $status, [ 'Content-Type' => 'application/json', 'Content-Length' => length($body) ], [$body] ];
114 }
115  
116 23 ▶sub _done {
Subroutine _done: called CC=1 SCAR=0.0
117 23 my ( $self, $status, $data ) = @_;
118 23 return $self->{loop}->new_future->done( $self->_response( $status, $data ) );
119 }
120  
121 1055 ▶sub _copy_response {
Subroutine _copy_response: called CC=1 SCAR=0.0
122 1055 my ($response) = @_;
123 1055 my ( $status, $headers, $body ) = @$response;
124 1055 return [ $status, [@$headers], [@$body] ];
125 }
126  
127 1052 ▶sub _waiter {
Subroutine _waiter: called CC=1 SCAR=0.0
128 1052 my ( $self, $source ) = @_;
129  
130 # Each HTTP caller gets its own response copy and cannot cancel shared handler work.
131 1052 my $future = $self->{loop}->new_future;
132 $source->on_ready(
133 1052 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
134 1052 ▶ return if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 1 1051 1052
135 1051 ▶ if ( $source->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->is_done) { } 1051 0 1051
136 1051 my ($response) = $source->result;
137 1051 $future->done( _copy_response($response) );
138 }
139 else {
140 0 $future->done( $self->_response( 503, { error => 'Interaction request cancelled' } ) );
141 }
142 }
143 1052 );
144 1052 return $future;
145 }
146  
147 1079 ▶sub handle {
Subroutine handle: called CC=18 SCAR=30.1
Pod documented
148 1079 my ( $self, $headers, $body, %args ) = @_;
149  
150 # Record arrival before verification so all admission time counts toward the deadline.
151 1079 ▶ my $received_at = $args{received_at} // $self->{clock}->();
Condition66%2/3
$args{"received_at"} // $self->{"clock"}->()
l!l&&r!l&&!r
810710
MC/DC0%0/2
$args{"received_at"} // $self->{"clock"}->()
$args{"received_at"} $self->{"clock"}->()
Truth table66%2/3
A // B
A= $args{"received_at"}B= $self->{"clock"}->()
ABresult
1X1
011
000
152 1079 ++$self->{counters}{requests};
153 1079 ▶ return $self->_done( 503, { error => 'Interaction service closed' } ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 1078 1079
154 1078 ▶ return $self->_done( 400, { error => 'Invalid request body' } ) unless defined($body) && !ref($body);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and not ref $body 0 1078 1078
Condition33%1/3
defined $body and not ref $body
!ll&&!rl&&r
001078
MC/DC0%0/2
defined $body and not ref $body
defined $body not ref $body
Truth table33%1/3
A and not(B)
A= defined $bodyB= not ref $body
ABresult
0X0
100
111
155 1078 ▶ return $self->_done( 413, { error => 'Request body too large' } ) if length($body) > $self->{max_body};
Branch: 1 branch
BranchTrueFalseTotal
if length $body > $self->{"max_body"} 1 1077 1078
156 1077 ▶ return $self->_done( 401, { error => 'Invalid request signature' } ) unless ref($headers) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers eq "HASH" 0 1077 1077
157 1077 my %normalized_headers = map { lc($_) => $headers->{$_} } keys %$headers;
158 1077 my $signature = $normalized_headers{'x-signature-ed25519'};
159 1077 my $timestamp = $normalized_headers{'x-signature-timestamp'};
160  
161 # Verify the exact signed octets before trusting or parsing JSON fields.
162 1077 ▶ unless ( $self->{verifier}->verify( $signature, $timestamp, $body ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"verifier"}->verify($signature, $timestamp, $body)) 10 1067 1077
163 10 ++$self->{counters}{rejected_signatures};
164 10 return $self->_done( 401, { error => 'Invalid request signature' } );
165 }
166  
167 # PING is a signature check and needs neither a command context nor replay storage.
168 1067 my $payload = eval { JSON::PP->new->utf8->decode($body) };
169 return $self->_done( 400, { error => 'Invalid interaction JSON' } )
170 if $@
171 || ref($payload) ne 'HASH'
172 || !defined( $payload->{type} )
173 1067 ▶ || ref( $payload->{type} );
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $payload ne "HASH" or not defined $payload->{"type"} or ref $payload->{"type"} 1 1066 1067
Condition66%6/9
$@ or ref $payload ne "HASH" or not defined $payload->{"type"} or ref $payload->{"type"}
l!l&&r!l&&!r
101066
$@ or ref $payload ne "HASH" or not defined $payload->{"type"}
l!l&&r!l&&!r
101066
$@ or ref $payload ne "HASH"
l!l&&r!l&&!r
101066
MC/DC25%1/4
$@ or ref $payload ne "HASH" or not defined $payload->{"type"} or ref $payload->{"type"}
$@ ref $payload ne "HASH" not defined $payload->{"type"} ref $payload->{"type"}
Truth table40%2/5
A or B or not(C) or D
A= $@B= ref $payload ne "HASH"C= not defined $payload->{"type"}D= ref $payload->{"type"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
174 1066 ▶ return $self->_done( 200, { type => 1 } ) if $payload->{type} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $payload->{"type"} eq 1 5 1061 1066
175  
176 1061 my $now = $self->{clock}->();
177 1061 $self->_expire_replay_records($now);
178  
179 # Coalesce running duplicates and replay completed responses without rerunning handlers.
180 1061 my $id = $payload->{id};
181 1061 ▶ return $self->_done( 400, { error => 'Invalid interaction ID' } )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and $id =~ /\A\d+\z/u 0 1061 1061
Condition33%2/6
defined $id and not ref $id and $id =~ /\A\d+\z/u
!ll&&!rl&&r
001061
defined $id and not ref $id
!ll&&!rl&&r
001061
MC/DC0%0/3
defined $id and not ref $id and $id =~ /\A\d+\z/u
defined $id not ref $id $id =~ /\A\d+\z/u
Truth table25%1/4
A and not(B) and C
A= defined $idB= not ref $idC= $id =~ /\A\d+\z/u
ABCresult
0XX0
10X0
1100
1111
182 unless defined($id) && !ref($id) && $id =~ /\A\d+\z/;
183 1061 my $replayed_response = $self->_replay_response($id);
184 1061 ▶ return $replayed_response if $replayed_response;
Branch: 1 branch
BranchTrueFalseTotal
if $replayed_response 9 1052 1061
185  
186 return $self->_done( 503, { error => 'Interaction capacity exceeded' } )
187 1052 ▶ if keys( %{ $self->{seen} } ) >= $self->{max_seen};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"seen"};} >= $self->{"max_seen"} 3 1049 1052
188 1049 return $self->_dispatch_payload( $payload, $received_at, $now );
189 }
190  
191 1061 ▶sub _expire_replay_records {
Subroutine _expire_replay_records: called CC=8 SCAR=20.8
192 1061 my ( $self, $now ) = @_;
193  
194 # No retained ID can expire before the earliest recorded deadline.
195 1061 ▶ return if defined( $self->{next_seen_expiry} ) && $now < $self->{next_seen_expiry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"next_seen_expiry"} and $now < $self->{"next_seen_expiry"} 1038 23 1061
Condition100%3/3
defined $self->{"next_seen_expiry"} and $now < $self->{"next_seen_expiry"}
!ll&&!rl&&r
1491038
MC/DC100%2/2
defined $self->{"next_seen_expiry"} and $now < $self->{"next_seen_expiry"}
defined $self->{"next_seen_expiry"} $now < $self->{"next_seen_expiry"}
Truth table100%3/3
A and B
A= defined $self->{"next_seen_expiry"}B= $now < $self->{"next_seen_expiry"}
ABresult
0X0
100
111
196 23 delete $self->{next_seen_expiry};
197  
198 # Expire completed IDs only; running work must remain protected from replay.
199 23 for my $id ( keys %{ $self->{seen} } ) {
200 14 my $expires = $self->{seen}{$id};
201 14 ▶ if ( !$self->{pending}{$id} && $expires <= $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"pending"}{$id} and $expires <= $now) 6 8 14
Condition100%3/3
not $self->{"pending"}{$id} and $expires <= $now
!ll&&!rl&&r
446
MC/DC100%2/2
not $self->{"pending"}{$id} and $expires <= $now
not $self->{"pending"}{$id} $expires <= $now
Truth table100%3/3
not(A) and B
A= not $self->{"pending"}{$id}B= $expires <= $now
ABresult
0X0
100
111
202 6 delete $self->{seen}{$id};
203 6 delete $self->{replies}{$id};
204 6 next;
205 }
206  
207 # An expired running ID keeps the next request eligible to check again.
208 $self->{next_seen_expiry} = $expires
209 8 ▶ if !defined( $self->{next_seen_expiry} ) || $expires < $self->{next_seen_expiry};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"next_seen_expiry"} or $expires < $self->{"next_seen_expiry"} 6 2 8
Condition66%2/3
not defined $self->{"next_seen_expiry"} or $expires < $self->{"next_seen_expiry"}
l!l&&r!l&&!r
602
MC/DC50%1/2
not defined $self->{"next_seen_expiry"} or $expires < $self->{"next_seen_expiry"}
not defined $self->{"next_seen_expiry"} $expires < $self->{"next_seen_expiry"}
Truth table66%2/3
not(A) or B
A= not defined $self->{"next_seen_expiry"}B= $expires < $self->{"next_seen_expiry"}
ABresult
1X1
011
000
210 }
211 }
212  
213 1061 ▶sub _replay_response {
Subroutine _replay_response: called CC=4 SCAR=13.9
214 1061 my ( $self, $id ) = @_;
215  
216 # Pending work, cached bytes and retained IDs have different replay outcomes.
217 1061 ▶ if ( my $record = $self->{pending}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->{"pending"}{$id}) 3 1058 1061
218 3 ++$self->{counters}{duplicates};
219 3 return $self->_waiter( $record->{response} );
220 }
221 1058 ▶ if ( my $record = $self->{replies}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->{"replies"}{$id}) 4 1054 1058
222 4 ++$self->{counters}{duplicates};
223 4 return $self->{loop}->new_future->done( _copy_response( $record->{response} ) );
224 }
225  
226 # Evicted response bytes do not permit the same side effects to run again.
227 1054 ▶ if ( exists $self->{seen}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"seen"}{$id}) 2 1052 1054
228 2 ++$self->{counters}{duplicates};
229 2 return $self->_done( 409, { error => 'Interaction response no longer cached' } );
230 }
231 1052 return undef;
232 }
233  
234 1049 ▶sub _dispatch_payload {
Subroutine _dispatch_payload: called CC=4 SCAR=13.9
235 1049 my ( $self, $payload, $received_at, $now ) = @_;
236 1049 my $id = $payload->{id};
237 1049 my $response_future = $self->{loop}->new_future;
238 1049 my $record = { response => $response_future };
239  
240 # Reserve the ID before invoking application code, including reentrant calls.
241 1049 $self->{pending}{$id} = $record;
242 1049 $self->{seen}{$id} = $now + $self->{replay_ttl};
243 $self->{next_seen_expiry} = $self->{seen}{$id}
244 1049 ▶ if !defined( $self->{next_seen_expiry} ) || $self->{seen}{$id} < $self->{next_seen_expiry};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"next_seen_expiry"} or $self->{"seen"}{$id} < $self->{"next_seen_expiry"} 17 1032 1049
Condition66%2/3
not defined $self->{"next_seen_expiry"} or $self->{"seen"}{$id} < $self->{"next_seen_expiry"}
l!l&&r!l&&!r
1701032
MC/DC50%1/2
not defined $self->{"next_seen_expiry"} or $self->{"seen"}{$id} < $self->{"next_seen_expiry"}
not defined $self->{"next_seen_expiry"} $self->{"seen"}{$id} < $self->{"next_seen_expiry"}
Truth table66%2/3
not(A) or B
A= not defined $self->{"next_seen_expiry"}B= $self->{"seen"}{$id} < $self->{"next_seen_expiry"}
ABresult
1X1
011
000
245 my $work = $self->{dispatcher}->handle(
246 $payload,
247 received_at => $received_at,
248 1043 ▶ initial_sender => sub { $self->_complete_inline_reply( $response_future, $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
249 1049 );
250 1049 ▶ $record->{work} = $work unless $work->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $work->is_ready 9 1040 1049
251  
252 # Release active ownership after both acknowledgement and application work settle.
253 $work->on_ready(
254 1049 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
255 1049 delete $record->{work};
256 1049 ▶ if ( $work->is_failed && ( $work->failure )[1] eq 'queue_full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work->is_failed and ($work->failure)[1] eq "queue_full") 1 1048 1049
Condition100%3/3
$work->is_failed and ($work->failure)[1] eq "queue_full"
!ll&&!rl&&r
104531
MC/DC100%2/2
$work->is_failed and ($work->failure)[1] eq "queue_full"
$work->is_failed ($work->failure)[1] eq "queue_full"
Truth table100%3/3
A and B
A= $work->is_failedB= ($work->failure)[1] eq "queue_full"
ABresult
0X0
100
111
257  
258 # Admission failed before any handler ran; retries may try again.
259 1 delete $self->{seen}{$id};
260 1 delete $self->{pending}{$id};
261 1 $self->_finish_unanswered_response( $response_future, $work );
262 1 return;
263 }
264 1048 $self->_finish_unanswered_response( $response_future, $work );
265 1048 ▶ $self->_cache_response( $id, $response_future ) unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 1046 2 1048
266 1048 delete $self->{pending}{$id};
267 }
268 1049 );
269 1049 return $self->_waiter($response_future);
270 }
271  
272 1046 ▶sub _cache_response {
Subroutine _cache_response: called CC=2 SCAR=6.9
273 1046 my ( $self, $id, $response_future ) = @_;
274 1046 $self->{replies}{$id} = { response => ( $response_future->result )[0], order => ++$self->{reply_order} };
275 1046 ▶ return if keys( %{ $self->{replies} } ) <= $self->{max_replay};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"replies"};} <= $self->{"max_replay"} 1044 2 1046
276  
277 # Evict response bytes, retaining the ID so side effects cannot run twice.
278 8982 my ($oldest) = sort { $self->{replies}{$a}{order} <=> $self->{replies}{$b}{order} }
279 2 keys %{ $self->{replies} };
280 2 delete $self->{replies}{$oldest};
281 }
282  
283 1043 ▶sub _complete_inline_reply {
Subroutine _complete_inline_reply: called CC=3 SCAR=11.2
284 1043 my ( $self, $response_future, $reply ) = @_;
285  
286 # Accept at most one serializable inline response for the request.
287 1043 ▶ return $self->{loop}->new_future->fail( 'HTTP response is already complete', 'state' )
Branch: 1 branch
BranchTrueFalseTotal
if $response_future->is_ready 0 1043 1043
288 if $response_future->is_ready;
289 1043 my $wire = eval { $self->_response( 200, $reply ) };
290 1043 ▶ return $self->{loop}->new_future->fail( 'Invalid HTTP interaction response', 'request' ) unless $wire;
Branch: 1 branch
BranchTrueFalseTotal
unless $wire 0 1043 1043
291 1043 $response_future->done($wire);
292 1043 return $self->{loop}->new_future->done;
293 }
294  
295 1049 ▶sub _finish_unanswered_response {
Subroutine _finish_unanswered_response: called CC=4 SCAR=13.9
296 1049 my ( $self, $response_future, $work ) = @_;
297 1049 ▶ return if $response_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $response_future->is_ready 1045 4 1049
298  
299 # Map public failure categories to HTTP status without exposing private error details.
300 4 my $status = 500;
301 4 ▶ if ( $work->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work->is_failed) 4 0 4
302 4 my ( undef, $category ) = $work->failure;
303 4 my %status_for = ( deadline => 504, protocol => 400, queue_full => 503 );
304 4 ▶ $status = $status_for{$category} // 500;
Condition100%2/2
$status_for{$category} // 500
l!l
31
MC/DC100%1/1
$status_for{$category} // 500
$status_for{$category}
Truth table100%2/2
A
A= $status_for{$category}
Aresult
11
00
305 }
306 4 $response_future->done( $self->_response( $status, { error => 'Interaction could not be answered' } ) );
307 }
308  
309 2 ▶sub psgi_app {
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod documented
310 2 my ($self) = @_;
311 2 ▶ return sub { $self->_handle_psgi_request( $_[0] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
312 }
313  
314 9 ▶sub _handle_psgi_request {
Subroutine _handle_psgi_request: called CC=9 SCAR=22.3
315 9 my ( $self, $env ) = @_;
316  
317 # Bound the POST body before reading from the host-provided input stream.
318 9 my $received_at = $self->{clock}->();
319 9 ▶ return $self->_response( 405, { error => 'POST required' } ) unless ( $env->{REQUEST_METHOD} // '' ) eq 'POST';
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"REQUEST_METHOD"} // "") eq "POST" 1 8 9
Condition50%1/2
$env->{"REQUEST_METHOD"} // ""
l!l
90
MC/DC0%0/1
$env->{"REQUEST_METHOD"} // ""
$env->{"REQUEST_METHOD"}
Truth table50%1/2
A
A= $env->{"REQUEST_METHOD"}
Aresult
11
00
320 8 my $length = $env->{CONTENT_LENGTH};
321 8 ▶ return $self->_response( 400, { error => 'Content-Length required' } )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length and $length =~ /\A\d+\z/u 0 8 8
Condition33%1/3
defined $length and $length =~ /\A\d+\z/u
!ll&&!rl&&r
008
MC/DC0%0/2
defined $length and $length =~ /\A\d+\z/u
defined $length $length =~ /\A\d+\z/u
Truth table33%1/3
A and B
A= defined $lengthB= $length =~ /\A\d+\z/u
ABresult
0X0
100
111
322 unless defined($length) && $length =~ /\A\d+\z/;
323 8 ▶ return $self->_response( 413, { error => 'Request body too large' } ) if $length > $self->{max_body};
Branch: 1 branch
BranchTrueFalseTotal
if $length > $self->{"max_body"} 0 8 8
324  
325 # Preserve request bytes for signature validation; reject incomplete host input.
326 8 my $body = eval { _read_psgi_body( $env->{'psgi.input'}, $length ) };
327 8 ▶ return $self->_response( 400, { error => 'Incomplete request body' } ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 7 8
328 my $future = $self->handle(
329 {
330 'X-Signature-Ed25519' => $env->{HTTP_X_SIGNATURE_ED25519},
331 'X-Signature-Timestamp' => $env->{HTTP_X_SIGNATURE_TIMESTAMP},
332 },
333 7 $body,
334 received_at => $received_at
335 );
336 7 ▶ return $future->get if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 5 2 7
337  
338 # Only streaming hosts can wait asynchronously for an unanswered interaction.
339 2 ▶ unless ( $env->{'psgi.streaming'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"psgi.streaming"}) 1 1 2
340 1 return $self->_reject_pending_psgi_response($body);
341 }
342 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
343 1 my ($responder) = @_;
344 1 ▶ $future->on_ready( sub { $responder->( $future->get ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
345 1 };
346 }
347  
348 8 ▶sub _read_psgi_body {
Subroutine _read_psgi_body: called CC=4 SCAR=13.9
349 8 my ( $input, $length ) = @_;
350 8 my $body = '';
351  
352 # PSGI input may return short chunks; zero bytes before Content-Length is a failure.
353 8 ▶ while ( length($body) < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body < $length) 9 7 16
354 9 my $read = $input->read( my $chunk, $length - length($body) );
355 9 ▶ die 'Short request body' unless defined($read) && $read > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read and $read > 0 1 8 9
Condition66%2/3
defined $read and $read > 0
!ll&&!rl&&r
018
MC/DC50%1/2
defined $read and $read > 0
defined $read $read > 0
Truth table66%2/3
A and B
A= defined $readB= $read > 0
ABresult
0X0
100
111
356 8 $body .= $chunk;
357 }
358 7 return $body;
359 }
360  
361 1 ▶sub _reject_pending_psgi_response {
Subroutine _reject_pending_psgi_response: called CC=7 SCAR=21.8
362 1 my ( $self, $body ) = @_;
363  
364 # A synchronous host cannot keep an unanswered handler running after returning 503.
365 1 my $payload = eval { JSON::PP->new->utf8->decode($body) };
366 1 ▶ my $record = ref($payload) eq 'HASH' ? $self->{pending}{ $payload->{id} // '' } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $payload eq 'HASH' ? : 1 0 1
Condition50%1/2
$payload->{"id"} // ""
l!l
10
MC/DC0%0/1
$payload->{"id"} // ""
$payload->{"id"}
Truth table50%1/2
A
A= $payload->{"id"}
Aresult
11
00
367 1 ▶ if ($record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record) 1 0 1
368 $record->{response}->done( $self->_response( 503, { error => 'Asynchronous PSGI support required' } ) )
369 1 ▶ unless $record->{response}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"response"}->is_ready 1 0 1
370 1 ▶ $record->{work}->cancel if $record->{work} && !$record->{work}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"work"} and not $record->{"work"}->is_ready 1 0 1
Condition33%1/3
$record->{"work"} and not $record->{"work"}->is_ready
!ll&&!rl&&r
001
MC/DC0%0/2
$record->{"work"} and not $record->{"work"}->is_ready
$record->{"work"} not $record->{"work"}->is_ready
Truth table33%1/3
A and not(B)
A= $record->{"work"}B= not $record->{"work"}->is_ready
ABresult
0X0
100
111
371 }
372 1 return $self->_response( 503, { error => 'Asynchronous PSGI support required' } );
373 }
374  
375 4 ▶sub stats {
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
376 4 my ($self) = @_;
377 return {
378 4 %{ $self->{counters} },
379 4 replay_entries => scalar( keys %{ $self->{replies} } ),
380 4 pending_requests => scalar( keys %{ $self->{pending} } ),
381 4 seen_entries => scalar( keys %{ $self->{seen} } )
382 };
383 }
384  
385 18 ▶sub close {
Subroutine close: called CC=5 SCAR=16.2
Pod documented
386 18 my ($self) = @_;
387  
388 # Answer outstanding HTTP callers before cancelling dispatcher work and owned resources.
389 18 $self->{closed} = 1;
390 18 for my $record ( values %{ $self->{pending} } ) {
391 $record->{response}->done( $self->_response( 503, { error => 'Interaction service closed' } ) )
392 2 ▶ unless $record->{response}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"response"}->is_ready 1 1 2
393 }
394 18 $self->{dispatcher}->close;
395 18 $self->{pending} = {};
396 18 $self->{replies} = {};
397 18 $self->{seen} = {};
398 18 ▶ $self->{commands}->close if $self->{own_commands};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"own_commands"} 18 0 18
399 18 ▶ return $self->{own_rest} ? $self->{rest}->close : $self->{loop}->new_future->done;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'own_rest'} ? : 18 0 18
400 }
401  
402 4 1;
403  
404 __END__
« blib/lib/Discord/Client/REST/RateLimiter.pm ↑ Summary blib/lib/Discord/Client.pm »