InternetData.pm

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