VPNDetection/Error.pm

filter: Statementstmt 94.3% 84 / 89 Branchbran 84.0% 37 / 44 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 95.8% 23 / 24 Podpod 50.0% 6 / 12 totaltotal 86.3% 158 / 183 CC 27 SCAR 14.9
CC
27
Cov
89%
wrap
8.7
from_response
8.0
_http_date
6.0
CRAP 4.4
« VPNDetection/Oauth.pm ↑ Summary VPNDetection/Bogon.pm »
1 package VPNDetection::Error;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Mojo::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use overload
11 1 ▶ '""' => sub { $_[0]->{message} },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12 181 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 15 fallback => 1;
14  
15 15 our $VERSION = '4.0.0';
16  
17 15 my %RETRYABLE = (rate_limited => 1, server_error => 1, network => 1);
18  
19 # What an answer past the organization's allowance raises: no figure, no plan.
20 15 ▶use constant CAPPED_MESSAGE =>
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 "the organization's lookups with every field are used; this answer has the basics";
22  
23 15 my %MONTH;
24 15 @MONTH{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = 0 .. 11;
25 15 my $DAY = join '|', qw(Mon Tue Wed Thu Fri Sat Sun);
26 15 my $WEEKDAY = join '|', qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday);
27 15 my $MON = join '|', keys %MONTH;
28 15 my $TIME = '([0-9]{2}):([0-9]{2}):([0-9]{2})';
29  
30 913 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
31 913 my ($class, %args) = @_;
32 return bless {
33 kind => $args{kind},
34 message => defined $args{message} ? $args{message} : $args{kind},
35 status => $args{status},
36 retry_after => $args{retry_after},
37 result => $args{result},
38 913 ▶ }, $class;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'message'} ? : 913 0 913
39 }
40  
41 # The error for an answer that carries cap_reached true: quota_exceeded, never
42 # retried, status 200, and the answer itself as its result so a catch still has the
43 # basics.
44 30 ▶sub capped {
Subroutine capped: called CC=1 SCAR=0.0
Pod undocumented
45 30 my ($class, $result) = @_;
46 30 return $class->new(kind => 'quota_exceeded', status => 200, message => CAPPED_MESSAGE, result => $result);
47 }
48  
49 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $class = shift;
51 0 die $class->new(@_);
52 }
53  
54 # Classifies one failed response. The two APIs behind this host answer with
55 # different envelopes - the lookup endpoint uses `error`, the database endpoints
56 # use `rc` - and both are read here so a caller never has to know which was hit.
57 95 ▶sub from_response {
Subroutine from_response: called CC=8 SCAR=20.8
Pod undocumented
58 95 my ($class, $status, $headers, $body) = @_;
59 95 my $message = _message($body);
60 95 ▶ $message = "request failed with status $status" unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 13 82 95
61 95 my $retry_after = _retry_after($headers->header('Retry-After'));
62  
63 # Present means a transient rate limit and retrying works; absent means an
64 # allowance is spent. Nothing else in the response separates the two.
65 95 ▶ if ($status == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 429) 23 72 95
66 23 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $retry_after ? : 9 14 23
67 kind => defined $retry_after ? 'rate_limited' : 'quota_exceeded',
68 message => $message, status => $status, retry_after => $retry_after,
69 );
70 }
71 72 ▶ return $class->new(kind => 'bad_request', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 400 28 44 72
72 if $status == 400;
73 44 ▶ return $class->new(kind => 'unauthorized', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 401 3 41 44
74 if $status == 401;
75 41 ▶ return $class->new(kind => 'forbidden', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 403 8 33 41
76 if $status == 403;
77 # Any other 4xx is a CLIENT error. Falling through to the server_error
78 # default would make it retryable, so a bad dataset ID would be retried
79 # twice before failing. Only 5xx and transport failures are worth a retry.
80 33 ▶ return $class->new(kind => 'bad_request', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status < 500 8 25 33
81 if $status < 500;
82 25 return $class->new(kind => 'server_error', message => $message, status => $status);
83 }
84  
85 # A per-entry failure inside a successful batch: the status the single lookup
86 # would have answered, and its message, with no headers at all - so a 429 here
87 # is a spent allowance, which is the only kind the API puts in an entry.
88 3 ▶sub from_entry {
Subroutine from_entry: called CC=1 SCAR=0.0
Pod undocumented
89 3 my ($class, $status, $message) = @_;
90 3 return $class->from_response($status, Mojo::Headers->new, { error => $message });
91 }
92  
93 # Anything a promise can reject with becomes one of these. Mojo::UserAgent
94 # rejects with a plain string when a connection never got far enough to have a
95 # status, which is exactly the transport failure the retry rule wants.
96 814 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=21.7
Pod undocumented
97 814 my ($class, $err) = @_;
98 814 ▶ return $err if Scalar::Util::blessed($err) && $err->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->isa(__PACKAGE__) 814 0 814
Condition33%1/3
builtin::blessed($err) and $err->isa(__PACKAGE__)
!ll&&!rl&&r
00814
MC/DC0%0/2
builtin::blessed($err) and $err->isa(__PACKAGE__)
builtin::blessed($err) $err->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa(__PACKAGE__)
ABresult
0X0
100
111
99 0 ▶ my $message = defined $err ? "$err" : 'request failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 0 0 0
100 0 $message =~ s/\s+\z//;
101 0 return $class->new(kind => 'network', message => $message);
102 }
103  
104 726 ▶sub kind { $_[0]->{kind} }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
105 33 ▶sub message { $_[0]->{message} }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
106 597 ▶sub status { $_[0]->{status} }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
107 161 ▶sub retry_after { $_[0]->{retry_after} }
Subroutine retry_after: called CC=1 SCAR=0.0
Pod documented
108 37 ▶sub result { $_[0]->{result} }
Subroutine result: called CC=1 SCAR=0.0
Pod documented
109  
110 241 ▶sub retryable {
Subroutine retryable: called CC=2 SCAR=6.9
Pod documented
111 241 ▶ return $RETRYABLE{ $_[0]->{kind} } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$RETRYABLE{$_[0]{'kind'}} ? : 205 36 241
112 }
113  
114 # An empty field says nothing, so it gives way to the other envelope, then to
115 # the status, rather than reading as a message of nothing at all (3.8.0 answered
116 # `{"error": "", "rc": "NOT_LICENSED"}` with an empty message).
117 95 ▶sub _message {
Subroutine _message: called CC=6 SCAR=17.9
118 95 my ($body) = @_;
119 95 ▶ return undef unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 11 84 95
120 84 for my $key (qw(error rc)) {
121 93 ▶ return $body->{$key} if defined $body->{$key} && !ref $body->{$key} && length $body->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $body->{$key} and not ref $body->{$key} and length $body->{$key} 82 11 93
Condition83%5/6
defined $body->{$key} and not ref $body->{$key} and length $body->{$key}
!ll&&!rl&&r
8382
defined $body->{$key} and not ref $body->{$key}
!ll&&!rl&&r
8085
MC/DC66%2/3
defined $body->{$key} and not ref $body->{$key} and length $body->{$key}
defined $body->{$key} not ref $body->{$key} length $body->{$key}
Truth table75%3/4
A and not(B) and C
A= defined $body->{$key}B= not ref $body->{$key}C= length $body->{$key}
ABCresult
0XX0
10X0
1100
1111
122 }
123 2 return undef;
124 }
125  
126 # Seconds as digits, or an HTTP date, and nothing else, so a value that is
127 # neither is no Retry-After at all and the 429 a spent quota. RFC 9110 allows
128 # exactly three date forms, all in GMT. Through 3.8.0 a fraction was read as
129 # seconds, and Mojo::Date read ISO 8601, any zone as GMT and a day or month it
130 # does not know as Monday or January, each of which held a call on a date the
131 # server never sent.
132 95 ▶sub _retry_after {
Subroutine _retry_after: called CC=5 SCAR=16.1
133 95 my ($value) = @_;
134 95 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 79 16 95
135 16 $value =~ s/^\s+|\s+\z//g;
136 16 ▶ return $value + 0 if $value =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A[0-9]+\z/ 6 10 16
137  
138 10 my $epoch = _http_date($value);
139 10 ▶ return undef unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 7 3 10
140 3 my $seconds = $epoch - time;
141 3 ▶ return $seconds > 0 ? $seconds : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seconds > 0 ? : 3 0 3
142 }
143  
144 # IMF-fixdate (Sun, 06 Nov 1994 08:49:37 GMT), RFC 850 (Sunday, 06-Nov-94
145 # 08:49:37 GMT) and asctime (Sun Nov 6 08:49:37 1994), as epoch seconds.
146 10 ▶sub _http_date {
Subroutine _http_date: called CC=6 SCAR=18.0
147 10 my ($value) = @_;
148 10 my ($day, $month, $year, $hour, $minute, $second);
149 10 ▶ if ($value =~ /\A(?:$DAY), ([0-9]{2}) ($MON) ([0-9]{4}) $TIME GMT\z/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ /\A(?:$DAY), ([0-9]{2}) ($MON) ([0-9]{4}) $TIME GMT\z/) { } 2 8 10
elsif ($value =~ /\A(?:$WEEKDAY), ([0-9]{2})-($MON)-([0-9]{2}) $TIME GMT\z/) { } 1 7 8
elsif ($value =~ /\A(?:$DAY) ($MON) ( [1-9]|[0-9]{2}) $TIME ([0-9]{4})\z/) { } 1 6 7
150 2 ($day, $month, $year, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6);
151 }
152 elsif ($value =~ /\A(?:$WEEKDAY), ([0-9]{2})-($MON)-([0-9]{2}) $TIME GMT\z/) {
153 1 ($day, $month, $year, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6);
154 # A two-digit year more than 50 years ahead is the most recent past one
155 # with those digits, as RFC 9110 has a recipient read it.
156 1 my $now = (gmtime)[5] + 1900;
157 1 $year += $now - $now % 100;
158 1 ▶ $year -= 100 if $year > $now + 50;
Branch: 1 branch
BranchTrueFalseTotal
if $year > $now + 50 0 1 1
159 }
160 elsif ($value =~ /\A(?:$DAY) ($MON) ( [1-9]|[0-9]{2}) $TIME ([0-9]{4})\z/) {
161 1 ($month, $day, $hour, $minute, $second, $year) = ($1, $2, $3, $4, $5, $6);
162 }
163 else {
164 6 return undef;
165 }
166 # Already past, and Time::Local reads a year below 1000 as one after 1900.
167 4 ▶ return 0 if $year < 1970;
Branch: 1 branch
BranchTrueFalseTotal
if $year < 1970 0 4 4
168 # Time::Local refuses a day, hour or minute out of range, which is no date.
169 4 my $epoch = eval { Time::Local::timegm($second, $minute, $hour, $day + 0, $MONTH{$month}, $year) };
170 4 return $epoch;
171 }
172  
173 15 1;
174  
175 __END__
« VPNDetection/Oauth.pm ↑ Summary VPNDetection/Bogon.pm »