HTTP/Client.pm

filter: Statementstmt 56.0% 60 / 107 Branchbran 21.4% 12 / 56 Conditioncond 20.5% 7 / 34 MC/DCmcdc 9.0% 2 / 22 Subroutinesub 57.1% 16 / 28 Podpod 0.0% 0 / 7 totaltotal 38.1% 97 / 254 CC 43 SCAR 36.5
CC
43
Cov
40%
new
93.2
__ANON__
72.0
__ANON__
42.0
CRAP 38.7
« HTTP/Server.pm ↑ Summary HTTP/_Stream.pm »
1 package Linux::Event::HTTP::Client;
2 16 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use parent 'Unblock::HTTP::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Linux::Event::HTTP::_Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Linux::Event::HTTP::_Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Linux::Event::Kernel::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Scalar::Util qw(weaken refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 our $VERSION = '0.005';
11  
12 11 ▶sub new ($class, %option) {
Subroutine new: called CC=16 SCAR=45.3
Pod undocumented
13 11 my $loop = delete $option{loop};
14 11 Linux::Event::HTTP::_Config::loop($loop);
15 11 my $stream_class = Linux::Event::HTTP::_Config::stream_class(\%option);
16 11 my %protocols = Linux::Event::HTTP::_Config::protocols(\%option);
17 11 ▶ my $tls = delete($option{tls}) // {};
Condition100%2/2
delete $option{"tls"} // {}
l!l
38
MC/DC100%1/1
delete $option{"tls"} // {}
delete $option{"tls"}
Truth table100%2/2
A
A= delete $option{"tls"}
Aresult
11
00
18 11 ▶ croak 'new(): tls must be a hash reference' unless ref($tls) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tls eq "HASH" 0 11 11
19 croak 'new(): HTTP Client owns TLS server_name and alpn'
20 11 ▶ if exists($tls->{server_name}) || exists($tls->{alpn});
Branch: 1 branch
BranchTrueFalseTotal
if exists $tls->{"server_name"} or exists $tls->{"alpn"} 0 11 11
Condition33%1/3
exists $tls->{"server_name"} or exists $tls->{"alpn"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
exists $tls->{"server_name"} or exists $tls->{"alpn"}
exists $tls->{"server_name"} exists $tls->{"alpn"}
Truth table33%1/3
A or B
A= exists $tls->{"server_name"}B= exists $tls->{"alpn"}
ABresult
1X1
011
000
21 11 $tls = { %$tls };
22 11 ▶ my $quic = delete($option{quic}) // {};
Condition50%1/2
delete $option{"quic"} // {}
l!l
011
MC/DC0%0/1
delete $option{"quic"} // {}
delete $option{"quic"}
Truth table50%1/2
A
A= delete $option{"quic"}
Aresult
11
00
23 11 ▶ croak 'new(): quic must be a hash reference' unless ref($quic) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $quic eq "HASH" 0 11 11
24 11 ▶ croak 'new(): quic requires http3 => 1' if %$quic && !$protocols{http3};
Branch: 1 branch
BranchTrueFalseTotal
if %$quic and not $protocols{"http3"} 0 11 11
Condition33%1/3
%$quic and not $protocols{"http3"}
!ll&&!rl&&r
1100
MC/DC0%0/2
%$quic and not $protocols{"http3"}
%$quic not $protocols{"http3"}
Truth table33%1/3
A and not(B)
A= %$quicB= not $protocols{"http3"}
ABresult
0X0
100
111
25 11 for my $key (keys %$quic) {
26 0 ▶ croak "new(): unknown quic option $key"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\A(?:ca_file|transport|version)\z/u 0 0 0
27 unless $key =~ /\A(?:ca_file|transport|version)\z/;
28 }
29 11 $quic = { %$quic };
30 $quic->{ca_file} = $tls->{ca_file}
31 11 ▶ if !exists($quic->{ca_file}) && exists($tls->{ca_file});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $quic->{"ca_file"} and exists $tls->{"ca_file"} 2 9 11
Condition66%2/3
not exists $quic->{"ca_file"} and exists $tls->{"ca_file"}
!ll&&!rl&&r
092
MC/DC50%1/2
not exists $quic->{"ca_file"} and exists $tls->{"ca_file"}
not exists $quic->{"ca_file"} exists $tls->{"ca_file"}
Truth table66%2/3
not(A) and B
A= not exists $quic->{"ca_file"}B= exists $tls->{"ca_file"}
ABresult
0X0
100
111
32 11 my %quic_connections;
33 11 for my $name (qw(connect_stream connect_quic defer schedule_timer release_connection)) {
34 55 ▶ die "new(): $name is owned by Linux::Event::HTTP\n" if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 0 55 55
35 }
36 my $self = $class->SUPER::new(%option, %protocols,
37 7 ▶ defer => sub ($callback) { $loop->defer($callback); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 7 ▶ schedule_timer => sub ($seconds, $callback) {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 my $timer = Linux::Event::Kernel::Timer->new(
40 loop => $loop, after => $seconds,
41 1 ▶ on_timer => sub ($timer) { $callback->() },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 7 );
43 7 ▶ return sub { $timer->cancel };
Subroutine __ANON__: called CC=1 SCAR=0.0
44 },
45 12 ▶ connect_stream => sub ($origin, $alpn, $ready) {
Subroutine __ANON__: called CC=3 SCAR=11.1
46 12 my %transport;
47 12 my $verified = 0;
48 12 ▶ if ($origin->{scheme} eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin->{"scheme"} eq "https") 3 9 12
49 3 require Linux::Event::TLS;
50 $transport{transport} = Linux::Event::TLS->client(
51 3 %$tls, server_name => $origin->{host}, alpn => $alpn,
52 );
53 3 ▶ $verified = exists($tls->{verify}) ? $tls->{verify} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $tls->{'verify'} ? : 0 3 3
54 }
55 my $stream = $stream_class->connect(
56 loop => $loop, host => $origin->{host}, port => $origin->{port},
57 12 %transport, data => { ready => $ready, verified_origin => $verified },
58 );
59 0 ▶ return sub { $stream->close };
Subroutine __ANON__: not called CC=1 SCAR=1.2
60 },
61 ($protocols{http3} ? (
62 0 ▶ connect_quic => sub ($origin, $ready) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
63 0 require Net::QUIC::Driver;
64 0 require Linux::Event::HTTP::_QUICSocket;
65 0 my $record = { cancelled => 0, completed => 0 };
66 0 my $weak = $record;
67 0 weaken($weak);
68 0 ▶ my $fail = sub ($error) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
69 0 ▶ my $state = $weak or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $weak or return 0 0 0
70 0 ▶ return if $state->{cancelled} || $state->{completed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} or $state->{"completed"}++ 0 0 0
Condition0%0/3
$state->{"cancelled"} or $state->{"completed"}++
l!l&&r!l&&!r
000
MC/DC0%0/2
$state->{"cancelled"} or $state->{"completed"}++
$state->{"cancelled"} $state->{"completed"}++
Truth table0%0/3
A or B
A= $state->{"cancelled"}B= $state->{"completed"}++
ABresult
1X1
011
000
71 0 ▶ $state->{socket}->close if $state->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"socket"} 0 0 0
72 0 $ready->(error => "$error");
73 0 };
74 my $socket = Linux::Event::HTTP::_QUICSocket->connect(
75 loop => $loop,
76 host => ($origin->{alternative_host} // $origin->{host}),
77 port => ($origin->{alternative_port} // $origin->{port}),
78 0 ▶ on_ready => sub ($bridge) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
79 0 ▶ my $state = $weak or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $weak or return 0 0 0
80 0 ▶ return if $state->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} 0 0 0
81 0 my $ok = eval {
82 my $driver = Net::QUIC::Driver->client(
83 local => $bridge->local, peer => $bridge->peer,
84 0 server_name => $origin->{host}, alpn => 'h3',
85 %$quic,
86 send => $bridge->send_callback,
87 set_timeout => $bridge->set_timeout_callback,
88 );
89 0 my $connection = $driver->connection;
90 0 $state->{driver} = $driver;
91 0 $state->{connection} = $connection;
92 0 $bridge->start($driver);
93 0 1;
94 };
95 0 ▶ $fail->("$@") unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
96 },
97 0 ▶ on_progress => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
98 0 ▶ my $state = $weak or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $weak or return 0 0 0
99 0 ▶ return if $state->{cancelled} || $state->{completed};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} or $state->{"completed"} 0 0 0
Condition0%0/3
$state->{"cancelled"} or $state->{"completed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$state->{"cancelled"} or $state->{"completed"}
$state->{"cancelled"} $state->{"completed"}
Truth table0%0/3
A or B
A= $state->{"cancelled"}B= $state->{"completed"}
ABresult
1X1
011
000
100 0 ▶ my $connection = $state->{connection} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $state->{"connection"} or return 0 0 0
101 0 ▶ if ($connection->ready && !$connection->closed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($connection->ready and not $connection->closed) { } 0 0 0
elsif ($connection->closed) { } 0 0 0
Condition0%0/3
$connection->ready and not $connection->closed
!ll&&!rl&&r
000
MC/DC0%0/2
$connection->ready and not $connection->closed
$connection->ready not $connection->closed
Truth table0%0/3
A and not(B)
A= $connection->readyB= not $connection->closed
ABresult
0X0
100
111
102 0 $state->{completed} = 1;
103 0 $quic_connections{refaddr($connection)} = $state;
104 0 $ready->(connection => $connection, verified_origin => 1);
105 } elsif ($connection->closed) {
106 0 $fail->('QUIC connection closed before authentication');
107 }
108 },
109 0 ▶ on_error => sub ($udp, $error) { $fail->("$error") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 ▶ );
Condition0%0/6
$origin->{"alternative_host"} // $origin->{"host"}
l!l&&r!l&&!r
000
$origin->{"alternative_port"} // $origin->{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$origin->{"alternative_host"} // $origin->{"host"}
$origin->{"alternative_host"} $origin->{"host"}
MC/DC0%0/2
$origin->{"alternative_port"} // $origin->{"port"}
$origin->{"alternative_port"} $origin->{"port"}
Truth table0%0/3
A // B
A= $origin->{"alternative_host"}B= $origin->{"host"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $origin->{"alternative_port"}B= $origin->{"port"}
ABresult
1X1
011
000
111 0 $record->{socket} = $socket;
112 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
113 0 ▶ return if $record->{cancelled}++;
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"cancelled"}++ 0 0 0
114 delete $quic_connections{refaddr($record->{connection})}
115 0 ▶ if $record->{connection};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"connection"} 0 0 0
116 $record->{connection}->close
117 0 ▶ if $record->{connection} && !$record->{connection}->closed;
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"connection"} and not $record->{"connection"}->closed 0 0 0
Condition0%0/3
$record->{"connection"} and not $record->{"connection"}->closed
!ll&&!rl&&r
000
MC/DC0%0/2
$record->{"connection"} and not $record->{"connection"}->closed
$record->{"connection"} not $record->{"connection"}->closed
Truth table0%0/3
A and not(B)
A= $record->{"connection"}B= not $record->{"connection"}->closed
ABresult
0X0
100
111
118 0 ▶ $record->{socket}->close if $record->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"socket"} 0 0 0
119 0 };
120 },
121 ) : ()),
122 9 ▶ release_connection => sub ($host, $engine, $protocol, $reason) {
Subroutine __ANON__: called CC=5 SCAR=28.4
123 9 ▶ if ($protocol eq 'h3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq 'h3') { } 0 9 9
124 0 my $record = delete $quic_connections{refaddr($host)};
125 0 ▶ $host->close unless $host->closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $host->closed 0 0 0
126 0 ▶ $record->{socket}->close if $record && $record->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $record and $record->{"socket"} 0 0 0
Condition0%0/3
$record and $record->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
$record and $record->{"socket"}
$record $record->{"socket"}
Truth table0%0/3
A and B
A= $recordB= $record->{"socket"}
ABresult
0X0
100
111
127 } else {
128 9 $host->close;
129 }
130 },
131 11 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$protocols{'http3'} ? : 0 11 11
132 11 $self->{linux_event_loop} = $loop;
133 11 return $self;
134 }
135  
136 0 ▶sub _release_transport ($stream, $engine, $protocol, $reason) {
Subroutine _release_transport: not called CC=1 SCAR=6.9
137 0 $stream->close;
138 0 return;
139 }
140  
141 0 ▶sub loop ($self) { $self->{linux_event_loop} }
Subroutine loop: not called CC=1 SCAR=6.9
Pod undocumented
142 10 ▶sub get ($self, $url, %option) { $self->request(%option, method => 'GET', url => $url) }
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
143 0 ▶sub head ($self, $url, %option) { $self->request(%option, method => 'HEAD', url => $url) }
Subroutine head: not called CC=1 SCAR=6.9
Pod undocumented
144 3 ▶sub post ($self, $url, %option) { $self->request(%option, method => 'POST', url => $url) }
Subroutine post: called CC=1 SCAR=0.0
Pod undocumented
145 0 ▶sub put ($self, $url, %option) { $self->request(%option, method => 'PUT', url => $url) }
Subroutine put: not called CC=1 SCAR=6.9
Pod undocumented
146 0 ▶sub delete ($self, $url, %option) { $self->request(%option, method => 'DELETE', url => $url) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
147  
148 16 1;
149  
150 __END__
« HTTP/Server.pm ↑ Summary HTTP/_Stream.pm »