InternetData.pm

filter: Statementstmt 99.8% 489 / 490 Branchbran 95.6% 199 / 208 Conditioncond 84.2% 134 / 159 MC/DCmcdc 73.2% 63 / 86 Subroutinesub 98.9% 91 / 92 Podpod 62.5% 10 / 16 totaltotal 93.8% 986 / 1051 CC 165 SCAR 23.1
CC
165
Cov
96%
_nonconforming
35.2
new
19.0
lookup_batch_p
13.0
CRAP 10.1
↑ Summary InternetData/Oauth.pm »
1 package InternetData;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Mojo::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use InternetData::AsnResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use InternetData::Bogon ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use InternetData::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use InternetData::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use InternetData::DomainResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use InternetData::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use InternetData::Oauth;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use InternetData::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 8 our $VERSION = '1.15.0';
25 8 our @EXPORT_OK = ('is_bogon');
26  
27 8 ▶use constant DEFAULT_BASE_URL => 'https://api.internetdata.io';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # The most addresses POST /batch takes in one call; a larger batch is sent in
30 # chunks of this size.
31 8 ▶use constant BATCH_MAX => 1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # Seconds before the first retry; each later one waits twice the one before.
34 8 ▶use constant BACKOFF_BASE => 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # The longest Retry-After honored, in seconds: 2**31 - 1 ms, about 24.8 days,
37 # the same bound as the .NET SDK's.
38 8 ▶use constant LONGEST_WAIT => 2_147_483.647;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 8 my %OPTIONS = map { $_ => 1 } qw(
41 api_key base_url cache_size cache_ttl concurrency retries timeout ua
42 );
43  
44 # The shapes of the answers, from the pinned spec's schemas: a member it requires
45 # and never sends as null is required, a nullable or optional one may be absent
46 # or null, and every one present has the spec's type at every depth. A detail
47 # requires no member, so `{}`, its answer under a false flag, is one.
48 8 my $CLASS_DETAIL = [fields => [map { [$_ => ['string']] } qw(provider provider_name confidence last_seen)]];
49 my $PROXY_DETAIL = [fields => [
50 32 (map { [$_ => ['string']] } qw(provider provider_name first_seen last_seen)),
51 8 (map { [$_ => ['integer']] } qw(hits hits_days_pct providers_num)),
52 ]];
53 my $PUBPROXY_DETAIL = [fields => [
54 [entry => ['boolean']], [exit => ['boolean']], [protocols => [list => ['string']]],
55 [num_ports => ['integer']], [num_proxies => ['integer']],
56 8 (map { [$_ => ['string']] } qw(last_seen last_usable)),
57 ]];
58 8 my $CARRIER_DETAIL = [fields => [map { [$_ => ['string']] } qw(name mcc mnc country confidence)]];
59 my $SATELLITE_DETAIL = [fields => [
60 8 (map { [$_ => ['string']] } qw(provider provider_name orbit segment country)),
61 [asn => ['integer']], [confidence => ['string']],
62 ]];
63 my $SSH_DETAIL = [fields => [
64 16 [port => ['integer']], (map { [$_ => ['string']] } qw(banner host_key_fp)),
65 8 [session_ok => ['boolean']], (map { [$_ => ['string']] } qw(first_seen last_seen)),
66 ]];
67 my $SMTP_DETAIL = [fields => [
68 [port => ['integer']], [banner => ['string']], [session_ok => ['boolean']],
69 8 (map { [$_ => ['string']] } qw(first_seen last_seen)),
70 ]];
71 # Web IP's detail is a list of the web servers found, each one whole.
72 my $WEB_SERVER = [fields => [
73 16 [port => ['integer'], 1], (map { [$_ => ['boolean'], 1] } qw(tls_ok session_ok)),
74 [status_code => ['integer'], 1],
75 48 (map { [$_ => ['string'], 1] } qw(server location title cert_subject_cn cert_subject_o cert_issuer_dn)),
76 [cert_browser_trusted => ['boolean'], 1],
77 8 (map { [$_ => ['string'], 1] } qw(tls_version first_seen last_seen)),
78 ]];
79 my $DNS_RESOLVER_DETAIL = [fields => [
80 [port => ['integer']], [recursion_available => ['boolean']], [rcode => ['integer']],
81 8 [answer_correct => ['boolean']], (map { [$_ => ['string']] } qw(first_seen last_seen)),
82 ]];
83 my $BITTORRENT_DETAIL = [fields => [
84 16 (map { [$_ => ['boolean']] } qw(verified id_matches_ip)),
85 24 (map { [$_ => ['string']] } qw(first_seen last_seen last_reachable)),
86 8 (map { [$_ => ['integer']] } qw(days_seen num_ports peak_num_ports)),
87 [indexer => ['boolean']], [clients => [list => ['string']]],
88 ]];
89 # The shared objects, whose members past the required ones are paid.
90 my $LOCATION = [fields => [
91 80 (map { [$_ => ['string'], 1] } qw(
92 continent continent_name country country_name timezone currency_code currency_name
93 currency_symbol flag_emoji flag_unicode
94 )),
95 32 (map { [$_ => ['string']] } qw(region region_code city postal_code)),
96 16 (map { [$_ => ['number']] } qw(latitude longitude)), [accuracy_radius => ['integer']],
97 8 (map { [$_ => ['string']] } qw(geoname_id dma_code last_changed)),
98 ]];
99 my $ASN = [fields => [
100 32 [asn => ['integer'], 1], (map { [$_ => ['string'], 1] } qw(name domain type prefix)),
101 8 (map { [$_ => ['string']] } qw(country rpki_status)),
102 ]];
103 my $COMPANY = [fields => [
104 8 (map { [$_ => ['string'], 1] } qw(name domain)), (map { [$_ => ['string']] } qw(type country)),
105 ]];
106 my $ABUSE = [fields => [
107 8 [email => ['string'], 1], (map { [$_ => ['string']] } qw(name phone address country network)),
108 [validated => ['boolean']], [unvalidated_since => ['string']],
109 ]];
110 my $WHOIS = [fields => [
111 8 (map { [$_ => ['string'], 1] } qw(id name org country status source domain abuse_email)),
112 [asn => ['integer'], 1],
113 ]];
114 8 my $RWHOIS = [fields => [map { [$_ => ['string'], 1] } qw(id name country source domain abuse_email)]];
115 our $LOOKUP = [fields => [
116 [ip => ['string'], 1], [is_bogon => ['boolean'], 1], [is_vpn => ['boolean']],
117 16 (map { [$_ => ['boolean'], 1] } qw(is_tor is_relay)),
118 48 (map { [$_ => ['boolean']] } qw(is_resproxy is_dcproxy is_mobproxy is_pubproxy is_hosting is_cdn)),
119 64 (map { [$_ => ['boolean'], 1] } qw(
120 is_anycast is_carrier is_satellite is_ssh is_smtp is_web is_dns_resolver is_bittorrent
121 )),
122 16 [vpn => $CLASS_DETAIL], (map { [$_ => $CLASS_DETAIL, 1] } qw(tor relay)),
123 24 (map { [$_ => $PROXY_DETAIL] } qw(resproxy dcproxy mobproxy)), [pubproxy => $PUBPROXY_DETAIL],
124 8 (map { [$_ => $CLASS_DETAIL] } qw(hosting cdn)), [carrier => $CARRIER_DETAIL],
125 [satellite => $SATELLITE_DETAIL], [ssh => $SSH_DETAIL], [smtp => $SMTP_DETAIL],
126 [web => [list => $WEB_SERVER]], [dns_resolver => $DNS_RESOLVER_DETAIL], [bittorrent => $BITTORRENT_DETAIL],
127 [location => $LOCATION, 1], [asn => $ASN, 1], [company => $COMPANY, 1], [hostname => ['string'], 1],
128 [abuse => $ABUSE, 1], [whois => $WHOIS], [rwhois => $RWHOIS],
129 [hosted_domains => [fields => [[total => ['integer'], 1], [domains => [list => ['string']]]]]],
130 [trial_expired => ['boolean']],
131 ]];
132 my $AS_PREFIX = [fields => [
133 [prefix => ['string'], 1], [peer_count => ['integer'], 1],
134 8 (map { [$_ => ['string'], 1] } qw(rpki_status first_seen country)),
135 [size => ['integer'], 1], [other_origins => [list => ['integer']], 1],
136 ]];
137 our $ASN_LOOKUP = [fields => [
138 40 [asn => ['integer'], 1], (map { [$_ => ['string'], 1] } qw(name country registry allocated domain)),
139 [type => ['string']],
140 48 (map { [$_ => ['integer']] } qw(num_ips num_prefixes4 num_prefixes6 rpki_valid rpki_invalid rpki_not_found)),
141 [whois => [map_of => ['string']]], [prefixes => [list => $AS_PREFIX]],
142 [rank => ['integer']], [tier1 => ['boolean']],
143 8 (map { (["num_$_" => ['integer']], [$_ => [list => ['integer']]]) } qw(upstreams peers downstreams)),
144 [hosted_domains => [fields => [[total => ['integer'], 1], [domains => [list => ['string']]]]]],
145 [trial_expired => ['boolean']],
146 ]];
147 8 my $DNS_ADDRESS = [fields => [map { [$_ => ['string'], 1] } qw(ip last_seen)]];
148 my $DOMAIN_DNS = [fields => [
149 16 (map { [$_ => [list => $DNS_ADDRESS], 1] } qw(a aaaa)),
150 16 [mx => [list => [fields => [[priority => ['integer'], 1], map { [$_ => ['string'], 1] } qw(host last_seen)]]], 1],
151 16 [ns => [list => [fields => [map { [$_ => ['string'], 1] } qw(host last_seen)]]], 1],
152 8 [cname => [list => [fields => [map { [$_ => ['string'], 1] } qw(target last_seen)]]], 1],
153 ]];
154 my $DOMAIN_IP_RANGES = [fields => [
155 16 (map { [$_ => ['string'], 1] } qw(domain redirects_to)), (map { [$_ => ['integer'], 1] } qw(total ipv4)),
156 8 [ranges => [list => [fields => [map { [$_ => ['string'], 1] } qw(range name country)]]], 1],
157 ]];
158 our $DOMAIN_LOOKUP = [fields => [
159 [domain => ['string'], 1], [is_registered => ['boolean'], 1], [is_disposable => ['boolean']],
160 40 (map { [$_ => ['boolean'], 1] } qw(has_a has_aaaa has_mx has_ns has_cname)),
161 [dns => $DOMAIN_DNS], [ip_ranges => $DOMAIN_IP_RANGES],
162 [disposable => [fields => [
163 [confidence => ['string']], [providers => [list => ['string']]],
164 8 (map { [$_ => ['string']] } qw(first_seen last_seen)),
165 ]]],
166 [trial_expired => ['boolean']],
167 ]];
168 8 our $BATCH_ERROR = [fields => [[status => ['integer'], 1], [error => ['string'], 1]]];
169 # Each entry is judged alone, by _lookup_chunk_p.
170 8 our $BATCH = [fields => [[results => [map_of => ['any']], 1], [errors => [map_of => ['any']], 1]]];
171 8 our $ENTITLEMENT = [fields => [
172 [org_id => ['string'], 1],
173 [apikey => [fields => [
174 [id => ['string'], 1], [expires => ['date-time']], [allowed_cidrs => [list => ['string']], 1],
175 ]], 1],
176 [plan => [fields => [[key => ['string'], 1], [name => ['string'], 1], [tier => ['string'], 1]]], 1],
177 [usage => [fields => [
178 [requests => ['integer'], 1], [quota => ['integer'], 1], [hard_limit => ['integer']],
179 [window_start => ['date-time'], 1], [window_end => ['date-time'], 1],
180 ]], 1],
181 ]];
182  
183 477 ▶sub new {
Subroutine new: called CC=19 SCAR=29.4
Pod documented
184 477 my ($class, %args) = @_;
185 477 my @unknown = sort grep { !$OPTIONS{$_} } keys %args;
186 477 ▶ Carp::croak("InternetData->new: unknown option(s): @unknown") if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 476 477
187  
188 476 ▶ my $concurrency = defined $args{concurrency} ? $args{concurrency} : 8;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'concurrency'} ? : 11 465 476
189 476 ▶ my $retries = defined $args{retries} ? $args{retries} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'retries'} ? : 229 247 476
190 476 ▶ my $cache_size = defined $args{cache_size} ? $args{cache_size} : 10_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'cache_size'} ? : 32 444 476
191 476 ▶ Carp::croak('InternetData->new: concurrency must be a whole number, 1 or more') unless _is_count($concurrency, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($concurrency, 1) 9 467 476
192 467 ▶ Carp::croak('InternetData->new: retries must be a whole number, 0 or more') unless _is_count($retries, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($retries, 0) 8 459 467
193 # Mojo arms a negative or non-numeric bound as a timer that fires at once, so
194 # every call would fail as a network error after reaching the server.
195 459 ▶ my $timeout = defined $args{timeout} ? $args{timeout} : 30;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'timeout'} ? : 46 413 459
196 459 ▶ Carp::croak('InternetData->new: timeout must be a number of seconds, 0 or more')
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($timeout) and $timeout >= 0 2 457 459
Condition100%3/3
&Scalar::Util::looks_like_number($timeout) and $timeout >= 0
!ll&&!rl&&r
11457
MC/DC100%2/2
&Scalar::Util::looks_like_number($timeout) and $timeout >= 0
&Scalar::Util::looks_like_number($timeout) $timeout >= 0
Truth table100%3/3
A and B
A= &Scalar::Util::looks_like_number($timeout)B= $timeout >= 0
ABresult
0X0
100
111
197 unless Scalar::Util::looks_like_number($timeout) && $timeout >= 0;
198  
199 # Trimmed, so a key read from a file loses its newline, and blanks alone are
200 # no key. Mojo refuses a CR or LF in a header with a raw string from every
201 # call and sends any other control character, so one left inside is refused
202 # here, naming the cause and not the key.
203 457 my $api_key = $args{api_key};
204 457 ▶ if (defined $api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api_key) 216 241 457
205 216 $api_key =~ s/\A\s+|\s+\z//g;
206 216 ▶ $api_key = undef unless length $api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $api_key 4 212 216
207 }
208 457 ▶ Carp::croak('InternetData->new: api_key holds a control character')
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/ 5 452 457
Condition100%3/3
defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/
!ll&&!rl&&r
2452075
MC/DC100%2/2
defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/
defined $api_key $api_key =~ /[\x00-\x1f\x7f]/
Truth table100%3/3
A and B
A= defined $api_keyB= $api_key =~ /[\x00-\x1f\x7f]/
ABresult
0X0
100
111
209 if defined $api_key && $api_key =~ /[\x00-\x1f\x7f]/;
210 # Mojo writes a header as bytes, so a character past U+00FF dies in the
211 # reactor and every call fails after its whole timeout with nothing sent.
212 452 ▶ Carp::croak('InternetData->new: api_key holds a character past U+00FF')
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_key and $api_key =~ /[^\x00-\xff]/ 2 450 452
Condition100%3/3
defined $api_key and $api_key =~ /[^\x00-\xff]/
!ll&&!rl&&r
2452052
MC/DC100%2/2
defined $api_key and $api_key =~ /[^\x00-\xff]/
defined $api_key $api_key =~ /[^\x00-\xff]/
Truth table100%3/3
A and B
A= defined $api_keyB= $api_key =~ /[^\x00-\xff]/
ABresult
0X0
100
111
213 if defined $api_key && $api_key =~ /[^\x00-\xff]/;
214  
215 my $self = bless {
216 api_key => $api_key,
217 base_url => _base_url($args{base_url}),
218 concurrency => 0 + $concurrency,
219 retries => 0 + $retries,
220 # Held as a number: Mojo tests the bound for truth before arming it, and a
221 # string such as '0.0' is true, so it would be a timer that fires at once
222 # rather than no bound.
223 timeout => 0 + $timeout,
224 cache => $cache_size > 0 ? InternetData::Cache->new(
225 max => $cache_size,
226 ttl => defined $args{cache_ttl} ? $args{cache_ttl} : 3600,
227 ) : undef,
228 # The addresses with a request in flight, each a promise its waiters
229 # take the answer from (see _board).
230 flights => {},
231 450 ▶ ua => $args{ua} || Mojo::UserAgent->new,
Branch: 2 branches
BranchTrueFalseTotal
$cache_size > 0 ? : 419 31 450
defined $args{'cache_ttl'} ? : 1 418 419
Condition66%2/3
$args{'ua'} || 'Mojo::UserAgent'->new
l!l&&r!l&&!r
84420
MC/DC0%0/2
$args{'ua'} || 'Mojo::UserAgent'->new
$args{'ua'} 'Mojo::UserAgent'->new
Truth table66%2/3
A || B
A= $args{'ua'}B= 'Mojo::UserAgent'->new
ABresult
1X1
011
000
232 }, $class;
233  
234 # Mojo::UserAgent does not follow redirects by default, but MOJO_MAX_REDIRECTS
235 # in the environment turns that on for every agent in the process. Setting it
236 # here is what stops a download's 302 being chased into a multi-gigabyte
237 # transfer, on a machine whose environment we do not own.
238 450 $self->{ua}->max_redirects(0);
239 450 $self->{ua}->request_timeout($self->{timeout});
240 450 $self->{ua}->transactor->name("internetdata-perl/$VERSION");
241 450 return $self;
242 }
243  
244 # Both a method and an exportable function, so the address is the LAST argument
245 # either way: is_bogon($ip) and $client->is_bogon($ip) reach the same code.
246 111 ▶sub is_bogon {
Subroutine is_bogon: called CC=3 SCAR=12.5
Pod documented
247 111 my $ip = pop;
248 111 ▶ Carp::croak('is_bogon: expected an IP address') if !defined $ip || ref $ip;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip or ref $ip 0 111 111
Condition33%1/3
not defined $ip or ref $ip
l!l&&r!l&&!r
00111
MC/DC0%0/2
not defined $ip or ref $ip
not defined $ip ref $ip
Truth table33%1/3
not(A) or B
A= not defined $ipB= ref $ip
ABresult
1X1
011
000
249 111 return InternetData::Bogon::is_bogon($ip);
250 }
251  
252 # What every database says about one address. A bogon is answered locally and
253 # never reaches the network; everything else is served, then cached for this
254 # client.
255 159 ▶sub lookup {
Subroutine lookup: called CC=1 SCAR=0.0
Pod documented
256 159 my $self = shift;
257 159 $self->_assert_blocking_ok('lookup');
258 158 return $self->_wait($self->lookup_p(@_));
259 }
260  
261 201 ▶sub lookup_p {
Subroutine lookup_p: called CC=10 SCAR=23.0
Pod undocumented
262 201 my ($self, $ip, %options) = @_;
263 201 ▶ Carp::croak('lookup: expected an IP address') if !defined $ip || !length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 2 199 201
Condition100%3/3
defined $ip and length $ip
!ll&&!rl&&r
11199
MC/DC100%2/2
defined $ip and length $ip
defined $ip length $ip
Truth table100%3/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
264 199 $self->_check_options('lookup', \%options, 'retries', 'timeout');
265 # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
266 173 $ip = InternetData::Bogon::unmapped($ip);
267 # This API answers an AS or a domain at the same path, as an answer this call
268 # cannot read, so one sent here would come back as three retried server_errors.
269 173 ▶ return Mojo::Promise->reject(InternetData::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
unless InternetData::Bogon::is_ip($ip) 16 157 173
270 kind => 'bad_request',
271 message => "'$ip' is not an IP address: look up an AS with lookup_asn, and a domain with lookup_domain",
272 )) unless InternetData::Bogon::is_ip($ip);
273  
274 157 ▶ return Mojo::Promise->resolve(InternetData::Bogon::bogon_result($ip))
Branch: 1 branch
BranchTrueFalseTotal
if InternetData::Bogon::is_bogon($ip) 4 153 157
275 if InternetData::Bogon::is_bogon($ip);
276  
277 153 ▶ if ($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 96 57 153
278 96 my $hit = $self->{cache}->get($ip);
279 96 ▶ return Mojo::Promise->resolve($hit) if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 4 92 96
280 # A miss for an address already in flight, for a lookup or a batch,
281 # takes that request's answer rather than sending its own.
282 92 ▶ return $self->{flights}{$ip}->then(sub { shift }) if $self->{flights}{$ip};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"flights"}{$ip} 24 68 92
283 }
284  
285 125 my $flight = $self->_board($ip);
286 125 my $url = $self->_url('/' . Mojo::Util::url_escape($ip, '^A-Za-z0-9\-._~:'));
287 125 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 2 123 125
288 # Anything that dies before a request is under way lands the flight too, or
289 # every later lookup of the address would wait on it forever.
290 125 ▶ my $sent = eval { $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $LOOKUP) }) };
Subroutine __ANON__: called CC=1 SCAR=0.0
291 125 ▶ if (!$sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 3 122 125
292 3 my $error = $@;
293 3 $self->_land($ip, $flight, 0, $error);
294 3 die $error;
295 }
296 return $sent->then(
297 44 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
298 44 my $result = InternetData::Result->from_wire(shift);
299 # Only a served answer is cached, and before it lands, so a miss
300 # after the flight is gone finds it. Errors never are, and bogons
301 # never reach this far.
302 44 ▶ $self->{cache}->set($ip, $result) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 26 18 44
303 44 $self->_land($ip, $flight, 1, $result);
304 44 return $result;
305 },
306 78 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
307 78 my $error = shift;
308 78 $self->_land($ip, $flight, 0, $error);
309 78 return Mojo::Promise->reject($error);
310 },
311 122 );
312 }
313  
314 # Concurrent misses for one address share ONE request. The first miss boards the
315 # address, and every miss after it, a batch's included, waits for that request
316 # instead. The request runs under the options of the call that led it; a failure
317 # reaches every waiter and is cached for none. Nothing is shared without a cache,
318 # since every lookup is then served. A Mojo::Promise cannot be canceled, so no
319 # waiter is ever left behind by a leader that gave up.
320 27668 ▶sub _board {
Subroutine _board: called CC=2 SCAR=6.9
321 27668 my ($self, $ip) = @_;
322 27668 ▶ return undef unless $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cache"} 27578 90 27668
323 90 my $flight = Mojo::Promise->new;
324 # Handled even when no waiter joined, so a failure is not warned about.
325 90 ▶ $flight->catch(sub { });
Subroutine __ANON__: called CC=1 SCAR=0.0
326 90 return $self->{flights}{$ip} = $flight;
327 }
328  
329 # Takes an address off the board and settles its flight for every waiter.
330 27668 ▶sub _land {
Subroutine _land: called CC=5 SCAR=16.7
331 27668 my ($self, $ip, $flight, $ok, $value) = @_;
332 27668 ▶ return unless $flight;
Branch: 1 branch
BranchTrueFalseTotal
unless $flight 27578 90 27668
333 delete $self->{flights}{$ip}
334 90 ▶ if $self->{flights}{$ip} && $self->{flights}{$ip} == $flight;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight 90 0 90
Condition33%1/3
$self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight
!ll&&!rl&&r
0090
MC/DC0%0/2
$self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight
$self->{"flights"}{$ip} $self->{"flights"}{$ip} == $flight
Truth table33%1/3
A and B
A= $self->{"flights"}{$ip}B= $self->{"flights"}{$ip} == $flight
ABresult
0X0
100
111
335 90 ▶ return $ok ? $flight->resolve($value) : $flight->reject($value);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 42 48 90
336 }
337  
338 # What every database says about one AS. Not cached and not batched: only an
339 # address is.
340 43 ▶sub lookup_asn {
Subroutine lookup_asn: called CC=1 SCAR=0.0
Pod documented
341 43 my $self = shift;
342 43 $self->_assert_blocking_ok('lookup_asn');
343 42 return $self->_wait($self->lookup_asn_p(@_));
344 }
345  
346 43 ▶sub lookup_asn_p {
Subroutine lookup_asn_p: called CC=4 SCAR=13.9
Pod undocumented
347 43 my ($self, $asn, %options) = @_;
348 43 ▶ Carp::croak('lookup_asn: expected an AS number') if !defined $asn || !length $asn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $asn and length $asn 2 41 43
Condition100%3/3
defined $asn and length $asn
!ll&&!rl&&r
1141
MC/DC100%2/2
defined $asn and length $asn
defined $asn length $asn
Truth table100%3/3
A and B
A= defined $asnB= length $asn
ABresult
0X0
100
111
349 41 $self->_check_options('lookup_asn', \%options, 'retries', 'timeout');
350  
351 31 my $url = $self->_url('/asn/' . _path_segment($asn));
352 31 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 30 31
353 44 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $ASN_LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
354 31 ▶ ->then(sub { InternetData::AsnResult->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
355 }
356  
357 # What every database says about one domain, neither cached nor batched.
358 41 ▶sub lookup_domain {
Subroutine lookup_domain: called CC=1 SCAR=0.0
Pod documented
359 41 my $self = shift;
360 41 $self->_assert_blocking_ok('lookup_domain');
361 40 return $self->_wait($self->lookup_domain_p(@_));
362 }
363  
364 41 ▶sub lookup_domain_p {
Subroutine lookup_domain_p: called CC=4 SCAR=13.9
Pod undocumented
365 41 my ($self, $domain, %options) = @_;
366 41 ▶ Carp::croak('lookup_domain: expected a domain') if !defined $domain || !length $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain and length $domain 2 39 41
Condition100%3/3
defined $domain and length $domain
!ll&&!rl&&r
1139
MC/DC100%2/2
defined $domain and length $domain
defined $domain length $domain
Truth table100%3/3
A and B
A= defined $domainB= length $domain
ABresult
0X0
100
111
367 39 $self->_check_options('lookup_domain', \%options, 'retries', 'timeout');
368  
369 29 my $url = $self->_url('/domain/' . _path_segment($domain));
370 29 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 28 29
371 35 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $DOMAIN_LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
372 29 ▶ ->then(sub { InternetData::DomainResult->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
373 }
374  
375 # The address our edge observed this client calling from. Deliberately not
376 # cached: the cache is keyed by address, and which address this is IS the
377 # question.
378 26 ▶sub my_ip {
Subroutine my_ip: called CC=1 SCAR=0.0
Pod documented
379 26 my $self = shift;
380 26 $self->_assert_blocking_ok('my_ip');
381 25 return $self->_wait($self->my_ip_p(@_));
382 }
383  
384 25 ▶sub my_ip_p {
Subroutine my_ip_p: called CC=2 SCAR=6.9
Pod undocumented
385 25 my ($self, %options) = @_;
386 25 $self->_check_options('my_ip', \%options, 'retries', 'timeout');
387  
388 15 my $url = $self->_url('/myip');
389 15 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 14 15
390 18 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
391 15 ▶ ->then(sub { InternetData::Result->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
392 }
393  
394 # What this key is entitled to and what it has spent. Deliberately not cached:
395 # the whole point is the number, and a cached one is wrong within seconds.
396 52 ▶sub my_entitlement {
Subroutine my_entitlement: called CC=1 SCAR=0.0
Pod documented
397 52 my $self = shift;
398 52 $self->_assert_blocking_ok('my_entitlement');
399 51 return $self->_wait($self->my_entitlement_p(@_));
400 }
401  
402 51 ▶sub my_entitlement_p {
Subroutine my_entitlement_p: called CC=2 SCAR=6.9
Pod undocumented
403 51 my ($self, %options) = @_;
404 51 $self->_check_options('my_entitlement', \%options, 'retries', 'timeout');
405  
406 41 my $url = $self->_url('/api/v1/entitlement');
407 41 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 40 41
408 41 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $ENTITLEMENT) });
Subroutine __ANON__: called CC=1 SCAR=0.0
409 }
410  
411 # Look up many addresses concurrently, keyed by address rather than positional,
412 # so duplicates collapse to one request and the caller never lines two lists up.
413 48 ▶sub lookup_batch {
Subroutine lookup_batch: called CC=1 SCAR=0.0
Pod documented
414 48 my $self = shift;
415 48 $self->_assert_blocking_ok('lookup_batch');
416 47 return $self->_wait($self->lookup_batch_p(@_));
417 }
418  
419 65 ▶sub lookup_batch_p {
Subroutine lookup_batch_p: called CC=13 SCAR=25.7
Pod undocumented
420 65 my ($self, $ips, %options) = @_;
421 65 ▶ Carp::croak('lookup_batch: expected an array reference of addresses')
Branch: 1 branch
BranchTrueFalseTotal
if ref $ips ne "ARRAY" 1 64 65
422 if ref $ips ne 'ARRAY';
423 64 $self->_check_options('lookup_batch', \%options, 'retries', 'concurrency', 'timeout');
424  
425 31 ▶ my $limit = defined $options{concurrency} ? $options{concurrency} : $self->{concurrency};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'concurrency'} ? : 3 28 31
426 31 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 0 31 31
427  
428 # Bogons are answered locally and cached answers are reused; everything else
429 # goes to the batch endpoint in chunks of up to 1000 addresses, with at most
430 # `limit` chunks in flight. An IPv4-mapped address is sent as the address it
431 # carries, once however many of its spellings were asked, and answered under
432 # each one asked.
433 31 ▶ my @asked = grep { defined && length } @$ips;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0027548
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
434 31 my %seen;
435 31 my @unique = grep { !$seen{$_}++ } map { InternetData::Bogon::unmapped($_) } @asked;
436 31 my %answers;
437 my @pending;
438 31 my %joined;
439 31 for my $ip (@unique) {
440 27547 ▶ if (InternetData::Bogon::is_bogon($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if (InternetData::Bogon::is_bogon($ip)) 2 27545 27547
441 2 $answers{$ip} = InternetData::Bogon::bogon_result($ip);
442 2 next;
443 }
444 27545 ▶ my $hit = $self->{cache} ? $self->{cache}->get($ip) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cache'} ? : 24 27521 27545
445 27545 ▶ if ($hit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit) 1 27544 27545
446 1 $answers{$ip} = $hit;
447 1 next;
448 }
449 # Already in flight, for a lookup or another batch: this batch takes
450 # that request's answer rather than sending the address again.
451 27544 ▶ if ($self->{cache} && $self->{flights}{$ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} and $self->{"flights"}{$ip}) 1 27543 27544
Condition100%3/3
$self->{"cache"} and $self->{"flights"}{$ip}
!ll&&!rl&&r
27521221
MC/DC100%2/2
$self->{"cache"} and $self->{"flights"}{$ip}
$self->{"cache"} $self->{"flights"}{$ip}
Truth table100%3/3
A and B
A= $self->{"cache"}B= $self->{"flights"}{$ip}
ABresult
0X0
100
111
452 1 $joined{$ip} = $self->{flights}{$ip};
453 1 next;
454 }
455 27543 push @pending, $ip;
456 }
457 # Boarded before any chunk is built, so a lookup of one of these arriving
458 # meanwhile waits for this batch's answer.
459 31 my %boarded = map { ($_ => $self->_board($_)) } @pending;
460 31 my @queue;
461 31 ▶ push @queue, [splice @pending, 0, BATCH_MAX] while @pending;
Branch: 1 branch
BranchTrueFalseTotal
@pending and do { push @queue, [splice(@pending, 0, 1000)] } 57 31 88
462  
463 my $batch = {
464 queue => \@queue, answers => \%answers, active => 0, limit => $limit,
465 31 retries => $retries, timeout => $options{timeout}, promise => Mojo::Promise->new,
466 boarded => \%boarded,
467 };
468 31 $self->_dispatch($batch);
469 my $done = %joined ? Mojo::Promise->all(
470 $batch->{promise},
471 map {
472 1 my $ip = $_;
473 $joined{$ip}->then(
474 1 ▶ sub { $answers{$ip} = shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
475 0 ▶ sub { $answers{$ip} = InternetData::Error->wrap(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
476 1 );
477 } sort keys %joined,
478 31 ▶ ) : $batch->{promise};
Branch: 1 branch
BranchTrueFalseTotal
%joined ? : 1 30 31
479 31 ▶ return $done->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
480 31 return { map { ($_ => $answers{ InternetData::Bogon::unmapped($_) }) } @asked };
481 31 });
482 }
483  
484 # The licensed database downloads. Built per call rather than held, so the
485 # client and its sub-API never form a reference cycle.
486 162 ▶sub database {
Subroutine database: called CC=1 SCAR=0.0
Pod documented
487 162 my ($self) = @_;
488 162 return InternetData::Database->_new($self);
489 }
490  
491 # The OAuth device-flow sign-in, built per call for the same reason.
492 167 ▶sub oauth {
Subroutine oauth: called CC=1 SCAR=0.0
Pod documented
493 167 my ($self) = @_;
494 167 return InternetData::Oauth->_new($self);
495 }
496  
497 # Keeps exactly `limit` chunks in flight: every answer starts the next one, so a
498 # per-call concurrency is a real ceiling rather than an option that was accepted
499 # and ignored. A failing address carries its error as its value, so one bad
500 # entry cannot lose the rest of the answers: the API reports a per-entry failure
501 # with the status the single lookup would have answered, and a chunk that fails
502 # as a whole marks every address in it.
503 88 ▶sub _dispatch {
Subroutine _dispatch: called CC=5 SCAR=16.1
504 88 my ($self, $batch) = @_;
505 88 ▶ if (!@{$batch->{queue}} && !$batch->{active}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$batch->{"queue"};} or $batch->{"active"}) 31 57 88
Condition100%3/3
@{$batch->{"queue"};} or $batch->{"active"}
l!l&&r!l&&!r
48931
MC/DC100%2/2
@{$batch->{"queue"};} or $batch->{"active"}
@{$batch->{"queue"};} $batch->{"active"}
Truth table100%3/3
A or B
A= @{$batch->{"queue"};}B= $batch->{"active"}
ABresult
1X1
011
000
506 31 $batch->{promise}->resolve($batch->{answers});
507 31 return;
508 }
509 57 ▶ while ($batch->{active} < $batch->{limit} && @{$batch->{queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch->{"active"} < $batch->{"limit"} and @{$batch->{"queue"};}) 57 57 114
Condition100%3/3
$batch->{"active"} < $batch->{"limit"} and @{$batch->{"queue"};}
!ll&&!rl&&r
223557
MC/DC100%2/2
$batch->{"active"} < $batch->{"limit"} and @{$batch->{"queue"};}
$batch->{"active"} < $batch->{"limit"} @{$batch->{"queue"};}
Truth table100%3/3
A and B
A= $batch->{"active"} < $batch->{"limit"}B= @{$batch->{"queue"};}
ABresult
0X0
100
111
510 57 my $chunk = shift @{$batch->{queue}};
511 57 $batch->{active}++;
512 57 ▶ $self->_lookup_chunk_p($chunk, $batch->{retries}, $batch->{timeout})->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
513 57 my $answers = shift;
514 57 for my $ip (keys %$answers) {
515 27543 my $answer = $answers->{$ip};
516 27543 $batch->{answers}{$ip} = $answer;
517 27543 ▶ my $failed = Scalar::Util::blessed($answer) && $answer->isa('InternetData::Error');
Condition66%2/3
builtin::blessed($answer) && $answer->isa('InternetData::Error')
!ll&&!rl&&r
02752320
MC/DC50%1/2
builtin::blessed($answer) && $answer->isa('InternetData::Error')
builtin::blessed($answer) $answer->isa('InternetData::Error')
Truth table66%2/3
A && B
A= builtin::blessed($answer)B= $answer->isa('InternetData::Error')
ABresult
0X0
100
111
518 27543 $self->_land($ip, $batch->{boarded}{$ip}, !$failed, $answer);
519 }
520 57 $self->_settled($batch);
521 57 });
522 }
523 }
524  
525 # One POST /batch, mapped back onto the addresses it was asked about. A
526 # chunk-level failure - the call refused, the transport failing, the retries
527 # exhausted - becomes every address's error, exactly as it would have been had
528 # each been looked up alone. Never rejects: the failure is the value.
529 57 ▶sub _lookup_chunk_p {
Subroutine _lookup_chunk_p: called CC=1 SCAR=0.0
530 57 my ($self, $chunk, $retries, $timeout) = @_;
531 57 my $url = $self->_url('/batch');
532 58 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, { ips => $chunk }, $timeout, undef, $BATCH) })->then(
Subroutine __ANON__: called CC=1 SCAR=0.0
533 47 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
534 47 my $body = shift;
535 47 my $results = $body->{results};
536 47 my $errors = $body->{errors};
537 47 my %answers;
538 # An entry is judged alone: one that is not an answer is that
539 # address's error, and the rest of the chunk keeps its answers.
540 47 for my $ip (@$chunk) {
541 27532 ▶ if (defined $results->{$ip} && !defined _nonconforming($LOOKUP, $results->{$ip}, [])) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $results->{$ip} and not defined _nonconforming($InternetData::LOOKUP, $results->{$ip}, scalar [])) { } 27523 9 27532
elsif (defined $errors->{$ip} and not defined _nonconforming($InternetData::BATCH_ERROR, $errors->{$ip}, scalar [])) { } 4 5 9
Condition100%3/3
defined $results->{$ip} and not defined _nonconforming($InternetData::LOOKUP, $results->{$ip}, scalar [])
!ll&&!rl&&r
6327523
MC/DC100%2/2
defined $results->{$ip} and not defined _nonconforming($InternetData::LOOKUP, $results->{$ip}, scalar [])
defined $results->{$ip} not defined _nonconforming($InternetData::LOOKUP, $results->{$ip}, scalar [])
Truth table100%3/3
A and not(B)
A= defined $results->{$ip}B= not defined _nonconforming($InternetData::LOOKUP, $results->{$ip}, scalar [])
ABresult
0X0
100
111
542 27523 my $result = InternetData::Result->from_wire($results->{$ip});
543 27523 ▶ $self->{cache}->set($ip, $result) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 16 27507 27523
544 27523 $answers{$ip} = $result;
545 }
546 ▶ elsif (defined $errors->{$ip} && !defined _nonconforming($BATCH_ERROR, $errors->{$ip}, [])) {
Condition100%3/3
defined $errors->{$ip} and not defined _nonconforming($InternetData::BATCH_ERROR, $errors->{$ip}, scalar [])
!ll&&!rl&&r
324
MC/DC100%2/2
defined $errors->{$ip} and not defined _nonconforming($InternetData::BATCH_ERROR, $errors->{$ip}, scalar [])
defined $errors->{$ip} not defined _nonconforming($InternetData::BATCH_ERROR, $errors->{$ip}, scalar [])
Truth table100%3/3
A and not(B)
A= defined $errors->{$ip}B= not defined _nonconforming($InternetData::BATCH_ERROR, $errors->{$ip}, scalar [])
ABresult
0X0
100
111
547 $answers{$ip} = InternetData::Error->from_entry(
548 $errors->{$ip}{status}, $errors->{$ip}{error},
549 4 );
550 }
551 else {
552 5 $answers{$ip} = InternetData::Error->new(
553 kind => 'server_error', status => 200,
554 message => "the batch answer did not include $ip",
555 );
556 }
557 }
558 47 return \%answers;
559 },
560 10 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
561 10 my $error = InternetData::Error->wrap(shift);
562 10 return { map { $_ => $error } @$chunk };
563 },
564 57 );
565 }
566  
567 # Releasing the slot happens in the SAME handler that records the answer. A
568 # second `then` would leave a gap in which a dying handler strands the slot, and
569 # a batch that never releases one never finishes.
570 57 ▶sub _settled {
Subroutine _settled: called CC=1 SCAR=0.0
571 57 my ($self, $batch) = @_;
572 57 $batch->{active}--;
573 57 $self->_dispatch($batch);
574 }
575  
576 # One file transfer. Every chunk is handed to $on_chunk and none is kept, so a
577 # body costs the same in memory whether it is 264 bytes or 5.34 GiB. Resolves
578 # with the number of bytes handed over.
579 17 ▶sub _stream_p {
Subroutine _stream_p: called CC=1 SCAR=0.0
580 17 my ($self, $url, $on_chunk) = @_;
581 # Built here rather than through _get_p, which is the only place the API key
582 # is ever attached: the presigned link authorizes itself, so carrying the key
583 # would hand it to a host with no business holding it. Object storage answers
584 # 400 to a presigned GET that also carries an Authorization header, so this
585 # is not merely a leak - it breaks the download too.
586 17 my $tx = $self->{ua}->build_tx(GET => $url);
587  
588 # Mojo asks for gzip on every request it builds. A published file is already
589 # compressed, so the only thing that would buy is a Content-Length counting
590 # bytes that never reach the sink, and that length is the only evidence the
591 # transfer arrived whole.
592 17 $tx->req->headers->remove('Accept-Encoding');
593 # Mojo aborts a response past max_message_size, counting every byte it parses
594 # whether or not it keeps any of them. Mojo::Message::Response defaults to
595 # 2 GiB, which the catalog is already past, and MOJO_MAX_MESSAGE_SIZE lowers
596 # it for every response in the process, on a machine whose environment we do
597 # not own.
598 17 $tx->res->max_message_size(0);
599  
600 17 my $received = 0;
601 # Unsubscribing Mojo's own reader is what stops the body being collected into
602 # the message. The subscriber hangs off the transaction, so holding the
603 # transaction strongly inside it would be a cycle the interpreter never
604 # collects; the user agent keeps it alive for as long as bytes are arriving.
605 17 my $weak = $tx;
606 17 Scalar::Util::weaken($weak);
607 211 ▶ $tx->res->content->unsubscribe('read')->on(read => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
608 211 my (undef, $bytes) = @_;
609 # An error body is neither written out nor held: the status is what
610 # separates a lapsed link from a refused one, and nothing bounds the size
611 # of what a storage host puts in the body of a refusal.
612 211 ▶ return unless $weak && ($weak->res->code || 0) == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and ($weak->res->code || 0) == 200 2 209 211
Condition60%3/5
$weak and ($weak->res->code || 0) == 200
!ll&&!rl&&r
02209
$weak->res->code || 0
l!l
2110
MC/DC0%0/2
$weak and ($weak->res->code || 0) == 200
$weak $weak->res->code
Truth table33%1/3
A and B
A= $weakB= $weak->res->code
ABresult
0X0
100
111
613 209 $received += length $bytes;
614 209 $on_chunk->($bytes);
615 17 });
616  
617 16 ▶ return $self->_start_p($tx, 0)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
618 16 my $res = shift->res;
619 # No body to read: the handler above kept nothing that was not a 200,
620 # because nothing bounds the size of what a storage host puts in a
621 # refusal. So the message names where the refusal came from, and the
622 # status still decides the kind - which is what says whether the link
623 # lapsed or was never good.
624 16 ▶ die InternetData::Error->from_response(
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 2 14 16
625 $res->code, $res->headers, undef,
626 'object storage refused the download link with status ' . $res->code,
627 ) unless $res->code == 200;
628  
629 # Before Mojolicious 9.54 a body that stops early reaches Perl as an
630 # ordinary end of stream: the status was 200 and Mojo reports no error,
631 # so without this a half transfer is a short file nobody notices.
632 14 my $short = _short_transfer($res, $received);
633 14 ▶ die $short if $short;
Branch: 1 branch
BranchTrueFalseTotal
if $short 0 14 14
634 14 return $received;
635 1 ▶ }, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
636 1 my $error = shift;
637 # From 9.54 the same body rejects as `Premature connection close`, which
638 # says nothing of how far it got, so the lengths are added to it.
639 1 my $short = _short_transfer($tx->res, $received, $error);
640 1 ▶ die $short // $error;
Condition33%1/3
$short // $error
l!l&&r!l&&!r
100
MC/DC0%0/2
$short // $error
$short $error
Truth table33%1/3
A // B
A= $shortB= $error
ABresult
1X1
011
000
641 17 });
642 }
643  
644 # The error for a 200 whose body stopped short of the length it declared, or
645 # undef when it did not. $cause is what Mojo rejected it with, when it did.
646 15 ▶sub _short_transfer {
Subroutine _short_transfer: called CC=8 SCAR=23.7
647 15 my ($res, $received, $cause) = @_;
648 15 my $declared = $res->headers->content_length;
649 return undef
650 15 ▶ unless ($res->code || 0) == 200 && defined $declared && length $declared && $declared != $received;
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->code || 0) == 200 and defined $declared and length $declared and $declared != $received 14 1 15
Condition45%5/11
($res->code || 0) == 200 and defined $declared and length $declared and $declared != $received
!ll&&!rl&&r
0141
($res->code || 0) == 200 and defined $declared and length $declared
!ll&&!rl&&r
0015
($res->code || 0) == 200 and defined $declared
!ll&&!rl&&r
0015
$res->code || 0
l!l
150
MC/DC25%1/4
($res->code || 0) == 200 and defined $declared and length $declared and $declared != $received
$res->code defined $declared length $declared $declared != $received
Truth table40%2/5
A and B and C and D
A= $res->codeB= defined $declaredC= length $declaredD= $declared != $received
ABCDresult
0XXX0
10XX0
110X0
11100
11111
651 1 my $message = "the transfer ended after $received of $declared bytes";
652 1 ▶ $message .= ': ' . (ref $cause ? $cause->message : $cause) if defined $cause;
Branch: 2 branches
BranchTrueFalseTotal
if defined $cause 1 0 1
ref $cause ? : 1 0 1
653 1 return InternetData::Error->new(kind => 'network', message => $message);
654 }
655  
656 # request_timeout bounds the WHOLE response and Mojo::UserAgent has no
657 # per-transaction form of it, yet a call may bring its own bound and a transfer
658 # must have none (0): 30 seconds is right for a metadata call and wrong for a
659 # gigabyte. So it is set only across the hand-over, where start_p arms the timer
660 # synchronously - and set EXPLICITLY each time, because Mojo runs a loop tick
661 # inside start_p when the loop is idle, and a retry started in that tick would
662 # otherwise inherit this call's bound.
663 #
664 # A rejection is a plain string when the request never got far enough to have a
665 # status, which is exactly the transport failure the retry rule treats as worth
666 # another attempt.
667 1173 ▶sub _start_p {
Subroutine _start_p: called CC=3 SCAR=11.0
668 1173 my ($self, $tx, $timeout) = @_;
669 1173 my $ua = $self->{ua};
670 1173 my $bound = $ua->request_timeout;
671 # A number, for the reason new gives: a per-call '0.0' is otherwise a timer
672 # that fires at once.
673 1173 ▶ $ua->request_timeout(defined $timeout ? 0 + $timeout : $self->{timeout});
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 76 1097 1173
674 1173 my $promise = eval { $ua->start_p($tx) };
675 1173 my $failed = $@;
676 1173 $ua->request_timeout($bound);
677 1173 ▶ die InternetData::Error->wrap($failed) unless $promise;
Branch: 1 branch
BranchTrueFalseTotal
unless $promise 6 1167 1173
678 52 ▶ return $promise->catch(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
679 52 die InternetData::Error->new(kind => 'network', message => "$_[0]");
680 1167 });
681 }
682  
683 # Recurses through $self rather than through a self-referential closure, which
684 # in Perl would be a reference cycle the interpreter never collects. $tries
685 # counts the retries already made.
686 1175 ▶sub _retry_p {
Subroutine _retry_p: called CC=2 SCAR=6.9
687 1175 my ($self, $left, $attempt, $may_retry, $tries) = @_;
688 1175 ▶ $tries //= 0;
Condition100%2/2
$tries //= 0
l!l
324851
MC/DC100%1/1
$tries //= 0
$tries
Truth table100%2/2
A
A= $tries
Aresult
11
00
689 879 ▶ return $attempt->()->catch(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
690 879 my $error = InternetData::Error->wrap(shift);
691 # $may_retry, when given, can veto a retry the error alone would allow.
692 879 ▶ die $error if $left <= 0 || !$error->retryable || ($may_retry && !$may_retry->());
Branch: 1 branch
BranchTrueFalseTotal
if $left <= 0 or not $error->retryable or $may_retry and not &$may_retry() 555 324 879
Condition100%9/9
$left <= 0 or not $error->retryable or $may_retry and not &$may_retry()
l!l&&r!l&&!r
5541324
$left <= 0 or not $error->retryable
l!l&&r!l&&!r
49856325
$may_retry and not &$may_retry()
!ll&&!rl&&r
32311
MC/DC100%4/4
$left <= 0 or not $error->retryable or $may_retry and not &$may_retry()
$left <= 0 not $error->retryable $may_retry not &$may_retry()
Truth table100%5/5
A or not(B) or C and not(D)
A= $left <= 0B= not $error->retryableC= $may_retryD= not &$may_retry()
ABCDresult
1XXX1
01XX1
00111
000X0
00100
693 # The wait is a TIMER, never a sleep: this promise may share an event
694 # loop with a Mojolicious application, and sleeping here would stall
695 # every other thing on it.
696 return Mojo::Promise->timer(_retry_delay($error->retry_after, $tries))
697 324 ▶ ->then(sub { $self->_retry_p($left - 1, $attempt, $may_retry, $tries + 1) });
Subroutine __ANON__: called CC=1 SCAR=0.0
698 1175 });
699 }
700  
701 # The server's Retry-After when it gave a usable one, otherwise the backoff:
702 # 250 ms, doubling per retry, capped from the seventh. A Retry-After past
703 # LONGEST_WAIT is waited out on the backoff too, still rate_limited, rather than
704 # holding the call for as long as any server asks.
705 337 ▶sub _retry_delay {
Subroutine _retry_delay: called CC=5 SCAR=16.1
706 337 my ($retry_after, $tries) = @_;
707 337 ▶ return $retry_after
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647" 3 334 337
Condition100%6/6
defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647"
!ll&&!rl&&r
33133
defined $retry_after and $retry_after > 0
!ll&&!rl&&r
321106
MC/DC100%3/3
defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647"
defined $retry_after $retry_after > 0 $retry_after <= "2147483.647"
Truth table100%4/4
A and B and C
A= defined $retry_afterB= $retry_after > 0C= $retry_after <= "2147483.647"
ABCresult
0XX0
10X0
1100
1111
708 if defined $retry_after && $retry_after > 0 && $retry_after <= LONGEST_WAIT;
709 334 ▶ return BACKOFF_BASE * 2**($tries < 6 ? $tries : 6);
Branch: 1 branch
BranchTrueFalseTotal
$tries < 6 ? : 332 2 334
710 }
711  
712 # $body, when given, is POSTed as JSON, and $timeout is the call's own bound, or
713 # undef for the client's. $shape is the whole answer's, and $member, when given,
714 # the one member of it the call returns. An answer without the shape is no answer
715 # at all, so it fails inside the attempt as a retried server_error, like any
716 # other 2xx that cannot be read, rather than reaching the caller as undef.
717 908 ▶sub _json_p {
Subroutine _json_p: called CC=2 SCAR=6.9
718 908 my ($self, $url, $body, $timeout, $member, $shape) = @_;
719 908 ▶ my $sent = defined $body ? $self->_post_p($url, $body, $timeout) : $self->_get_p($url, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 58 850 908
720 879 ▶ return $sent->then(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
721 879 my $res = shift->res;
722 879 ▶ die InternetData::Error->from_response($res->code, $res->headers, $res->json)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 143 736 879
723 unless $res->is_success;
724 736 my $decoded = $res->json;
725 546 ▶ my $unreadable = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
726 546 die InternetData::Error->new(kind => 'server_error', status => $res->code, message => shift);
727 736 };
728 736 ▶ $unreadable->('the API did not answer with a JSON object') unless ref $decoded eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $decoded eq "HASH" 46 690 736
729 690 ▶ return $decoded unless $shape;
Branch: 1 branch
BranchTrueFalseTotal
unless $shape 0 690 690
730 690 my $wrong = _nonconforming($shape, $decoded, []);
731 690 ▶ $unreadable->($wrong) if defined $wrong;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wrong 500 190 690
732 190 ▶ return defined $member ? $decoded->{$member} : $decoded;
Branch: 1 branch
BranchTrueFalseTotal
defined $member ? : 59 131 190
733 904 });
734 }
735  
736 # undef when the value has the shape, else what the API answered wrong, naming
737 # the member by its path. A string is told from a number by the scalar Mojo::JSON
738 # decoded it into, as InternetData::Oauth does, since nothing else in Perl tells
739 # `"7"` from `7`. No list entry or map value may be null.
740 1082145 ▶sub _nonconforming {
Subroutine _nonconforming: called CC=35 SCAR=35.6
741 1082145 my ($shape, $value, $at) = @_;
742 1082145 my ($kind, $inner) = @$shape;
743 1082145 ▶ my $wrong = sub { 'the API answered ' . (@$at ? join('.', @$at) : 'the answer') . ' of the wrong type' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@$at ? : 93 0 93
744 1082145 ▶ return $wrong->() unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 10 1082135 1082145
745 1082135 ▶ return undef if $kind eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "any" 27532 1054603 1082135
746 1054603 ▶ if ($kind eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "string") 526755 527848 1054603
747 526755 ▶ return InternetData::Oauth::_is_string($value) ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
InternetData::Oauth::_is_string($value) ? : 526741 14 526755
748 }
749 527848 ▶ if ($kind eq 'date' || $kind eq 'date-time') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "date" or $kind eq "date-time") 624 527224 527848
Condition100%3/3
$kind eq "date" or $kind eq "date-time"
l!l&&r!l&&!r
61563527224
MC/DC100%2/2
$kind eq "date" or $kind eq "date-time"
$kind eq "date" $kind eq "date-time"
Truth table100%3/3
A or B
A= $kind eq "date"B= $kind eq "date-time"
ABresult
1X1
011
000
750 624 ▶ return InternetData::Oauth::_is_string($value) && _is_rfc3339($kind, $value) ? undef
Branch: 1 branch
BranchTrueFalseTotal
InternetData::Oauth::_is_string($value) && _is_rfc3339($kind, $value) ? : 282 342 624
Condition100%3/3
InternetData::Oauth::_is_string($value) and _is_rfc3339($kind, $value)
!ll&&!rl&&r
2340282
MC/DC100%2/2
InternetData::Oauth::_is_string($value) and _is_rfc3339($kind, $value)
InternetData::Oauth::_is_string($value) _is_rfc3339($kind, $value)
Truth table100%3/3
A and B
A= InternetData::Oauth::_is_string($value)B= _is_rfc3339($kind, $value)
ABresult
0X0
100
111
751 : 'the API answered ' . join('.', @$at) . ' that is no RFC 3339 ' . $kind;
752 }
753 527224 ▶ if ($kind eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "integer") 27952 499272 527224
754 27952 ▶ return !ref $value && !InternetData::Oauth::_is_string($value) && $value =~ /\A-?[0-9]+\z/
Branch: 1 branch
BranchTrueFalseTotal
!(ref $value || InternetData::Oauth::_is_string($value)) && $value =~ /\A-?[0-9]+\z/ ? : 27918 34 27952
Condition83%5/6
not ref $value || InternetData::Oauth::_is_string($value) and $value =~ /\A-?[0-9]+\z/
!ll&&!rl&&r
30427918
ref $value || InternetData::Oauth::_is_string($value)
l!l&&r!l&&!r
03027922
MC/DC66%2/3
not ref $value || InternetData::Oauth::_is_string($value) and $value =~ /\A-?[0-9]+\z/
ref $value InternetData::Oauth::_is_string($value) $value =~ /\A-?[0-9]+\z/
Truth table75%3/4
not(A || B) and C
A= ref $valueB= InternetData::Oauth::_is_string($value)C= $value =~ /\A-?[0-9]+\z/
ABCresult
1XX0
01X0
0000
0011
755 ? undef : $wrong->();
756 }
757 499272 ▶ if ($kind eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "number") 10 499262 499272
758 10 ▶ return !ref $value && !InternetData::Oauth::_is_string($value) && Scalar::Util::looks_like_number($value)
Branch: 1 branch
BranchTrueFalseTotal
!(ref $value || InternetData::Oauth::_is_string($value)) && &Scalar::Util::looks_like_number($value) ? : 8 2 10
Condition66%4/6
not ref $value || InternetData::Oauth::_is_string($value) and &Scalar::Util::looks_like_number($value)
!ll&&!rl&&r
208
ref $value || InternetData::Oauth::_is_string($value)
l!l&&r!l&&!r
028
MC/DC33%1/3
not ref $value || InternetData::Oauth::_is_string($value) and &Scalar::Util::looks_like_number($value)
ref $value InternetData::Oauth::_is_string($value) &Scalar::Util::looks_like_number($value)
Truth table50%2/4
not(A || B) and C
A= ref $valueB= InternetData::Oauth::_is_string($value)C= &Scalar::Util::looks_like_number($value)
ABCresult
1XX0
01X0
0000
0011
759 ? undef : $wrong->();
760 }
761 499262 ▶ if ($kind eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "boolean") 304218 195044 499262
762 304218 ▶ return Scalar::Util::blessed($value) && $value->isa('JSON::PP::Boolean') ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('JSON::PP::Boolean') ? : 304197 21 304218
Condition66%2/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
210304197
MC/DC50%1/2
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
builtin::blessed($value) $value->isa("JSON::PP::Boolean")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
763 }
764 195044 ▶ if ($kind eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "object") 2 195042 195044
765 2 ▶ return ref $value eq 'HASH' ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 0 2 2
766 }
767 195042 ▶ if ($kind eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "list") 524 194518 195042
768 524 ▶ return $wrong->() unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 4 520 524
769 520 for my $i (0 .. $#$value) {
770 485 my $w = _nonconforming($inner, $value->[$i], [@$at, $i]);
771 485 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 326 159 485
772 }
773 194 return undef;
774 }
775 194518 ▶ if ($kind eq 'map_of') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "map_of") 137 194381 194518
776 137 ▶ return $wrong->() unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 2 135 137
777 135 for my $key (sort keys %$value) {
778 27611 my $w = _nonconforming($inner, $value->{$key}, [@$at, $key]);
779 27611 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 12 27599 27611
780 }
781 123 return undef;
782 }
783 194381 ▶ return $wrong->() unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 4 194377 194381
784 194377 for my $field (@$inner) {
785 2516867 my ($name, $member_shape, $required) = @$field;
786 2516867 ▶ if (!defined $value->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value->{$name}) 1491040 1025827 2516867
787 1491040 ▶ return 'the API answered with no ' . join('.', @$at, $name) if $required;
Branch: 1 branch
BranchTrueFalseTotal
if $required 70 1490970 1491040
788 1490970 next;
789 }
790 1025827 my $w = _nonconforming($member_shape, $value->{$name}, [@$at, $name]);
791 1025827 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 824 1025003 1025827
792 }
793 193483 return undef;
794 }
795  
796 # RFC 3339's `full-date`, or its `date-time` with the `T` and `Z` in either case,
797 # an hour below 24, a minute and second below 60 and an offset of at most 23:59,
798 # on a day its month has. ASCII digits and `\z`, since Perl's `\d` takes any
799 # script's digits and `$` a trailing newline.
800 8 my $FULL_DATE = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/;
801 8 my $DATE_TIME = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?
802 (?:[Zz]|[+-]([0-9]{2}):([0-9]{2}))\z/x;
803  
804 622 ▶sub _is_rfc3339 {
Subroutine _is_rfc3339: called CC=10 SCAR=23.0
805 622 my ($kind, $value) = @_;
806 622 ▶ if ($kind eq 'date') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "date") 61 561 622
807 61 ▶ my @date = $value =~ $FULL_DATE or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@date) = $value =~ /$FULL_DATE/ or return 0 33 28 61
808 33 return _is_day(@date);
809 }
810 561 ▶ my ($year, $month, $day, $hour, $minute, $second, $off_hour, $off_minute) = $value =~ $DATE_TIME
Branch: 1 branch
BranchTrueFalseTotal
() = $value =~ /$DATE_TIME/ or return 0 331 230 561
811 or return 0;
812 331 ▶ return _is_day($year, $month, $day) && $hour < 24 && $minute < 60 && $second < 60
Condition100%18/18
_is_day($year, $month, $day) && $hour < 24 && $minute < 60 && $second < 60 && (!defined($off_hour) || $off_hour < 24 && $off_minute < 60)
!ll&&!rl&&r
5220259
_is_day($year, $month, $day) and $hour < 24 and $minute < 60 and $second < 60
!ll&&!rl&&r
4210279
_is_day($year, $month, $day) and $hour < 24 and $minute < 60
!ll&&!rl&&r
3210289
_is_day($year, $month, $day) and $hour < 24
!ll&&!rl&&r
1616299
not defined $off_hour or $off_hour < 24 and $off_minute < 60
l!l&&r!l&&!r
253620
$off_hour < 24 and $off_minute < 60
!ll&&!rl&&r
10106
MC/DC100%7/7
_is_day($year, $month, $day) && $hour < 24 && $minute < 60 && $second < 60 && (!defined($off_hour) || $off_hour < 24 && $off_minute < 60)
_is_day($year, $month, $day) $hour < 24 $minute < 60 $second < 60 not defined $off_hour $off_hour < 24 $off_minute < 60
Truth table100%8/8
A and B and C and D && not(E) or F and G
A= _is_day($year, $month, $day)B= $hour < 24C= $minute < 60D= $second < 60E= not defined $off_hourF= $off_hour < 24G= $off_minute < 60
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
111100X0
11110100
11111XX1
11110111
813 && (!defined $off_hour || ($off_hour < 24 && $off_minute < 60));
814 }
815  
816 364 ▶sub _is_day {
Subroutine _is_day: called CC=7 SCAR=19.5
817 364 my ($year, $month, $day) = @_;
818 364 ▶ return 0 if $month < 1 || $month > 12 || $day < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 or $day < 1 2 362 364
Condition66%4/6
$month < 1 or $month > 12 or $day < 1
l!l&&r!l&&!r
20362
$month < 1 or $month > 12
l!l&&r!l&&!r
02362
MC/DC33%1/3
$month < 1 or $month > 12 or $day < 1
$month < 1 $month > 12 $day < 1
Truth table50%2/4
A or B or C
A= $month < 1B= $month > 12C= $day < 1
ABCresult
1XX1
01X1
0011
0000
819 362 ▶ my $leap = ($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0;
Condition100%6/6
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
l!l&&r!l&&!r
33356
$year % 4 == 0 and $year % 100 != 0
!ll&&!rl&&r
35453
MC/DC100%3/3
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
$year % 4 == 0 $year % 100 != 0 $year % 400 == 0
Truth table80%4/5
A and B || C
A= $year % 4 == 0B= $year % 100 != 0C= $year % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
820 362 ▶ return $day <= (31, $leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$month - 1];
Branch: 1 branch
BranchTrueFalseTotal
$leap ? : 6 356 362
821 }
822  
823 902 ▶sub _get_p {
Subroutine _get_p: called CC=1 SCAR=0.0
824 902 my ($self, $url, $timeout) = @_;
825 902 return $self->_start_p($self->{ua}->build_tx(GET => $url => $self->_headers), $timeout);
826 }
827  
828 # The one request with a body: the batch. Mojo encodes the JSON and sets the
829 # content type.
830 58 ▶sub _post_p {
Subroutine _post_p: called CC=1 SCAR=0.0
831 58 my ($self, $url, $body, $timeout) = @_;
832 58 my $tx = $self->{ua}->build_tx(POST => $url => $self->_headers => json => $body);
833 58 return $self->_start_p($tx, $timeout);
834 }
835  
836 960 ▶sub _headers {
Subroutine _headers: called CC=2 SCAR=6.9
837 960 my ($self) = @_;
838 960 my %headers = (Accept => 'application/json');
839 # One scheme. The v1 endpoints on this same host take `?apikey=` with a
840 # different key vocabulary, so sending a v2 key that way would make it look
841 # plausible on the version it does not belong to, and query strings end up in
842 # logs. A blank key is none at all (see new): that is what an unset
843 # `${{ secrets.X }}` interpolates to, and `Bearer ` is a worse answer than no
844 # header.
845 960 ▶ $headers{Authorization} = "Bearer $self->{api_key}" if defined $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"api_key"} 785 175 960
846 960 return \%headers;
847 }
848  
849 678 ▶sub _url {
Subroutine _url: called CC=2 SCAR=6.9
850 678 my ($self, $path, %query) = @_;
851 678 my $url = Mojo::URL->new($self->{base_url} . $path);
852 678 ▶ $url->query(%query) if %query;
Branch: 1 branch
BranchTrueFalseTotal
if %query 113 565 678
853 678 return $url;
854 }
855  
856 # One path segment as given: its UTF-8 bytes, everything but the unreserved
857 # characters escaped, so a `/`, `?` or `#` in it cannot reach another route.
858 60 ▶sub _path_segment {
Subroutine _path_segment: called CC=1 SCAR=0.0
859 60 my ($value) = @_;
860 60 return Mojo::Util::url_escape(Mojo::Util::encode('UTF-8', $value), '^A-Za-z0-9\-._~');
861 }
862  
863 794 ▶sub _wait {
Subroutine _wait: called CC=2 SCAR=6.9
864 794 my ($self, $promise) = @_;
865 794 my ($value, $error, $failed);
866 794 ▶ $promise->then(sub { $value = shift }, sub { ($error, $failed) = (shift, 1) })->wait;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
867 794 ▶ die $error if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 561 233 794
868 233 return $value;
869 }
870  
871 # Checked BEFORE the promise is built, not after: Mojo::UserAgent starts a
872 # transaction as soon as one is created, so croaking later would still have spent
873 # a request from the caller's allowance.
874 973 ▶sub _assert_blocking_ok {
Subroutine _assert_blocking_ok: called CC=2 SCAR=6.9
875 973 my ($self, $method) = @_;
876 973 ▶ return unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 966 7 973
877 7 Carp::croak(
878 "InternetData::$method cannot block inside a running Mojo::IOLoop; "
879 . "call ${method}_p instead, which returns a Mojo::Promise"
880 );
881 }
882  
883 1059 ▶sub _check_options {
Subroutine _check_options: called CC=9 SCAR=22.0
884 1059 my ($self, $method, $options, @allowed) = @_;
885 1059 my %allowed = map { $_ => 1 } @allowed;
886 1059 my @unknown = sort grep { !$allowed{$_} } keys %$options;
887 1059 ▶ Carp::croak("InternetData::$method: unknown option(s): @unknown") if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 13 1046 1059
888 # A negative or non-numeric bound would fire at once and fail the very call
889 # it was meant to protect.
890 1046 my $timeout = $options->{timeout};
891 1046 ▶ Carp::croak("InternetData::$method: timeout must be a number of seconds, 0 or more")
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0 19 1027 1046
Condition100%6/6
defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0
!ll&&!rl&&r
9695819
&Scalar::Util::looks_like_number($timeout) && $timeout >= 0
!ll&&!rl&&r
91058
MC/DC100%3/3
defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0
defined $timeout &Scalar::Util::looks_like_number($timeout) $timeout >= 0
Truth table100%4/4
A and not(B && C)
A= defined $timeoutB= &Scalar::Util::looks_like_number($timeout)C= $timeout >= 0
ABCresult
0XX0
1110
10X1
1101
892 if defined $timeout && !(Scalar::Util::looks_like_number($timeout) && $timeout >= 0);
893 Carp::croak("InternetData::$method: retries must be a whole number, 0 or more")
894 1027 ▶ if defined $options->{retries} && !_is_count($options->{retries}, 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"retries"} and not _is_count($options->{"retries"}, 0) 98 929 1027
Condition100%3/3
defined $options->{"retries"} and not _is_count($options->{"retries"}, 0)
!ll&&!rl&&r
921898
MC/DC100%2/2
defined $options->{"retries"} and not _is_count($options->{"retries"}, 0)
defined $options->{"retries"} not _is_count($options->{"retries"}, 0)
Truth table100%3/3
A and not(B)
A= defined $options->{"retries"}B= not _is_count($options->{"retries"}, 0)
ABresult
0X0
100
111
895 Carp::croak("InternetData::$method: concurrency must be a whole number, 1 or more")
896 929 ▶ if defined $options->{concurrency} && !_is_count($options->{concurrency}, 1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1) 16 913 929
Condition100%3/3
defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1)
!ll&&!rl&&r
910316
MC/DC100%2/2
defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1)
defined $options->{"concurrency"} not _is_count($options->{"concurrency"}, 1)
Truth table100%3/3
A and not(B)
A= defined $options->{"concurrency"}B= not _is_count($options->{"concurrency"}, 1)
ABresult
0X0
100
111
897 }
898  
899 # A whole number from $least, or Inf for no limit. NaN compares false with
900 # anything, so a NaN count retried a failing call for good and a NaN concurrency
901 # put no chunk in flight, spinning the loop forever; a word reads as 0, and 1.5
902 # as two retries.
903 1068 ▶sub _is_count {
Subroutine _is_count: called CC=4 SCAR=13.9
904 1068 my ($n, $least) = @_;
905 1068 ▶ return !ref $n && Scalar::Util::looks_like_number($n) && $n >= $least && $n == int $n;
Condition100%9/9
!ref($n) && &Scalar::Util::looks_like_number($n) && $n >= $least && $n == int $n
!ll&&!rl&&r
11318937
not ref $n and &Scalar::Util::looks_like_number($n) and $n >= $least
!ll&&!rl&&r
5459955
not ref $n and &Scalar::Util::looks_like_number($n)
!ll&&!rl&&r
18361014
MC/DC100%4/4
!ref($n) && &Scalar::Util::looks_like_number($n) && $n >= $least && $n == int $n
not ref $n &Scalar::Util::looks_like_number($n) $n >= $least $n == int $n
Truth table100%5/5
not(A) and B and C && D
A= not ref $nB= &Scalar::Util::looks_like_number($n)C= $n >= $leastD= $n == int $n
ABCDresult
0XXX0
10XX0
110X0
11100
11111
906 }
907  
908 450 ▶sub _base_url {
Subroutine _base_url: called CC=3 SCAR=11.0
909 450 my ($url) = @_;
910 450 ▶ $url = DEFAULT_BASE_URL unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 10 440 450
Condition66%2/3
defined $url and length $url
!ll&&!rl&&r
100440
MC/DC50%1/2
defined $url and length $url
defined $url length $url
Truth table66%2/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
911 450 $url =~ s{/+\z}{};
912 450 return $url;
913 }
914  
915 8 1;
916  
917 __END__
↑ Summary InternetData/Oauth.pm »