| 1 | package Mail::BIMI::Role::HasHTTPClient; | ||||||||||||
| 2 | # ABSTRACT: Class to model a HTTP client | ||||||||||||
| 3 | 29 | our $VERSION = '3.20261116'; # VERSION | |||||||||||
| 4 | 29 | ▶ | use 5.20.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 29 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 29 | ▶ | use Mail::BIMI::Prelude; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 29 | ▶ | use HTTP::Tiny::Paranoid; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 29 | ▶ | use Time::HiRes qw{ualarm}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 29 | has http_client => ( is => 'rw', lazy => 1, builder => '_build_http_client', | |||||||||||
| 11 | documentation => 'HTTP::Tiny::Paranoid (or similar) object used for HTTP operations' ); | ||||||||||||
| 12 | 29 | requires 'http_client_max_fetch_size'; | |||||||||||
| 13 | |||||||||||||
| 14 | |||||||||||||
| 15 | { | ||||||||||||
| 16 | 29 | my $http_client; | |||||||||||
| 17 | 21 | ▶ | sub _build_http_client($self) { | ||||||||||
|
Subroutine
_build_http_client: called CC=3 SCAR=11.0
| |||||||||||||
| 18 | 21 | ▶ | return $http_client if $http_client; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 8 | ▶ | my $agent = 'Mail::BIMI ' . ( $Mail::BIMI::Version // 'dev' ) . '/1.0'; | ||||||||||
|
Condition50%1/2
$Mail::BIMI::Version // "dev"
MC/DC0%0/1
$Mail::BIMI::Version // "dev"
$Mail::BIMI::Version
Truth table50%1/2
A
A= $Mail::BIMI::Version
| |||||||||||||
| 20 | 8 | $http_client = HTTP::Tiny::Paranoid->new( | |||||||||||
| 21 | agent => $agent, | ||||||||||||
| 22 | max_size => $self->http_client_max_fetch_size, | ||||||||||||
| 23 | max_redirect => $self->bimi_object->options->http_client_max_redirect, | ||||||||||||
| 24 | timeout => $self->bimi_object->options->http_client_timeout, | ||||||||||||
| 25 | verify_SSL => 1, # Certificates MUST verify | ||||||||||||
| 26 | default_headers => { | ||||||||||||
| 27 | 'accept-encoding' => 'identity', | ||||||||||||
| 28 | }, | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | 8 | return $http_client; | |||||||||||
| 31 | } | ||||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | 29 | ▶ | sub http_client_get($self, $uri) { | ||||||||||
|
Subroutine
http_client_get: called CC=7 SCAR=21.4
Pod
documented
| |||||||||||||
| 36 | # Set an overall hard timeout 1/10 second longer than the timeout we | ||||||||||||
| 37 | # pass to the client object. | ||||||||||||
| 38 | 21 | my $timeout_microseconds = ($self->bimi_object->options->http_client_timeout * 1000000) + 100000; | |||||||||||
| 39 | |||||||||||||
| 40 | 21 | my $result; | |||||||||||
| 41 | my $old_alarm; | ||||||||||||
| 42 | 21 | my $timed_out = 0; | |||||||||||
| 43 | |||||||||||||
| 44 | # Maintain an internal list of servers giving us a timeout, and do not | ||||||||||||
| 45 | # retry those for the duration of our current overall scope. | ||||||||||||
| 46 | $self->bimi_object->{_http_client_failed_server_list} = {} | ||||||||||||
| 47 | 21 | ▶ | unless exists $self->bimi_object->{_http_client_failed_server_list}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | |||||||||||||
| 49 | 21 | my ($server) = $uri =~ m/^[^:]*:\/\/([^\/]*)\/.*/; | |||||||||||
| 50 | 21 | ▶ | $server = lc ($server // ''); | ||||||||||
|
Condition50%1/2
$server // ""
MC/DC0%0/1
$server // ""
$server
Truth table50%1/2
A
A= $server
| |||||||||||||
| 51 | |||||||||||||
| 52 | 21 | ▶ | if (exists $self->bimi_object->{_http_client_failed_server_list}->{$server}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | return { | ||||||||||||
| 54 | 0 | success => 0, | |||||||||||
| 55 | status => 599, | ||||||||||||
| 56 | }; | ||||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 21 | eval{ | |||||||||||
| 60 | 21 | $old_alarm = ualarm($timeout_microseconds); | |||||||||||
| 61 | 0 | ▶ | local $SIG{ALRM} = sub { $timed_out = 1; die; }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||
| 62 | 21 | $result = $self->http_client->get($uri); | |||||||||||
| 63 | }; | ||||||||||||
| 64 | 21 | my $error = $@; | |||||||||||
| 65 | 21 | ualarm($old_alarm); | |||||||||||
| 66 | |||||||||||||
| 67 | 21 | ▶ | if ($timed_out) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | $self->bimi_object->{_http_client_failed_server_list}->{$server} = 1; | |||||||||||
| 69 | return { | ||||||||||||
| 70 | 0 | success => 0, | |||||||||||
| 71 | status => 599, | ||||||||||||
| 72 | }; | ||||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 21 | ▶ | die $error if $error; # rethrow if the error wasn't our timeout | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | |||||||||||||
| 77 | 21 | ▶ | $self->bimi_object->{_http_client_failed_server_list}->{$server} = 1 if $result->{status} == 599; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | |||||||||||||
| 79 | 21 | return $result; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 29 | 1; | |||||||||||
| 83 | |||||||||||||
| 84 | __END__ | ||||||||||||