HTTP/Server.pm

filter: Statementstmt 71.7% 112 / 156 Branchbran 45.0% 45 / 100 Conditioncond 28.2% 13 / 46 MC/DCmcdc 7.6% 2 / 26 Subroutinesub 65.5% 19 / 29 Podpod 0.0% 0 / 10 totaltotal 52.0% 191 / 367 CC 74 SCAR 40.3
CC
74
Cov
56%
new
136.6
_quic_progress
110.0
close
17.7
CRAP 56.3
↑ Summary HTTP/Client.pm »
1 package Linux::Event::HTTP::Server;
2 19 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use Scalar::Util qw(weaken refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Unblock::HTTP::Server 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Linux::Event::IO::Sock::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Linux::Event::Kernel::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Linux::Event::HTTP::_Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Linux::Event::HTTP::_Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 our $VERSION = '0.005';
12  
13 23 ▶sub new ($class, %option) {
Subroutine new: called CC=31 SCAR=49.2
Pod undocumented
14 23 my $loop = delete $option{loop};
15 23 Linux::Event::HTTP::_Config::loop($loop);
16 23 my $stream_class = Linux::Event::HTTP::_Config::stream_class(\%option);
17 23 my %protocols = Linux::Event::HTTP::_Config::protocols(\%option);
18 # Keep the capability check for mixed or incomplete installations.
19 # Check once at construction, before binding, never per request.
20 23 ▶ if ($protocols{http1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocols{"http1"}) 23 0 23
21 23 require Unblock::HTTP1::Server;
22 23 ▶ croak 'new(): Unblock::HTTP1::Server lacks drain() required for HTTP/1 graceful shutdown; install a compatible Unblock::HTTP1 build'
Branch: 1 branch
BranchTrueFalseTotal
unless "Unblock::HTTP1::Server"->can("drain") 1 22 23
23 unless Unblock::HTTP1::Server->can('drain');
24 }
25 22 ▶ my $quic = delete($option{quic}) // {};
Condition50%1/2
delete $option{"quic"} // {}
l!l
022
MC/DC0%0/1
delete $option{"quic"} // {}
delete $option{"quic"}
Truth table50%1/2
A
A= delete $option{"quic"}
Aresult
11
00
26 22 ▶ croak 'new(): quic must be a hash reference' unless ref($quic) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $quic eq "HASH" 0 22 22
27 22 ▶ croak 'new(): quic requires http3 => 1' if %$quic && !$protocols{http3};
Branch: 1 branch
BranchTrueFalseTotal
if %$quic and not $protocols{"http3"} 0 22 22
Condition33%1/3
%$quic and not $protocols{"http3"}
!ll&&!rl&&r
2200
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
28 22 my %callbacks;
29 22 for my $name (qw(on_headers on_request on_error on_websocket on_handoff)) {
30 110 ▶ $callbacks{$name} = delete $option{$name} if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 36 74 110
31 }
32 # H2/H3 Extended CONNECT WebSocket output must be scheduled outside
33 # the native protocol receive stack. H1 Upgrade does not use this hook.
34 0 ▶ my $defer = sub ($callback) { $loop->defer($callback) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 22 my $definition = Unblock::HTTP::Server->new(
36 %protocols, %callbacks, defer => $defer,
37 );
38 22 my $self = bless { loop => $loop, definition => $definition,
39 connections => {} }, $class;
40 22 my $state = { server => $self };
41 22 weaken($state->{server});
42 22 my %stream = (class => $stream_class, data => $state);
43 22 for my $name (qw(tls tuning)) {
44 44 ▶ $stream{$name} = delete $option{$name} if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 5 39 44
45 }
46 croak 'new(): HTTP/2 Server requires tls for ALPN negotiation'
47 22 ▶ if exists($protocols{http2}) && !exists($stream{tls});
Branch: 1 branch
BranchTrueFalseTotal
if exists $protocols{"http2"} and not exists $stream{"tls"} 0 22 22
Condition33%1/3
exists $protocols{"http2"} and not exists $stream{"tls"}
!ll&&!rl&&r
2200
MC/DC0%0/2
exists $protocols{"http2"} and not exists $stream{"tls"}
exists $protocols{"http2"} not exists $stream{"tls"}
Truth table33%1/3
A and not(B)
A= exists $protocols{"http2"}B= not exists $stream{"tls"}
ABresult
0X0
100
111
48 22 ▶ if (exists $protocols{http3}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocols{"http3"}) 0 22 22
49 croak 'new(): HTTP3 requires a concrete host (not a wildcard)'
50 unless defined($option{host})
51 0 ▶ && $option{host} ne '0.0.0.0' && $option{host} ne '::';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"host"} and $option{"host"} ne "0.0.0.0" and $option{"host"} ne "::" 0 0 0
Condition0%0/6
defined $option{"host"} and $option{"host"} ne "0.0.0.0" and $option{"host"} ne "::"
!ll&&!rl&&r
000
defined $option{"host"} and $option{"host"} ne "0.0.0.0"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $option{"host"} and $option{"host"} ne "0.0.0.0" and $option{"host"} ne "::"
defined $option{"host"} $option{"host"} ne "0.0.0.0" $option{"host"} ne "::"
Truth table0%0/4
A and B and C
A= defined $option{"host"}B= $option{"host"} ne "0.0.0.0"C= $option{"host"} ne "::"
ABCresult
0XX0
10X0
1100
1111
52 croak 'new(): HTTP3 requires TLS cert_file and key_file'
53 unless ref($stream{tls}) eq 'HASH'
54 0 ▶ && $stream{tls}{cert_file} && $stream{tls}{key_file};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $stream{"tls"} eq "HASH" and $stream{"tls"}{"cert_file"} and $stream{"tls"}{"key_file"} 0 0 0
Condition0%0/6
ref $stream{"tls"} eq "HASH" and $stream{"tls"}{"cert_file"} and $stream{"tls"}{"key_file"}
!ll&&!rl&&r
000
ref $stream{"tls"} eq "HASH" and $stream{"tls"}{"cert_file"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $stream{"tls"} eq "HASH" and $stream{"tls"}{"cert_file"} and $stream{"tls"}{"key_file"}
ref $stream{"tls"} eq "HASH" $stream{"tls"}{"cert_file"} $stream{"tls"}{"key_file"}
Truth table0%0/4
A and B and C
A= ref $stream{"tls"} eq "HASH"B= $stream{"tls"}{"cert_file"}C= $stream{"tls"}{"key_file"}
ABCresult
0XX0
10X0
1100
1111
55 croak 'new(): HTTP3-only listeners are not implemented yet'
56 0 ▶ unless exists($protocols{http1}) || exists($protocols{http2});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $protocols{"http1"} or exists $protocols{"http2"} 0 0 0
Condition0%0/3
exists $protocols{"http1"} or exists $protocols{"http2"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $protocols{"http1"} or exists $protocols{"http2"}
exists $protocols{"http1"} exists $protocols{"http2"}
Truth table0%0/3
A or B
A= exists $protocols{"http1"}B= exists $protocols{"http2"}
ABresult
1X1
011
000
57 }
58 22 ▶ if (exists $stream{tls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $stream{"tls"}) 4 18 22
59 4 ▶ croak 'new(): tls must be a hash reference' unless ref($stream{tls}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $stream{"tls"} eq "HASH" 0 4 4
60 4 ▶ croak 'new(): HTTP Server owns TLS alpn' if exists $stream{tls}{alpn};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stream{"tls"}{"alpn"} 0 4 4
61 4 $stream{tls} = { %{ $stream{tls} }, alpn => [$definition->alpn_protocols] };
62 }
63 22 my $on_error = delete $option{on_listener_error};
64 22 my %listen;
65 22 for my $name (qw(host port path fh backlog reuseaddr reuseport v6only unlink_on_close)) {
66 198 ▶ $listen{$name} = delete $option{$name} if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 44 154 198
67 }
68 22 ▶ croak 'new(): unknown option: ' . join(', ', sort keys %option) if %option;
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 22 22
69 $self->{listener} = Linux::Event::IO::Sock::Listener->new(
70 loop => $loop, %listen, stream => \%stream,
71 21 ▶ on_accept => sub ($listener, $stream) {
Subroutine __ANON__: called CC=2 SCAR=7.2
72 21 my $server = $state->{server};
73 21 ▶ $server ? $server->_track($stream) : $stream->close;
Branch: 1 branch
BranchTrueFalseTotal
$server ? : 21 0 21
74 },
75 22 ▶ (defined($on_error) ? (on_error => $on_error) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $on_error ? : 0 22 22
76 );
77 22 ▶ if (exists $protocols{http3}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocols{"http3"}) 0 22 22
78 0 require Unblock::HTTP::QUIC;
79 0 require Linux::Event::HTTP::_QUICSocket;
80 my $socket = Linux::Event::HTTP::_QUICSocket->new(
81 loop => $loop, host => $listen{host}, port => $self->port,
82 (defined($on_error) ? (on_error => $on_error) : ()),
83 0 ▶ on_progress => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
84 0 ▶ my $server = $state->{server} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $server = $state->{"server"} or return 0 0 0
85 0 $server->_quic_progress;
86 },
87 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $on_error ? : 0 0 0
88 my $manager = Unblock::HTTP::QUIC->server(
89 server => $definition,
90 certificate_file => $stream{tls}{cert_file},
91 private_key_file => $stream{tls}{key_file},
92 %$quic,
93 send => $socket->send_callback,
94 set_timeout => $socket->set_timeout_callback,
95 0 ▶ on_ready => sub ($engine, $quic_manager) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
96 0 ▶ my $server = $state->{server} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $server = $state->{"server"} or return 0 0 0
97 0 $server->_quic_ready($engine);
98 },
99 0 );
100 0 $self->{quic} = { socket => $socket, manager => $manager };
101 0 $socket->start($manager);
102 }
103 22 return $self;
104 }
105  
106 0 ▶sub loop ($self) { $self->{loop} }
Subroutine loop: not called CC=1 SCAR=6.9
Pod undocumented
107 0 ▶sub listener ($self) { $self->{listener} }
Subroutine listener: not called CC=1 SCAR=6.9
Pod undocumented
108 0 ▶sub host ($self) { $self->{listener}->host }
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
109 21 ▶sub port ($self) { $self->{listener}->port }
Subroutine port: called CC=1 SCAR=0.0
Pod undocumented
110 0 ▶sub pause ($self) { $self->{listener}->pause; return $self }
Subroutine pause: not called CC=1 SCAR=6.9
Pod undocumented
111 0 ▶sub resume ($self) { $self->{listener}->resume; return $self }
Subroutine resume: not called CC=1 SCAR=6.9
Pod undocumented
112 22 ▶sub close ($self) {
Subroutine close: called CC=6 SCAR=28.7
Pod undocumented
113 22 $self->{listener}->close;
114 22 ▶ if ($self->{quic} && !$self->{quic_closing}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"quic"} and not $self->{"quic_closing"}++) 0 22 22
Condition33%1/3
$self->{"quic"} and not $self->{"quic_closing"}++
!ll&&!rl&&r
2200
MC/DC0%0/2
$self->{"quic"} and not $self->{"quic_closing"}++
$self->{"quic"} not $self->{"quic_closing"}++
Truth table33%1/3
A and not(B)
A= $self->{"quic"}B= not $self->{"quic_closing"}++
ABresult
0X0
100
111
115 0 for my $engine ($self->{quic}{manager}->sessions) {
116 0 ▶ $engine->shutdown_notice unless $engine->shutdown_notice_sent;
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->shutdown_notice_sent 0 0 0
117 0 ▶ $engine->shutdown unless $engine->shutdown_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->shutdown_started 0 0 0
118 }
119 0 $self->_quic_progress;
120 }
121 22 return $self;
122 }
123 38 ▶sub connection_count ($self) {
Subroutine connection_count: called CC=2 SCAR=7.0
Pod undocumented
124 38 my $count = scalar keys %{ $self->{connections} };
125 38 ▶ $count += scalar($self->{quic}{manager}->sessions) if $self->{quic};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quic"} 0 38 38
126 38 return $count;
127 }
128  
129 10 ▶sub shutdown ($self, %option) {
Subroutine shutdown: called CC=11 SCAR=24.9
Pod undocumented
130 10 ▶ my $grace = delete($option{grace}) // 30;
Condition100%2/2
delete $option{"grace"} // 30
l!l
82
MC/DC100%1/1
delete $option{"grace"} // 30
delete $option{"grace"}
Truth table100%2/2
A
A= delete $option{"grace"}
Aresult
11
00
131 10 my $complete = delete $option{on_complete};
132 10 ▶ croak 'shutdown(): grace must be a non-negative number of seconds'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $grace and $grace =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u 0 10 10
Condition33%1/3
not ref $grace and $grace =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
!ll&&!rl&&r
0010
MC/DC0%0/2
not ref $grace and $grace =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
not ref $grace $grace =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
Truth table33%1/3
not(A) and B
A= not ref $graceB= $grace =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
ABresult
0X0
100
111
133 unless !ref($grace) && $grace =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/;
134 10 ▶ croak 'shutdown(): on_complete must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $complete and ref $complete ne "CODE" 0 10 10
Condition66%2/3
defined $complete and ref $complete ne "CODE"
!ll&&!rl&&r
190
MC/DC0%0/2
defined $complete and ref $complete ne "CODE"
defined $complete ref $complete ne "CODE"
Truth table66%2/3
A and B
A= defined $completeB= ref $complete ne "CODE"
ABresult
0X0
100
111
135 if defined($complete) && ref($complete) ne 'CODE';
136 10 ▶ croak 'shutdown(): unknown option: ' . join(', ', sort keys %option) if %option;
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 10 10
137 10 ▶ return $self if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 1 9 10
138 9 $self->{shutdown} = 1;
139 9 $self->{shutdown_complete} = $complete;
140 9 $self->close;
141 # A synchronous close can remove a stream while this snapshot is drained.
142 9 for my $stream (values %{ $self->{connections} }) {
143 8 $stream->_http_drain;
144 }
145 9 ▶ if ($self->connection_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_count) 4 5 9
146 4 my $weak = $self;
147 4 weaken($weak);
148 $self->{shutdown_timer} = Linux::Event::Kernel::Timer->new(
149 loop => $self->{loop}, after => $grace,
150 1 ▶ on_timer => sub {
Subroutine __ANON__: called CC=4 SCAR=14.9
151 1 ▶ my $server = $weak or return;
Branch: 1 branch
BranchTrueFalseTotal
my $server = $weak or return 1 0 1
152 1 $_->close for values %{ $server->{connections} };
153 1 ▶ if (my $quic = delete $server->{quic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $quic = delete $server->{"quic"}) 0 1 1
154 0 $quic->{socket}->close;
155 }
156 1 $server->_shutdown_done;
157 },
158 4 );
159 }
160 9 ▶ $self->_quic_progress if $self->{quic};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quic"} 0 9 9
161 9 $self->_shutdown_done;
162 9 return $self;
163 }
164  
165 18 ▶sub _shutdown_done ($self) {
Subroutine _shutdown_done: called CC=7 SCAR=19.7
166 18 ▶ return unless $self->{shutdown} && !$self->connection_count && !$self->{quic};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} and not $self->connection_count and not $self->{"quic"} 5 13 18
Condition66%4/6
$self->{"shutdown"} and not $self->connection_count and not $self->{"quic"}
!ll&&!rl&&r
5013
$self->{"shutdown"} and not $self->connection_count
!ll&&!rl&&r
0513
MC/DC33%1/3
$self->{"shutdown"} and not $self->connection_count and not $self->{"quic"}
$self->{"shutdown"} not $self->connection_count not $self->{"quic"}
Truth table50%2/4
A and not(B) and not(C)
A= $self->{"shutdown"}B= not $self->connection_countC= not $self->{"quic"}
ABCresult
0XX0
10X0
1100
1111
167 13 ▶ return if $self->{shutdown_notified}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown_notified"}++ 4 9 13
168 9 my $timer = delete $self->{shutdown_timer};
169 9 ▶ $timer->cancel if $timer;
Branch: 1 branch
BranchTrueFalseTotal
if $timer 4 5 9
170 9 my $complete = delete $self->{shutdown_complete};
171 9 ▶ $complete->($self) if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 9 0 9
172 9 return;
173 }
174  
175  
176 0 ▶sub _quic_ready ($self, $engine) {
Subroutine _quic_ready: not called CC=3 SCAR=24.8
177 0 ▶ if ($self->{quic_closing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"quic_closing"}) 0 0 0
178 0 ▶ $engine->quic->close unless $engine->quic->closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->quic->closed 0 0 0
179 }
180 0 return;
181 }
182  
183 0 ▶sub _quic_progress ($self) {
Subroutine _quic_progress: not called CC=10 SCAR=47.0
184 0 ▶ my $quic = $self->{quic} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $quic = $self->{"quic"} or return 0 0 0
185 0 ▶ return unless $self->{quic_closing};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quic_closing"} 0 0 0
186 0 my @sessions = $quic->{manager}->sessions;
187 0 for my $engine (@sessions) {
188 0 my $connection = $engine->quic;
189 0 ▶ next if $connection->closed;
Branch: 1 branch
BranchTrueFalseTotal
if $connection->closed 0 0 0
190 0 ▶ if ($self->{shutdown} && ($engine->failed || $engine->drained)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shutdown"} and $engine->failed || $engine->drained) 0 0 0
Condition0%0/6
$self->{"shutdown"} and $engine->failed || $engine->drained
!ll&&!rl&&r
000
$engine->failed or $engine->drained
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"shutdown"} and $engine->failed || $engine->drained
$self->{"shutdown"} $engine->failed $engine->drained
Truth table0%0/4
A and B or C
A= $self->{"shutdown"}B= $engine->failedC= $engine->drained
ABCresult
0XX0
1000
11X1
1011
191 0 $connection->close;
192 }
193 }
194 # If no sessions are admitted, a handshake in progress has no HTTP
195 # application transaction to preserve. Close the UDP listener.
196 0 ▶ if (!@sessions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@sessions) 0 0 0
197 0 delete $self->{quic};
198 0 $quic->{socket}->close;
199 0 ▶ $self->_shutdown_done if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 0 0
200 }
201 0 return;
202 }
203  
204 39 ▶sub _track ($self, $stream) {
Subroutine _track: called CC=3 SCAR=11.4
205 39 ▶ return if $stream->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $stream->is_closed 0 39 39
206 39 ▶ return $stream->close if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 39 39
207 39 $self->{connections}{refaddr($stream)} = $stream;
208 39 return;
209 }
210  
211 18 ▶sub _attach ($self, $stream) {
Subroutine _attach: called CC=2 SCAR=8.4
212 0 ▶ if ($self->{shutdown}) { $stream->close; return }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shutdown"}) 0 18 18
213 18 $self->_track($stream);
214 return $self->{definition}->attach_stream(
215 18 transport => $stream, alpn => $stream->selected_alpn,
216 );
217 }
218  
219 21 ▶sub _closed ($self, $stream) {
Subroutine _closed: called CC=2 SCAR=6.9
220 21 delete $self->{connections}{refaddr($stream)};
221 21 ▶ $self->_shutdown_done if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 8 13 21
222 21 return;
223 }
224  
225 19 1;
226  
227 __END__
↑ Summary HTTP/Client.pm »