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