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.16.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 [cap_reached => ['boolean']],
132 ]];
133 my $AS_PREFIX = [fields => [
134 [prefix => ['string'], 1], [peer_count => ['integer'], 1],
135 8 (map { [$_ => ['string'], 1] } qw(rpki_status first_seen country)),
136 [size => ['integer'], 1], [other_origins => [list => ['integer']], 1],
137 ]];
138 our $ASN_LOOKUP = [fields => [
139 40 [asn => ['integer'], 1], (map { [$_ => ['string'], 1] } qw(name country registry allocated domain)),
140 [type => ['string']],
141 48 (map { [$_ => ['integer']] } qw(num_ips num_prefixes4 num_prefixes6 rpki_valid rpki_invalid rpki_not_found)),
142 [whois => [map_of => ['string']]], [prefixes => [list => $AS_PREFIX]],
143 [rank => ['integer']], [tier1 => ['boolean']],
144 8 (map { (["num_$_" => ['integer']], [$_ => [list => ['integer']]]) } qw(upstreams peers downstreams)),
145 [hosted_domains => [fields => [[total => ['integer'], 1], [domains => [list => ['string']]]]]],
146 [trial_expired => ['boolean']],
147 [cap_reached => ['boolean']],
148 ]];
149 8 my $DNS_ADDRESS = [fields => [map { [$_ => ['string'], 1] } qw(ip last_seen)]];
150 my $DOMAIN_DNS = [fields => [
151 16 (map { [$_ => [list => $DNS_ADDRESS], 1] } qw(a aaaa)),
152 16 [mx => [list => [fields => [[priority => ['integer'], 1], map { [$_ => ['string'], 1] } qw(host last_seen)]]], 1],
153 16 [ns => [list => [fields => [map { [$_ => ['string'], 1] } qw(host last_seen)]]], 1],
154 8 [cname => [list => [fields => [map { [$_ => ['string'], 1] } qw(target last_seen)]]], 1],
155 ]];
156 my $DOMAIN_IP_RANGES = [fields => [
157 16 (map { [$_ => ['string'], 1] } qw(domain redirects_to)), (map { [$_ => ['integer'], 1] } qw(total ipv4)),
158 8 [ranges => [list => [fields => [map { [$_ => ['string'], 1] } qw(range name country)]]], 1],
159 ]];
160 our $DOMAIN_LOOKUP = [fields => [
161 [domain => ['string'], 1], [is_registered => ['boolean'], 1], [is_disposable => ['boolean']],
162 40 (map { [$_ => ['boolean'], 1] } qw(has_a has_aaaa has_mx has_ns has_cname)),
163 [dns => $DOMAIN_DNS], [ip_ranges => $DOMAIN_IP_RANGES],
164 [disposable => [fields => [
165 [confidence => ['string']], [providers => [list => ['string']]],
166 8 (map { [$_ => ['string']] } qw(first_seen last_seen)),
167 ]]],
168 [trial_expired => ['boolean']],
169 [cap_reached => ['boolean']],
170 ]];
171 8 our $BATCH_ERROR = [fields => [[status => ['integer'], 1], [error => ['string'], 1]]];
172 # Each entry is judged alone, by _lookup_chunk_p.
173 8 our $BATCH = [fields => [[results => [map_of => ['any']], 1], [errors => [map_of => ['any']], 1]]];
174 8 our $ENTITLEMENT = [fields => [
175 [org_id => ['string'], 1],
176 [apikey => [fields => [
177 [id => ['string'], 1], [expires => ['date-time']], [allowed_cidrs => [list => ['string']], 1],
178 ]], 1],
179 [plan => [fields => [[key => ['string'], 1], [name => ['string'], 1], [tier => ['string'], 1]]], 1],
180 [usage => [fields => [
181 [requests => ['integer'], 1], [quota => ['integer'], 1], [hard_limit => ['integer']],
182 [window_start => ['date-time'], 1], [window_end => ['date-time'], 1],
183 ]], 1],
184 ]];
185  
186 480 ▶sub new {
Subroutine new: called CC=19 SCAR=29.4
Pod documented
187 480 my ($class, %args) = @_;
188 480 my @unknown = sort grep { !$OPTIONS{$_} } keys %args;
189 480 ▶ Carp::croak("InternetData->new: unknown option(s): @unknown") if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 479 480
190  
191 479 ▶ my $concurrency = defined $args{concurrency} ? $args{concurrency} : 8;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'concurrency'} ? : 11 468 479
192 479 ▶ my $retries = defined $args{retries} ? $args{retries} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'retries'} ? : 229 250 479
193 479 ▶ my $cache_size = defined $args{cache_size} ? $args{cache_size} : 10_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'cache_size'} ? : 32 447 479
194 479 ▶ 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 470 479
195 470 ▶ 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 462 470
196 # Mojo arms a negative or non-numeric bound as a timer that fires at once, so
197 # every call would fail as a network error after reaching the server.
198 462 ▶ my $timeout = defined $args{timeout} ? $args{timeout} : 30;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'timeout'} ? : 46 416 462
199 462 ▶ 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 460 462
Condition100%3/3
&Scalar::Util::looks_like_number($timeout) and $timeout >= 0
!ll&&!rl&&r
11460
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
200 unless Scalar::Util::looks_like_number($timeout) && $timeout >= 0;
201  
202 # Trimmed, so a key read from a file loses its newline, and blanks alone are
203 # no key. Mojo refuses a CR or LF in a header with a raw string from every
204 # call and sends any other control character, so one left inside is refused
205 # here, naming the cause and not the key.
206 460 my $api_key = $args{api_key};
207 460 ▶ if (defined $api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api_key) 216 244 460
208 216 $api_key =~ s/\A\s+|\s+\z//g;
209 216 ▶ $api_key = undef unless length $api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $api_key 4 212 216
210 }
211 460 ▶ 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 455 460
Condition100%3/3
defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/
!ll&&!rl&&r
2482075
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
212 if defined $api_key && $api_key =~ /[\x00-\x1f\x7f]/;
213 # Mojo writes a header as bytes, so a character past U+00FF dies in the
214 # reactor and every call fails after its whole timeout with nothing sent.
215 455 ▶ 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 453 455
Condition100%3/3
defined $api_key and $api_key =~ /[^\x00-\xff]/
!ll&&!rl&&r
2482052
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
216 if defined $api_key && $api_key =~ /[^\x00-\xff]/;
217  
218 my $self = bless {
219 api_key => $api_key,
220 base_url => _base_url($args{base_url}),
221 concurrency => 0 + $concurrency,
222 retries => 0 + $retries,
223 # Held as a number: Mojo tests the bound for truth before arming it, and a
224 # string such as '0.0' is true, so it would be a timer that fires at once
225 # rather than no bound.
226 timeout => 0 + $timeout,
227 cache => $cache_size > 0 ? InternetData::Cache->new(
228 max => $cache_size,
229 ttl => defined $args{cache_ttl} ? $args{cache_ttl} : 3600,
230 ) : undef,
231 # The addresses with a request in flight, each a promise its waiters
232 # take the answer from (see _board).
233 flights => {},
234 453 ▶ ua => $args{ua} || Mojo::UserAgent->new,
Branch: 2 branches
BranchTrueFalseTotal
$cache_size > 0 ? : 422 31 453
defined $args{'cache_ttl'} ? : 1 421 422
Condition66%2/3
$args{'ua'} || 'Mojo::UserAgent'->new
l!l&&r!l&&!r
84450
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
235 }, $class;
236  
237 # Mojo::UserAgent does not follow redirects by default, but MOJO_MAX_REDIRECTS
238 # in the environment turns that on for every agent in the process. Setting it
239 # here is what stops a download's 302 being chased into a multi-gigabyte
240 # transfer, on a machine whose environment we do not own.
241 453 $self->{ua}->max_redirects(0);
242 453 $self->{ua}->request_timeout($self->{timeout});
243 453 $self->{ua}->transactor->name("internetdata-perl/$VERSION");
244 453 return $self;
245 }
246  
247 # Both a method and an exportable function, so the address is the LAST argument
248 # either way: is_bogon($ip) and $client->is_bogon($ip) reach the same code.
249 111 ▶sub is_bogon {
Subroutine is_bogon: called CC=3 SCAR=12.5
Pod documented
250 111 my $ip = pop;
251 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
252 111 return InternetData::Bogon::is_bogon($ip);
253 }
254  
255 # What every database says about one address. A bogon is answered locally and
256 # never reaches the network; everything else is served, then cached for this
257 # client.
258 161 ▶sub lookup {
Subroutine lookup: called CC=1 SCAR=0.0
Pod documented
259 161 my $self = shift;
260 161 $self->_assert_blocking_ok('lookup');
261 160 return $self->_wait($self->lookup_p(@_));
262 }
263  
264 203 ▶sub lookup_p {
Subroutine lookup_p: called CC=10 SCAR=23.0
Pod undocumented
265 203 my ($self, $ip, %options) = @_;
266 203 ▶ Carp::croak('lookup: expected an IP address') if !defined $ip || !length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 2 201 203
Condition100%3/3
defined $ip and length $ip
!ll&&!rl&&r
11201
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
267 201 $self->_check_options('lookup', \%options, 'retries', 'timeout');
268 # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
269 175 $ip = InternetData::Bogon::unmapped($ip);
270 # This API answers an AS or a domain at the same path, as an answer this call
271 # cannot read, so one sent here would come back as three retried server_errors.
272 175 ▶ return Mojo::Promise->reject(InternetData::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
unless InternetData::Bogon::is_ip($ip) 16 159 175
273 kind => 'bad_request',
274 message => "'$ip' is not an IP address: look up an AS with lookup_asn, and a domain with lookup_domain",
275 )) unless InternetData::Bogon::is_ip($ip);
276  
277 159 ▶ return Mojo::Promise->resolve(InternetData::Bogon::bogon_result($ip))
Branch: 1 branch
BranchTrueFalseTotal
if InternetData::Bogon::is_bogon($ip) 4 155 159
278 if InternetData::Bogon::is_bogon($ip);
279  
280 155 ▶ if ($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 97 58 155
281 97 my $hit = $self->{cache}->get($ip);
282 97 ▶ return Mojo::Promise->resolve($hit) if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 4 93 97
283 # A miss for an address already in flight, for a lookup or a batch,
284 # takes that request's answer rather than sending its own.
285 93 ▶ 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 69 93
286 }
287  
288 127 my $flight = $self->_board($ip);
289 127 my $url = $self->_url('/' . Mojo::Util::url_escape($ip, '^A-Za-z0-9\-._~:'));
290 127 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 2 125 127
291 # Anything that dies before a request is under way lands the flight too, or
292 # every later lookup of the address would wait on it forever.
293 127 ▶ 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
294 127 ▶ if (!$sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 3 124 127
295 3 my $error = $@;
296 3 $self->_land($ip, $flight, 0, $error);
297 3 die $error;
298 }
299 return $sent->then(
300 45 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
301 45 my $result = InternetData::Result->from_wire(shift);
302 # Only a served answer is cached, and before it lands, so a miss
303 # after the flight is gone finds it. Errors never are, and bogons
304 # never reach this far.
305 45 ▶ $self->{cache}->set($ip, $result) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 27 18 45
306 45 $self->_land($ip, $flight, 1, $result);
307 45 return $result;
308 },
309 79 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
310 79 my $error = shift;
311 79 $self->_land($ip, $flight, 0, $error);
312 79 return Mojo::Promise->reject($error);
313 },
314 124 );
315 }
316  
317 # Concurrent misses for one address share ONE request. The first miss boards the
318 # address, and every miss after it, a batch's included, waits for that request
319 # instead. The request runs under the options of the call that led it; a failure
320 # reaches every waiter and is cached for none. Nothing is shared without a cache,
321 # since every lookup is then served. A Mojo::Promise cannot be canceled, so no
322 # waiter is ever left behind by a leader that gave up.
323 27670 ▶sub _board {
Subroutine _board: called CC=2 SCAR=6.9
324 27670 my ($self, $ip) = @_;
325 27670 ▶ return undef unless $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cache"} 27579 91 27670
326 91 my $flight = Mojo::Promise->new;
327 # Handled even when no waiter joined, so a failure is not warned about.
328 91 ▶ $flight->catch(sub { });
Subroutine __ANON__: called CC=1 SCAR=0.0
329 91 return $self->{flights}{$ip} = $flight;
330 }
331  
332 # Takes an address off the board and settles its flight for every waiter.
333 27670 ▶sub _land {
Subroutine _land: called CC=5 SCAR=16.7
334 27670 my ($self, $ip, $flight, $ok, $value) = @_;
335 27670 ▶ return unless $flight;
Branch: 1 branch
BranchTrueFalseTotal
unless $flight 27579 91 27670
336 delete $self->{flights}{$ip}
337 91 ▶ if $self->{flights}{$ip} && $self->{flights}{$ip} == $flight;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight 91 0 91
Condition33%1/3
$self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight
!ll&&!rl&&r
0091
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
338 91 ▶ return $ok ? $flight->resolve($value) : $flight->reject($value);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 43 48 91
339 }
340  
341 # What every database says about one AS. Not cached and not batched: only an
342 # address is.
343 45 ▶sub lookup_asn {
Subroutine lookup_asn: called CC=1 SCAR=0.0
Pod documented
344 45 my $self = shift;
345 45 $self->_assert_blocking_ok('lookup_asn');
346 44 return $self->_wait($self->lookup_asn_p(@_));
347 }
348  
349 45 ▶sub lookup_asn_p {
Subroutine lookup_asn_p: called CC=4 SCAR=13.9
Pod undocumented
350 45 my ($self, $asn, %options) = @_;
351 45 ▶ Carp::croak('lookup_asn: expected an AS number') if !defined $asn || !length $asn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $asn and length $asn 2 43 45
Condition100%3/3
defined $asn and length $asn
!ll&&!rl&&r
1143
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
352 43 $self->_check_options('lookup_asn', \%options, 'retries', 'timeout');
353  
354 33 my $url = $self->_url('/asn/' . _path_segment($asn));
355 33 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 32 33
356 47 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $ASN_LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
357 33 ▶ ->then(sub { InternetData::AsnResult->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
358 }
359  
360 # What every database says about one domain, neither cached nor batched.
361 43 ▶sub lookup_domain {
Subroutine lookup_domain: called CC=1 SCAR=0.0
Pod documented
362 43 my $self = shift;
363 43 $self->_assert_blocking_ok('lookup_domain');
364 42 return $self->_wait($self->lookup_domain_p(@_));
365 }
366  
367 43 ▶sub lookup_domain_p {
Subroutine lookup_domain_p: called CC=4 SCAR=13.9
Pod undocumented
368 43 my ($self, $domain, %options) = @_;
369 43 ▶ Carp::croak('lookup_domain: expected a domain') if !defined $domain || !length $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain and length $domain 2 41 43
Condition100%3/3
defined $domain and length $domain
!ll&&!rl&&r
1141
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
370 41 $self->_check_options('lookup_domain', \%options, 'retries', 'timeout');
371  
372 31 my $url = $self->_url('/domain/' . _path_segment($domain));
373 31 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 30 31
374 38 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $DOMAIN_LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
375 31 ▶ ->then(sub { InternetData::DomainResult->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
376 }
377  
378 # The address our edge observed this client calling from. Deliberately not
379 # cached: the cache is keyed by address, and which address this is IS the
380 # question.
381 26 ▶sub my_ip {
Subroutine my_ip: called CC=1 SCAR=0.0
Pod documented
382 26 my $self = shift;
383 26 $self->_assert_blocking_ok('my_ip');
384 25 return $self->_wait($self->my_ip_p(@_));
385 }
386  
387 25 ▶sub my_ip_p {
Subroutine my_ip_p: called CC=2 SCAR=6.9
Pod undocumented
388 25 my ($self, %options) = @_;
389 25 $self->_check_options('my_ip', \%options, 'retries', 'timeout');
390  
391 15 my $url = $self->_url('/myip');
392 15 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 14 15
393 18 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
394 15 ▶ ->then(sub { InternetData::Result->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
395 }
396  
397 # What this key is entitled to and what it has spent. Deliberately not cached:
398 # the whole point is the number, and a cached one is wrong within seconds.
399 52 ▶sub my_entitlement {
Subroutine my_entitlement: called CC=1 SCAR=0.0
Pod documented
400 52 my $self = shift;
401 52 $self->_assert_blocking_ok('my_entitlement');
402 51 return $self->_wait($self->my_entitlement_p(@_));
403 }
404  
405 51 ▶sub my_entitlement_p {
Subroutine my_entitlement_p: called CC=2 SCAR=6.9
Pod undocumented
406 51 my ($self, %options) = @_;
407 51 $self->_check_options('my_entitlement', \%options, 'retries', 'timeout');
408  
409 41 my $url = $self->_url('/api/v1/entitlement');
410 41 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 1 40 41
411 41 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $ENTITLEMENT) });
Subroutine __ANON__: called CC=1 SCAR=0.0
412 }
413  
414 # Look up many addresses concurrently, keyed by address rather than positional,
415 # so duplicates collapse to one request and the caller never lines two lists up.
416 48 ▶sub lookup_batch {
Subroutine lookup_batch: called CC=1 SCAR=0.0
Pod documented
417 48 my $self = shift;
418 48 $self->_assert_blocking_ok('lookup_batch');
419 47 return $self->_wait($self->lookup_batch_p(@_));
420 }
421  
422 65 ▶sub lookup_batch_p {
Subroutine lookup_batch_p: called CC=13 SCAR=25.7
Pod undocumented
423 65 my ($self, $ips, %options) = @_;
424 65 ▶ Carp::croak('lookup_batch: expected an array reference of addresses')
Branch: 1 branch
BranchTrueFalseTotal
if ref $ips ne "ARRAY" 1 64 65
425 if ref $ips ne 'ARRAY';
426 64 $self->_check_options('lookup_batch', \%options, 'retries', 'concurrency', 'timeout');
427  
428 31 ▶ my $limit = defined $options{concurrency} ? $options{concurrency} : $self->{concurrency};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'concurrency'} ? : 3 28 31
429 31 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 0 31 31
430  
431 # Bogons are answered locally and cached answers are reused; everything else
432 # goes to the batch endpoint in chunks of up to 1000 addresses, with at most
433 # `limit` chunks in flight. An IPv4-mapped address is sent as the address it
434 # carries, once however many of its spellings were asked, and answered under
435 # each one asked.
436 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
437 31 my %seen;
438 31 my @unique = grep { !$seen{$_}++ } map { InternetData::Bogon::unmapped($_) } @asked;
439 31 my %answers;
440 my @pending;
441 31 my %joined;
442 31 for my $ip (@unique) {
443 27547 ▶ if (InternetData::Bogon::is_bogon($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if (InternetData::Bogon::is_bogon($ip)) 2 27545 27547
444 2 $answers{$ip} = InternetData::Bogon::bogon_result($ip);
445 2 next;
446 }
447 27545 ▶ my $hit = $self->{cache} ? $self->{cache}->get($ip) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cache'} ? : 24 27521 27545
448 27545 ▶ if ($hit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit) 1 27544 27545
449 1 $answers{$ip} = $hit;
450 1 next;
451 }
452 # Already in flight, for a lookup or another batch: this batch takes
453 # that request's answer rather than sending the address again.
454 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
455 1 $joined{$ip} = $self->{flights}{$ip};
456 1 next;
457 }
458 27543 push @pending, $ip;
459 }
460 # Boarded before any chunk is built, so a lookup of one of these arriving
461 # meanwhile waits for this batch's answer.
462 31 my %boarded = map { ($_ => $self->_board($_)) } @pending;
463 31 my @queue;
464 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
465  
466 my $batch = {
467 queue => \@queue, answers => \%answers, active => 0, limit => $limit,
468 31 retries => $retries, timeout => $options{timeout}, promise => Mojo::Promise->new,
469 boarded => \%boarded,
470 };
471 31 $self->_dispatch($batch);
472 my $done = %joined ? Mojo::Promise->all(
473 $batch->{promise},
474 map {
475 1 my $ip = $_;
476 $joined{$ip}->then(
477 1 ▶ sub { $answers{$ip} = shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
478 0 ▶ sub { $answers{$ip} = InternetData::Error->wrap(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
479 1 );
480 } sort keys %joined,
481 31 ▶ ) : $batch->{promise};
Branch: 1 branch
BranchTrueFalseTotal
%joined ? : 1 30 31
482 31 ▶ return $done->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
483 31 return { map { ($_ => $answers{ InternetData::Bogon::unmapped($_) }) } @asked };
484 31 });
485 }
486  
487 # The licensed database downloads. Built per call rather than held, so the
488 # client and its sub-API never form a reference cycle.
489 162 ▶sub database {
Subroutine database: called CC=1 SCAR=0.0
Pod documented
490 162 my ($self) = @_;
491 162 return InternetData::Database->_new($self);
492 }
493  
494 # The OAuth device-flow sign-in, built per call for the same reason.
495 167 ▶sub oauth {
Subroutine oauth: called CC=1 SCAR=0.0
Pod documented
496 167 my ($self) = @_;
497 167 return InternetData::Oauth->_new($self);
498 }
499  
500 # Keeps exactly `limit` chunks in flight: every answer starts the next one, so a
501 # per-call concurrency is a real ceiling rather than an option that was accepted
502 # and ignored. A failing address carries its error as its value, so one bad
503 # entry cannot lose the rest of the answers: the API reports a per-entry failure
504 # with the status the single lookup would have answered, and a chunk that fails
505 # as a whole marks every address in it.
506 88 ▶sub _dispatch {
Subroutine _dispatch: called CC=5 SCAR=16.1
507 88 my ($self, $batch) = @_;
508 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
509 31 $batch->{promise}->resolve($batch->{answers});
510 31 return;
511 }
512 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
513 57 my $chunk = shift @{$batch->{queue}};
514 57 $batch->{active}++;
515 57 ▶ $self->_lookup_chunk_p($chunk, $batch->{retries}, $batch->{timeout})->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
516 57 my $answers = shift;
517 57 for my $ip (keys %$answers) {
518 27543 my $answer = $answers->{$ip};
519 27543 $batch->{answers}{$ip} = $answer;
520 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
521 27543 $self->_land($ip, $batch->{boarded}{$ip}, !$failed, $answer);
522 }
523 57 $self->_settled($batch);
524 57 });
525 }
526 }
527  
528 # One POST /batch, mapped back onto the addresses it was asked about. A
529 # chunk-level failure - the call refused, the transport failing, the retries
530 # exhausted - becomes every address's error, exactly as it would have been had
531 # each been looked up alone. Never rejects: the failure is the value.
532 57 ▶sub _lookup_chunk_p {
Subroutine _lookup_chunk_p: called CC=1 SCAR=0.0
533 57 my ($self, $chunk, $retries, $timeout) = @_;
534 57 my $url = $self->_url('/batch');
535 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
536 47 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
537 47 my $body = shift;
538 47 my $results = $body->{results};
539 47 my $errors = $body->{errors};
540 47 my %answers;
541 # An entry is judged alone: one that is not an answer is that
542 # address's error, and the rest of the chunk keeps its answers.
543 47 for my $ip (@$chunk) {
544 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
545 27523 my $result = InternetData::Result->from_wire($results->{$ip});
546 27523 ▶ $self->{cache}->set($ip, $result) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 16 27507 27523
547 27523 $answers{$ip} = $result;
548 }
549 ▶ 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
550 $answers{$ip} = InternetData::Error->from_entry(
551 $errors->{$ip}{status}, $errors->{$ip}{error},
552 4 );
553 }
554 else {
555 5 $answers{$ip} = InternetData::Error->new(
556 kind => 'server_error', status => 200,
557 message => "the batch answer did not include $ip",
558 );
559 }
560 }
561 47 return \%answers;
562 },
563 10 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
564 10 my $error = InternetData::Error->wrap(shift);
565 10 return { map { $_ => $error } @$chunk };
566 },
567 57 );
568 }
569  
570 # Releasing the slot happens in the SAME handler that records the answer. A
571 # second `then` would leave a gap in which a dying handler strands the slot, and
572 # a batch that never releases one never finishes.
573 57 ▶sub _settled {
Subroutine _settled: called CC=1 SCAR=0.0
574 57 my ($self, $batch) = @_;
575 57 $batch->{active}--;
576 57 $self->_dispatch($batch);
577 }
578  
579 # One file transfer. Every chunk is handed to $on_chunk and none is kept, so a
580 # body costs the same in memory whether it is 264 bytes or 5.34 GiB. Resolves
581 # with the number of bytes handed over.
582 17 ▶sub _stream_p {
Subroutine _stream_p: called CC=1 SCAR=0.0
583 17 my ($self, $url, $on_chunk) = @_;
584 # Built here rather than through _get_p, which is the only place the API key
585 # is ever attached: the presigned link authorizes itself, so carrying the key
586 # would hand it to a host with no business holding it. Object storage answers
587 # 400 to a presigned GET that also carries an Authorization header, so this
588 # is not merely a leak - it breaks the download too.
589 17 my $tx = $self->{ua}->build_tx(GET => $url);
590  
591 # Mojo asks for gzip on every request it builds. A published file is already
592 # compressed, so the only thing that would buy is a Content-Length counting
593 # bytes that never reach the sink, and that length is the only evidence the
594 # transfer arrived whole.
595 17 $tx->req->headers->remove('Accept-Encoding');
596 # Mojo aborts a response past max_message_size, counting every byte it parses
597 # whether or not it keeps any of them. Mojo::Message::Response defaults to
598 # 2 GiB, which the catalog is already past, and MOJO_MAX_MESSAGE_SIZE lowers
599 # it for every response in the process, on a machine whose environment we do
600 # not own.
601 17 $tx->res->max_message_size(0);
602  
603 17 my $received = 0;
604 # Unsubscribing Mojo's own reader is what stops the body being collected into
605 # the message. The subscriber hangs off the transaction, so holding the
606 # transaction strongly inside it would be a cycle the interpreter never
607 # collects; the user agent keeps it alive for as long as bytes are arriving.
608 17 my $weak = $tx;
609 17 Scalar::Util::weaken($weak);
610 211 ▶ $tx->res->content->unsubscribe('read')->on(read => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
611 211 my (undef, $bytes) = @_;
612 # An error body is neither written out nor held: the status is what
613 # separates a lapsed link from a refused one, and nothing bounds the size
614 # of what a storage host puts in the body of a refusal.
615 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
616 209 $received += length $bytes;
617 209 $on_chunk->($bytes);
618 17 });
619  
620 16 ▶ return $self->_start_p($tx, 0)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
621 16 my $res = shift->res;
622 # No body to read: the handler above kept nothing that was not a 200,
623 # because nothing bounds the size of what a storage host puts in a
624 # refusal. So the message names where the refusal came from, and the
625 # status still decides the kind - which is what says whether the link
626 # lapsed or was never good.
627 16 ▶ die InternetData::Error->from_response(
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 2 14 16
628 $res->code, $res->headers, undef,
629 'object storage refused the download link with status ' . $res->code,
630 ) unless $res->code == 200;
631  
632 # Before Mojolicious 9.54 a body that stops early reaches Perl as an
633 # ordinary end of stream: the status was 200 and Mojo reports no error,
634 # so without this a half transfer is a short file nobody notices.
635 14 my $short = _short_transfer($res, $received);
636 14 ▶ die $short if $short;
Branch: 1 branch
BranchTrueFalseTotal
if $short 0 14 14
637 14 return $received;
638 1 ▶ }, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
639 1 my $error = shift;
640 # From 9.54 the same body rejects as `Premature connection close`, which
641 # says nothing of how far it got, so the lengths are added to it.
642 1 my $short = _short_transfer($tx->res, $received, $error);
643 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
644 17 });
645 }
646  
647 # The error for a 200 whose body stopped short of the length it declared, or
648 # undef when it did not. $cause is what Mojo rejected it with, when it did.
649 15 ▶sub _short_transfer {
Subroutine _short_transfer: called CC=8 SCAR=23.7
650 15 my ($res, $received, $cause) = @_;
651 15 my $declared = $res->headers->content_length;
652 return undef
653 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
654 1 my $message = "the transfer ended after $received of $declared bytes";
655 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
656 1 return InternetData::Error->new(kind => 'network', message => $message);
657 }
658  
659 # request_timeout bounds the WHOLE response and Mojo::UserAgent has no
660 # per-transaction form of it, yet a call may bring its own bound and a transfer
661 # must have none (0): 30 seconds is right for a metadata call and wrong for a
662 # gigabyte. So it is set only across the hand-over, where start_p arms the timer
663 # synchronously - and set EXPLICITLY each time, because Mojo runs a loop tick
664 # inside start_p when the loop is idle, and a retry started in that tick would
665 # otherwise inherit this call's bound.
666 #
667 # A rejection is a plain string when the request never got far enough to have a
668 # status, which is exactly the transport failure the retry rule treats as worth
669 # another attempt.
670 1182 ▶sub _start_p {
Subroutine _start_p: called CC=3 SCAR=11.0
671 1182 my ($self, $tx, $timeout) = @_;
672 1182 my $ua = $self->{ua};
673 1182 my $bound = $ua->request_timeout;
674 # A number, for the reason new gives: a per-call '0.0' is otherwise a timer
675 # that fires at once.
676 1182 ▶ $ua->request_timeout(defined $timeout ? 0 + $timeout : $self->{timeout});
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 76 1106 1182
677 1182 my $promise = eval { $ua->start_p($tx) };
678 1182 my $failed = $@;
679 1182 $ua->request_timeout($bound);
680 1182 ▶ die InternetData::Error->wrap($failed) unless $promise;
Branch: 1 branch
BranchTrueFalseTotal
unless $promise 6 1176 1182
681 52 ▶ return $promise->catch(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
682 52 die InternetData::Error->new(kind => 'network', message => "$_[0]");
683 1176 });
684 }
685  
686 # Recurses through $self rather than through a self-referential closure, which
687 # in Perl would be a reference cycle the interpreter never collects. $tries
688 # counts the retries already made.
689 1184 ▶sub _retry_p {
Subroutine _retry_p: called CC=2 SCAR=6.9
690 1184 my ($self, $left, $attempt, $may_retry, $tries) = @_;
691 1184 ▶ $tries //= 0;
Condition100%2/2
$tries //= 0
l!l
327857
MC/DC100%1/1
$tries //= 0
$tries
Truth table100%2/2
A
A= $tries
Aresult
11
00
692 885 ▶ return $attempt->()->catch(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
693 885 my $error = InternetData::Error->wrap(shift);
694 # $may_retry, when given, can veto a retry the error alone would allow.
695 885 ▶ 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() 558 327 885
Condition100%9/9
$left <= 0 or not $error->retryable or $may_retry and not &$may_retry()
l!l&&r!l&&!r
5571327
$left <= 0 or not $error->retryable
l!l&&r!l&&!r
50156328
$may_retry and not &$may_retry()
!ll&&!rl&&r
32611
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
696 # The wait is a TIMER, never a sleep: this promise may share an event
697 # loop with a Mojolicious application, and sleeping here would stall
698 # every other thing on it.
699 return Mojo::Promise->timer(_retry_delay($error->retry_after, $tries))
700 327 ▶ ->then(sub { $self->_retry_p($left - 1, $attempt, $may_retry, $tries + 1) });
Subroutine __ANON__: called CC=1 SCAR=0.0
701 1184 });
702 }
703  
704 # The server's Retry-After when it gave a usable one, otherwise the backoff:
705 # 250 ms, doubling per retry, capped from the seventh. A Retry-After past
706 # LONGEST_WAIT is waited out on the backoff too, still rate_limited, rather than
707 # holding the call for as long as any server asks.
708 340 ▶sub _retry_delay {
Subroutine _retry_delay: called CC=5 SCAR=16.1
709 340 my ($retry_after, $tries) = @_;
710 340 ▶ return $retry_after
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647" 3 337 340
Condition100%6/6
defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647"
!ll&&!rl&&r
33433
defined $retry_after and $retry_after > 0
!ll&&!rl&&r
324106
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
711 if defined $retry_after && $retry_after > 0 && $retry_after <= LONGEST_WAIT;
712 337 ▶ return BACKOFF_BASE * 2**($tries < 6 ? $tries : 6);
Branch: 1 branch
BranchTrueFalseTotal
$tries < 6 ? : 335 2 337
713 }
714  
715 # $body, when given, is POSTed as JSON, and $timeout is the call's own bound, or
716 # undef for the client's. $shape is the whole answer's, and $member, when given,
717 # the one member of it the call returns. An answer without the shape is no answer
718 # at all, so it fails inside the attempt as a retried server_error, like any
719 # other 2xx that cannot be read, rather than reaching the caller as undef.
720 917 ▶sub _json_p {
Subroutine _json_p: called CC=2 SCAR=6.9
721 917 my ($self, $url, $body, $timeout, $member, $shape) = @_;
722 917 ▶ my $sent = defined $body ? $self->_post_p($url, $body, $timeout) : $self->_get_p($url, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 58 859 917
723 888 ▶ return $sent->then(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
724 888 my $res = shift->res;
725 888 ▶ die InternetData::Error->from_response($res->code, $res->headers, $res->json)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 143 745 888
726 unless $res->is_success;
727 745 my $decoded = $res->json;
728 552 ▶ my $unreadable = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
729 552 die InternetData::Error->new(kind => 'server_error', status => $res->code, message => shift);
730 745 };
731 745 ▶ $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 699 745
732 699 ▶ return $decoded unless $shape;
Branch: 1 branch
BranchTrueFalseTotal
unless $shape 0 699 699
733 699 my $wrong = _nonconforming($shape, $decoded, []);
734 699 ▶ $unreadable->($wrong) if defined $wrong;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wrong 506 193 699
735 193 ▶ return defined $member ? $decoded->{$member} : $decoded;
Branch: 1 branch
BranchTrueFalseTotal
defined $member ? : 59 134 193
736 913 });
737 }
738  
739 # undef when the value has the shape, else what the API answered wrong, naming
740 # the member by its path. A string is told from a number by the scalar Mojo::JSON
741 # decoded it into, as InternetData::Oauth does, since nothing else in Perl tells
742 # `"7"` from `7`. No list entry or map value may be null.
743 1082317 ▶sub _nonconforming {
Subroutine _nonconforming: called CC=35 SCAR=35.6
744 1082317 my ($shape, $value, $at) = @_;
745 1082317 my ($kind, $inner) = @$shape;
746 1082317 ▶ 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 ? : 99 0 99
747 1082317 ▶ return $wrong->() unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 10 1082307 1082317
748 1082307 ▶ return undef if $kind eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "any" 27532 1054775 1082307
749 1054775 ▶ if ($kind eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "string") 526830 527945 1054775
750 526830 ▶ return InternetData::Oauth::_is_string($value) ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
InternetData::Oauth::_is_string($value) ? : 526816 14 526830
751 }
752 527945 ▶ if ($kind eq 'date' || $kind eq 'date-time') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "date" or $kind eq "date-time") 624 527321 527945
Condition100%3/3
$kind eq "date" or $kind eq "date-time"
l!l&&r!l&&!r
61563527321
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
753 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
754 : 'the API answered ' . join('.', @$at) . ' that is no RFC 3339 ' . $kind;
755 }
756 527321 ▶ if ($kind eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "integer") 27958 499363 527321
757 27958 ▶ 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/ ? : 27924 34 27958
Condition83%5/6
not ref $value || InternetData::Oauth::_is_string($value) and $value =~ /\A-?[0-9]+\z/
!ll&&!rl&&r
30427924
ref $value || InternetData::Oauth::_is_string($value)
l!l&&r!l&&!r
03027928
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
758 ? undef : $wrong->();
759 }
760 499363 ▶ if ($kind eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "number") 10 499353 499363
761 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
762 ? undef : $wrong->();
763 }
764 499353 ▶ if ($kind eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "boolean") 304282 195071 499353
765 304282 ▶ return Scalar::Util::blessed($value) && $value->isa('JSON::PP::Boolean') ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('JSON::PP::Boolean') ? : 304255 27 304282
Condition66%2/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
270304255
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
766 }
767 195071 ▶ if ($kind eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "object") 2 195069 195071
768 2 ▶ return ref $value eq 'HASH' ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 0 2 2
769 }
770 195069 ▶ if ($kind eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "list") 524 194545 195069
771 524 ▶ return $wrong->() unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 4 520 524
772 520 for my $i (0 .. $#$value) {
773 485 my $w = _nonconforming($inner, $value->[$i], [@$at, $i]);
774 485 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 326 159 485
775 }
776 194 return undef;
777 }
778 194545 ▶ if ($kind eq 'map_of') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "map_of") 137 194408 194545
779 137 ▶ return $wrong->() unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 2 135 137
780 135 for my $key (sort keys %$value) {
781 27611 my $w = _nonconforming($inner, $value->{$key}, [@$at, $key]);
782 27611 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 12 27599 27611
783 }
784 123 return undef;
785 }
786 194408 ▶ return $wrong->() unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 4 194404 194408
787 194404 for my $field (@$inner) {
788 2544853 my ($name, $member_shape, $required) = @$field;
789 2544853 ▶ if (!defined $value->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value->{$name}) 1518863 1025990 2544853
790 1518863 ▶ return 'the API answered with no ' . join('.', @$at, $name) if $required;
Branch: 1 branch
BranchTrueFalseTotal
if $required 70 1518793 1518863
791 1518793 next;
792 }
793 1025990 my $w = _nonconforming($member_shape, $value->{$name}, [@$at, $name]);
794 1025990 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 830 1025160 1025990
795 }
796 193504 return undef;
797 }
798  
799 # RFC 3339's `full-date`, or its `date-time` with the `T` and `Z` in either case,
800 # an hour below 24, a minute and second below 60 and an offset of at most 23:59,
801 # on a day its month has. ASCII digits and `\z`, since Perl's `\d` takes any
802 # script's digits and `$` a trailing newline.
803 8 my $FULL_DATE = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/;
804 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]+)?
805 (?:[Zz]|[+-]([0-9]{2}):([0-9]{2}))\z/x;
806  
807 622 ▶sub _is_rfc3339 {
Subroutine _is_rfc3339: called CC=10 SCAR=23.0
808 622 my ($kind, $value) = @_;
809 622 ▶ if ($kind eq 'date') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "date") 61 561 622
810 61 ▶ my @date = $value =~ $FULL_DATE or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@date) = $value =~ /$FULL_DATE/ or return 0 33 28 61
811 33 return _is_day(@date);
812 }
813 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
814 or return 0;
815 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
816 && (!defined $off_hour || ($off_hour < 24 && $off_minute < 60));
817 }
818  
819 364 ▶sub _is_day {
Subroutine _is_day: called CC=7 SCAR=19.5
820 364 my ($year, $month, $day) = @_;
821 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
822 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
823 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
824 }
825  
826 911 ▶sub _get_p {
Subroutine _get_p: called CC=1 SCAR=0.0
827 911 my ($self, $url, $timeout) = @_;
828 911 return $self->_start_p($self->{ua}->build_tx(GET => $url => $self->_headers), $timeout);
829 }
830  
831 # The one request with a body: the batch. Mojo encodes the JSON and sets the
832 # content type.
833 58 ▶sub _post_p {
Subroutine _post_p: called CC=1 SCAR=0.0
834 58 my ($self, $url, $body, $timeout) = @_;
835 58 my $tx = $self->{ua}->build_tx(POST => $url => $self->_headers => json => $body);
836 58 return $self->_start_p($tx, $timeout);
837 }
838  
839 969 ▶sub _headers {
Subroutine _headers: called CC=2 SCAR=6.9
840 969 my ($self) = @_;
841 969 my %headers = (Accept => 'application/json');
842 # One scheme. The v1 endpoints on this same host take `?apikey=` with a
843 # different key vocabulary, so sending a v2 key that way would make it look
844 # plausible on the version it does not belong to, and query strings end up in
845 # logs. A blank key is none at all (see new): that is what an unset
846 # `${{ secrets.X }}` interpolates to, and `Bearer ` is a worse answer than no
847 # header.
848 969 ▶ $headers{Authorization} = "Bearer $self->{api_key}" if defined $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"api_key"} 791 178 969
849 969 return \%headers;
850 }
851  
852 684 ▶sub _url {
Subroutine _url: called CC=2 SCAR=6.9
853 684 my ($self, $path, %query) = @_;
854 684 my $url = Mojo::URL->new($self->{base_url} . $path);
855 684 ▶ $url->query(%query) if %query;
Branch: 1 branch
BranchTrueFalseTotal
if %query 113 571 684
856 684 return $url;
857 }
858  
859 # One path segment as given: its UTF-8 bytes, everything but the unreserved
860 # characters escaped, so a `/`, `?` or `#` in it cannot reach another route.
861 64 ▶sub _path_segment {
Subroutine _path_segment: called CC=1 SCAR=0.0
862 64 my ($value) = @_;
863 64 return Mojo::Util::url_escape(Mojo::Util::encode('UTF-8', $value), '^A-Za-z0-9\-._~');
864 }
865  
866 800 ▶sub _wait {
Subroutine _wait: called CC=2 SCAR=6.9
867 800 my ($self, $promise) = @_;
868 800 my ($value, $error, $failed);
869 800 ▶ $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
870 800 ▶ die $error if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 564 236 800
871 236 return $value;
872 }
873  
874 # Checked BEFORE the promise is built, not after: Mojo::UserAgent starts a
875 # transaction as soon as one is created, so croaking later would still have spent
876 # a request from the caller's allowance.
877 979 ▶sub _assert_blocking_ok {
Subroutine _assert_blocking_ok: called CC=2 SCAR=6.9
878 979 my ($self, $method) = @_;
879 979 ▶ return unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 972 7 979
880 7 Carp::croak(
881 "InternetData::$method cannot block inside a running Mojo::IOLoop; "
882 . "call ${method}_p instead, which returns a Mojo::Promise"
883 );
884 }
885  
886 1065 ▶sub _check_options {
Subroutine _check_options: called CC=9 SCAR=22.0
887 1065 my ($self, $method, $options, @allowed) = @_;
888 1065 my %allowed = map { $_ => 1 } @allowed;
889 1065 my @unknown = sort grep { !$allowed{$_} } keys %$options;
890 1065 ▶ Carp::croak("InternetData::$method: unknown option(s): @unknown") if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 13 1052 1065
891 # A negative or non-numeric bound would fire at once and fail the very call
892 # it was meant to protect.
893 1052 my $timeout = $options->{timeout};
894 1052 ▶ 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 1033 1052
Condition100%6/6
defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0
!ll&&!rl&&r
9755819
&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
895 if defined $timeout && !(Scalar::Util::looks_like_number($timeout) && $timeout >= 0);
896 Carp::croak("InternetData::$method: retries must be a whole number, 0 or more")
897 1033 ▶ 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 935 1033
Condition100%3/3
defined $options->{"retries"} and not _is_count($options->{"retries"}, 0)
!ll&&!rl&&r
927898
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
898 Carp::croak("InternetData::$method: concurrency must be a whole number, 1 or more")
899 935 ▶ 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 919 935
Condition100%3/3
defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1)
!ll&&!rl&&r
916316
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
900 }
901  
902 # A whole number from $least, or Inf for no limit. NaN compares false with
903 # anything, so a NaN count retried a failing call for good and a NaN concurrency
904 # put no chunk in flight, spinning the loop forever; a word reads as 0, and 1.5
905 # as two retries.
906 1074 ▶sub _is_count {
Subroutine _is_count: called CC=4 SCAR=13.9
907 1074 my ($n, $least) = @_;
908 1074 ▶ 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
11318943
not ref $n and &Scalar::Util::looks_like_number($n) and $n >= $least
!ll&&!rl&&r
5459961
not ref $n and &Scalar::Util::looks_like_number($n)
!ll&&!rl&&r
18361020
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
909 }
910  
911 453 ▶sub _base_url {
Subroutine _base_url: called CC=3 SCAR=11.0
912 453 my ($url) = @_;
913 453 ▶ $url = DEFAULT_BASE_URL unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 10 443 453
Condition66%2/3
defined $url and length $url
!ll&&!rl&&r
100443
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
914 453 $url =~ s{/+\z}{};
915 453 return $url;
916 }
917  
918 8 1;
919  
920 __END__
↑ Summary InternetData/Oauth.pm »