blib/lib/Discord/Client/HTTPInteractions.pm

filter: Statementstmt 96.9% 224 / 231 Branchbran 77.6% 73 / 94 Conditioncond 62.8% 44 / 70 MC/DCmcdc 29.2% 12 / 41 Subroutinesub 93.3% 42 / 45 Podpod 100% 11 / 11 totaltotal 82.5% 406 / 492 CC 78 SCAR 22.3
CC
78
Cov
86%
new
23.5
handle
20.3
_handle_psgi_request
9.3
CRAP 9.3
« 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.002';
18  
19 18 ▶sub new {
Subroutine new: called CC=19 SCAR=31.6
Pod documented
20 18 my ( $class, %args ) = @_;
21  
22 # Allocate independent limits for active handlers, cached replies and retained IDs.
23 18 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition66%2/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
1620
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 18 ▶ }, $class;
Condition90%10/11
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
l!l&&r!l&&!r
1710
$args{"max_body"} // 1048576
l!l
117
$args{"max_replay"} // 1024
l!l
216
$args{"max_seen"} // 10000
l!l
315
$args{"replay_ttl"} // 900
l!l
216
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 18 for my $key (qw(max_body max_replay max_seen replay_ttl)) {
40 72 ▶ 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 72 72
Condition33%1/3
not ref $self->{$key} and $self->{$key} =~ /\A[1-9]\d*\z/u
!ll&&!rl&&r
0072
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 18 for my $key (qw(clock delay on_error)) {
43 54 ▶ 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 54 54
Condition66%2/3
exists $args{$key} and ref $args{$key} ne "CODE"
!ll&&!rl&&r
3510
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 18 ▶ ( exists( $args{max_age} ) ? ( max_age => $args{max_age} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'wall_clock'} ? : 17 1 18
exists $args{'max_age'} ? : 0 18 18
51 );
52 18 ▶ $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
0180
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 18 $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 18 ▶ );
Condition33%1/3
$args{"commands"} // "Discord::Client::Commands"->new("rest", $self->{"rest"}, "loop", $loop, "application_id", $args{"application_id"})
l!l&&r!l&&!r
0180
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 18 $self->{own_commands} = !$args{commands};
60  
61 # Dispatcher error callbacks must not keep the HTTP service alive.
62 18 my $weak = $self;
63 18 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 18 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'delay'} ? : 17 1 18
Condition100%2/2
$args{"max_pending"} // 512
l!l
117
MC/DC100%1/1
$args{"max_pending"} // 512
$args{"max_pending"}
Truth table100%2/2
A
A= $args{"max_pending"}
Aresult
11
00
88 18 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 2 ▶sub dispatcher { $_[0]->{dispatcher} }
Subroutine dispatcher: called CC=1 SCAR=0.0
Pod documented
95  
96 13 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
97 13 my ( $self, @args ) = @_;
98 13 $self->{commands}->define(@args);
99 13 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 1070 ▶sub _response {
Subroutine _response: called CC=1 SCAR=0.0
109 1070 my ( $self, $status, $data ) = @_;
110  
111 # Return PSGI bytes and a byte count, not a character count.
112 1070 my $body = JSON::PP->new->utf8->encode($data);
113 1070 return [ $status, [ 'Content-Type' => 'application/json', 'Content-Length' => length($body) ], [$body] ];
114 }
115  
116 22 ▶sub _done {
Subroutine _done: called CC=1 SCAR=0.0
117 22 my ( $self, $status, $data ) = @_;
118 22 return $self->{loop}->new_future->done( $self->_response( $status, $data ) );
119 }
120  
121 1049 ▶sub _copy_response {
Subroutine _copy_response: called CC=1 SCAR=0.0
122 1049 my ($response) = @_;
123 1049 my ( $status, $headers, $body ) = @$response;
124 1049 return [ $status, [@$headers], [@$body] ];
125 }
126  
127 1047 ▶sub _waiter {
Subroutine _waiter: called CC=1 SCAR=0.0
128 1047 my ( $self, $source ) = @_;
129  
130 # Each HTTP caller gets its own response copy and cannot cancel shared handler work.
131 1047 my $future = $self->{loop}->new_future;
132 $source->on_ready(
133 1047 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
134 1047 ▶ return if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 1 1046 1047
135 1046 ▶ if ( $source->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->is_done) { } 1046 0 1046
136 1046 my ($response) = $source->result;
137 1046 $future->done( _copy_response($response) );
138 }
139 else {
140 0 $future->done( $self->_response( 503, { error => 'Interaction request cancelled' } ) );
141 }
142 }
143 1047 );
144 1047 return $future;
145 }
146  
147 1072 ▶sub handle {
Subroutine handle: called CC=18 SCAR=30.1
Pod documented
148 1072 my ( $self, $headers, $body, %args ) = @_;
149  
150 # Record arrival before verification so all admission time counts toward the deadline.
151 1072 ▶ my $received_at = $args{received_at} // $self->{clock}->();
Condition66%2/3
$args{"received_at"} // $self->{"clock"}->()
l!l&&r!l&&!r
810640
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 1072 ++$self->{counters}{requests};
153 1072 ▶ return $self->_done( 503, { error => 'Interaction service closed' } ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 1071 1072
154 1071 ▶ 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 1071 1071
Condition33%1/3
defined $body and not ref $body
!ll&&!rl&&r
001071
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 1071 ▶ 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 1070 1071
156 1070 ▶ return $self->_done( 401, { error => 'Invalid request signature' } ) unless ref($headers) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers eq "HASH" 0 1070 1070
157 1070 my %normalized_headers = map { lc($_) => $headers->{$_} } keys %$headers;
158 1070 my $signature = $normalized_headers{'x-signature-ed25519'};
159 1070 my $timestamp = $normalized_headers{'x-signature-timestamp'};
160  
161 # Verify the exact signed octets before trusting or parsing JSON fields.
162 1070 ▶ unless ( $self->{verifier}->verify( $signature, $timestamp, $body ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"verifier"}->verify($signature, $timestamp, $body)) 10 1060 1070
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 1060 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 1060 ▶ || ref( $payload->{type} );
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $payload ne "HASH" or not defined $payload->{"type"} or ref $payload->{"type"} 1 1059 1060
Condition66%6/9
$@ or ref $payload ne "HASH" or not defined $payload->{"type"} or ref $payload->{"type"}
l!l&&r!l&&!r
101059
$@ or ref $payload ne "HASH" or not defined $payload->{"type"}
l!l&&r!l&&!r
101059
$@ or ref $payload ne "HASH"
l!l&&r!l&&!r
101059
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 1059 ▶ return $self->_done( 200, { type => 1 } ) if $payload->{type} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $payload->{"type"} eq 1 5 1054 1059
175  
176 1054 my $now = $self->{clock}->();
177 1054 $self->_expire_replay_records($now);
178  
179 # Coalesce running duplicates and replay completed responses without rerunning handlers.
180 1054 my $id = $payload->{id};
181 1054 ▶ 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 1054 1054
Condition33%2/6
defined $id and not ref $id and $id =~ /\A\d+\z/u
!ll&&!rl&&r
001054
defined $id and not ref $id
!ll&&!rl&&r
001054
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 1054 my $replayed_response = $self->_replay_response($id);
184 1054 ▶ return $replayed_response if $replayed_response;
Branch: 1 branch
BranchTrueFalseTotal
if $replayed_response 7 1047 1054
185  
186 return $self->_done( 503, { error => 'Interaction capacity exceeded' } )
187 1047 ▶ if keys( %{ $self->{seen} } ) >= $self->{max_seen};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"seen"};} >= $self->{"max_seen"} 2 1045 1047
188 1045 return $self->_dispatch_payload( $payload, $received_at, $now );
189 }
190  
191 1054 ▶sub _expire_replay_records {
Subroutine _expire_replay_records: called CC=4 SCAR=13.9
192 1054 my ( $self, $now ) = @_;
193  
194 # Expire completed IDs only; running work must remain protected from replay.
195 1054 for my $id ( keys %{ $self->{seen} } ) {
196 525844 ▶ next if $self->{pending}{$id} || $self->{seen}{$id} > $now;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pending"}{$id} or $self->{"seen"}{$id} > $now 525840 4 525844
Condition100%3/3
$self->{"pending"}{$id} or $self->{"seen"}{$id} > $now
l!l&&r!l&&!r
45258364
MC/DC100%2/2
$self->{"pending"}{$id} or $self->{"seen"}{$id} > $now
$self->{"pending"}{$id} $self->{"seen"}{$id} > $now
Truth table100%3/3
A or B
A= $self->{"pending"}{$id}B= $self->{"seen"}{$id} > $now
ABresult
1X1
011
000
197 4 delete $self->{seen}{$id};
198 4 delete $self->{replies}{$id};
199 }
200 }
201  
202 1054 ▶sub _replay_response {
Subroutine _replay_response: called CC=4 SCAR=13.9
203 1054 my ( $self, $id ) = @_;
204  
205 # Pending work, cached bytes and retained IDs have different replay outcomes.
206 1054 ▶ if ( my $record = $self->{pending}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->{"pending"}{$id}) 2 1052 1054
207 2 ++$self->{counters}{duplicates};
208 2 return $self->_waiter( $record->{response} );
209 }
210 1052 ▶ if ( my $record = $self->{replies}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->{"replies"}{$id}) 3 1049 1052
211 3 ++$self->{counters}{duplicates};
212 3 return $self->{loop}->new_future->done( _copy_response( $record->{response} ) );
213 }
214  
215 # Evicted response bytes do not permit the same side effects to run again.
216 1049 ▶ if ( exists $self->{seen}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"seen"}{$id}) 2 1047 1049
217 2 ++$self->{counters}{duplicates};
218 2 return $self->_done( 409, { error => 'Interaction response no longer cached' } );
219 }
220 1047 return undef;
221 }
222  
223 1045 ▶sub _dispatch_payload {
Subroutine _dispatch_payload: called CC=2 SCAR=6.9
224 1045 my ( $self, $payload, $received_at, $now ) = @_;
225 1045 my $id = $payload->{id};
226 1045 my $response_future = $self->{loop}->new_future;
227 1045 my $record = { response => $response_future };
228  
229 # Reserve the ID before invoking application code, including reentrant calls.
230 1045 $self->{pending}{$id} = $record;
231 1045 $self->{seen}{$id} = $now + $self->{replay_ttl};
232 my $work = $self->{dispatcher}->handle(
233 $payload,
234 received_at => $received_at,
235 1039 ▶ initial_sender => sub { $self->_complete_inline_reply( $response_future, $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
236 1045 );
237 1045 ▶ $record->{work} = $work unless $work->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $work->is_ready 8 1037 1045
238  
239 # Release active ownership after both acknowledgement and application work settle.
240 $work->on_ready(
241 1045 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
242 1045 delete $record->{work};
243 1045 ▶ 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 1044 1045
Condition100%3/3
$work->is_failed and ($work->failure)[1] eq "queue_full"
!ll&&!rl&&r
104131
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
244  
245 # Admission failed before any handler ran; retries may try again.
246 1 delete $self->{seen}{$id};
247 1 delete $self->{pending}{$id};
248 1 $self->_finish_unanswered_response( $response_future, $work );
249 1 return;
250 }
251 1044 $self->_finish_unanswered_response( $response_future, $work );
252 1044 ▶ $self->_cache_response( $id, $response_future ) unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 1042 2 1044
253 1044 delete $self->{pending}{$id};
254 }
255 1045 );
256 1045 return $self->_waiter($response_future);
257 }
258  
259 1042 ▶sub _cache_response {
Subroutine _cache_response: called CC=2 SCAR=6.9
260 1042 my ( $self, $id, $response_future ) = @_;
261 1042 $self->{replies}{$id} = { response => ( $response_future->result )[0], order => ++$self->{reply_order} };
262 1042 ▶ return if keys( %{ $self->{replies} } ) <= $self->{max_replay};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"replies"};} <= $self->{"max_replay"} 1040 2 1042
263  
264 # Evict response bytes, retaining the ID so side effects cannot run twice.
265 9019 my ($oldest) = sort { $self->{replies}{$a}{order} <=> $self->{replies}{$b}{order} }
266 2 keys %{ $self->{replies} };
267 2 delete $self->{replies}{$oldest};
268 }
269  
270 1039 ▶sub _complete_inline_reply {
Subroutine _complete_inline_reply: called CC=3 SCAR=11.2
271 1039 my ( $self, $response_future, $reply ) = @_;
272  
273 # Accept at most one serializable inline response for the request.
274 1039 ▶ return $self->{loop}->new_future->fail( 'HTTP response is already complete', 'state' )
Branch: 1 branch
BranchTrueFalseTotal
if $response_future->is_ready 0 1039 1039
275 if $response_future->is_ready;
276 1039 my $wire = eval { $self->_response( 200, $reply ) };
277 1039 ▶ return $self->{loop}->new_future->fail( 'Invalid HTTP interaction response', 'request' ) unless $wire;
Branch: 1 branch
BranchTrueFalseTotal
unless $wire 0 1039 1039
278 1039 $response_future->done($wire);
279 1039 return $self->{loop}->new_future->done;
280 }
281  
282 1045 ▶sub _finish_unanswered_response {
Subroutine _finish_unanswered_response: called CC=4 SCAR=13.9
283 1045 my ( $self, $response_future, $work ) = @_;
284 1045 ▶ return if $response_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $response_future->is_ready 1041 4 1045
285  
286 # Map public failure categories to HTTP status without exposing private error details.
287 4 my $status = 500;
288 4 ▶ if ( $work->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work->is_failed) 4 0 4
289 4 my ( undef, $category ) = $work->failure;
290 4 my %status_for = ( deadline => 504, protocol => 400, queue_full => 503 );
291 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
292 }
293 4 $response_future->done( $self->_response( $status, { error => 'Interaction could not be answered' } ) );
294 }
295  
296 2 ▶sub psgi_app {
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod documented
297 2 my ($self) = @_;
298 2 ▶ return sub { $self->_handle_psgi_request( $_[0] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
299 }
300  
301 9 ▶sub _handle_psgi_request {
Subroutine _handle_psgi_request: called CC=9 SCAR=22.3
302 9 my ( $self, $env ) = @_;
303  
304 # Bound the POST body before reading from the host-provided input stream.
305 9 my $received_at = $self->{clock}->();
306 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
307 8 my $length = $env->{CONTENT_LENGTH};
308 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
309 unless defined($length) && $length =~ /\A\d+\z/;
310 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
311  
312 # Preserve request bytes for signature validation; reject incomplete host input.
313 8 my $body = eval { _read_psgi_body( $env->{'psgi.input'}, $length ) };
314 8 ▶ return $self->_response( 400, { error => 'Incomplete request body' } ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 7 8
315 my $future = $self->handle(
316 {
317 'X-Signature-Ed25519' => $env->{HTTP_X_SIGNATURE_ED25519},
318 'X-Signature-Timestamp' => $env->{HTTP_X_SIGNATURE_TIMESTAMP},
319 },
320 7 $body,
321 received_at => $received_at
322 );
323 7 ▶ return $future->get if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 5 2 7
324  
325 # Only streaming hosts can wait asynchronously for an unanswered interaction.
326 2 ▶ unless ( $env->{'psgi.streaming'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"psgi.streaming"}) 1 1 2
327 1 return $self->_reject_pending_psgi_response($body);
328 }
329 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
330 1 my ($responder) = @_;
331 1 ▶ $future->on_ready( sub { $responder->( $future->get ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
332 1 };
333 }
334  
335 8 ▶sub _read_psgi_body {
Subroutine _read_psgi_body: called CC=4 SCAR=13.9
336 8 my ( $input, $length ) = @_;
337 8 my $body = '';
338  
339 # PSGI input may return short chunks; zero bytes before Content-Length is a failure.
340 8 ▶ while ( length($body) < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body < $length) 9 7 16
341 9 my $read = $input->read( my $chunk, $length - length($body) );
342 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
343 8 $body .= $chunk;
344 }
345 7 return $body;
346 }
347  
348 1 ▶sub _reject_pending_psgi_response {
Subroutine _reject_pending_psgi_response: called CC=7 SCAR=21.8
349 1 my ( $self, $body ) = @_;
350  
351 # A synchronous host cannot keep an unanswered handler running after returning 503.
352 1 my $payload = eval { JSON::PP->new->utf8->decode($body) };
353 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
354 1 ▶ if ($record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record) 1 0 1
355 $record->{response}->done( $self->_response( 503, { error => 'Asynchronous PSGI support required' } ) )
356 1 ▶ unless $record->{response}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"response"}->is_ready 1 0 1
357 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
358 }
359 1 return $self->_response( 503, { error => 'Asynchronous PSGI support required' } );
360 }
361  
362 4 ▶sub stats {
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
363 4 my ($self) = @_;
364 return {
365 4 %{ $self->{counters} },
366 4 replay_entries => scalar( keys %{ $self->{replies} } ),
367 4 pending_requests => scalar( keys %{ $self->{pending} } ),
368 4 seen_entries => scalar( keys %{ $self->{seen} } )
369 };
370 }
371  
372 17 ▶sub close {
Subroutine close: called CC=5 SCAR=16.2
Pod documented
373 17 my ($self) = @_;
374  
375 # Answer outstanding HTTP callers before cancelling dispatcher work and owned resources.
376 17 $self->{closed} = 1;
377 17 for my $record ( values %{ $self->{pending} } ) {
378 $record->{response}->done( $self->_response( 503, { error => 'Interaction service closed' } ) )
379 2 ▶ unless $record->{response}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"response"}->is_ready 1 1 2
380 }
381 17 $self->{dispatcher}->close;
382 17 $self->{pending} = {};
383 17 $self->{replies} = {};
384 17 $self->{seen} = {};
385 17 ▶ $self->{commands}->close if $self->{own_commands};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"own_commands"} 17 0 17
386 17 ▶ return $self->{own_rest} ? $self->{rest}->close : $self->{loop}->new_future->done;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'own_rest'} ? : 17 0 17
387 }
388  
389 4 1;
390  
391 __END__
« blib/lib/Discord/Client/REST/RateLimiter.pm ↑ Summary blib/lib/Discord/Client.pm »