HTTP/Server.pm

filter: Statementstmt 62.9% 73 / 116 Branchbran 50.0% 37 / 74 Conditioncond 43.2% 16 / 37 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 50.0% 16 / 32 Podpod 95.0% 19 / 20 totaltotal 54.8% 164 / 299 CC 53 SCAR 38.7
CC
53
Cov
56%
new
98.2
_http2_ready
20.0
__ANON__
20.0
CRAP 48.1
« HTTP/_Upgrade.pm ↑ 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 strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use Linux::Event::IO::Sock::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Linux::Event::HTTP::Server::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 our $VERSION = '0.003';
12  
13 67 ▶sub _load_connection_class ($class) {
Subroutine _load_connection_class: called CC=6 SCAR=23.5
14 67 ▶ croak 'new(): connection_class must be a package name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $class or ref $class or not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u 0 67 67
Condition33%2/6
not defined $class or ref $class or not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
l!l&&r!l&&!r
0067
not defined $class or ref $class
l!l&&r!l&&!r
0067
MC/DC0%0/3
not defined $class or ref $class or not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
not defined $class ref $class not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $classB= ref $classC= not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
ABCresult
1XX1
01X1
0011
0000
15 if !defined($class) || ref($class)
16 || $class !~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
17  
18 67 ▶ if (!$class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 0 67 67
19 0 (my $file = "$class.pm") =~ s{::}{/}g;
20 0 require $file;
21 }
22  
23 67 ▶ croak 'new(): connection_class must inherit Linux::Event::HTTP::Server::Connection'
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Linux::Event::HTTP::Server::Connection") 0 67 67
24 if !$class->isa('Linux::Event::HTTP::Server::Connection');
25 67 return $class;
26 }
27  
28 670 ▶sub _take_callback ($name, $option) {
Subroutine _take_callback: called CC=3 SCAR=11.0
29 670 ▶ return undef if !exists $option->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option->{$name} 634 36 670
30 36 my $callback = delete $option->{$name};
31 36 ▶ croak "new(): $name must be a coderef" if ref($callback) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 1 35 36
32 35 return $callback;
33 }
34  
35 0 ▶sub _http2_available () {
Subroutine _http2_available: not called CC=3 SCAR=24.8
36 0 ▶ return 0 if !eval {
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Net::HTTP2::nghttp2; "Net::HTTP2::nghttp2"->VERSION("0.011"); require Linux::Event::HTTP::_HTTP2::Server; require Linux::Event::HTTP::_HTTP2::ServerConnection; 1 } } 0 0 0
37 0 require Net::HTTP2::nghttp2;
38 0 Net::HTTP2::nghttp2->VERSION('0.011');
39 0 require Linux::Event::HTTP::_HTTP2::Server;
40 0 require Linux::Event::HTTP::_HTTP2::ServerConnection;
41 0 1;
42 };
43 0 ▶ return Net::HTTP2::nghttp2->available ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Net::HTTP2::nghttp2'->available ? : 0 0 0
44 }
45  
46 0 ▶sub _http2_ready ($conn, $user_ready, $max_header_list_size) {
Subroutine _http2_ready: not called CC=4 SCAR=30.0
47 0 ▶ if (($conn->selected_alpn // '') ne 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if (($conn->selected_alpn // "") ne "h2") 0 0 0
Condition0%0/2
$conn->selected_alpn // ""
l!l
00
MC/DC0%0/1
$conn->selected_alpn // ""
$conn->selected_alpn
Truth table0%0/2
A
A= $conn->selected_alpn
Aresult
11
00
48 0 ▶ $user_ready->($conn) if $user_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $user_ready 0 0 0
49 0 return;
50 }
51  
52 0 $conn->pause_read;
53 0 ▶ $conn->loop->defer(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
54 0 ▶ return if $conn->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->is_closed 0 0 0
55  
56 my $executor = Linux::Event::HTTP::_HTTP2::Server->new(
57 stream => $conn,
58 connection => $conn,
59 autostart => 0,
60 on_request => $conn->{_http_on_request},
61 on_body => $conn->{_http_on_body},
62 on_request_end => $conn->{_http_on_request_end},
63 0 max_header_list_size => $max_header_list_size,
64 );
65 0 $conn->{_http2_executor} = $executor;
66  
67 0 $conn->transition_to(
68 'Linux::Event::HTTP::_HTTP2::ServerConnection',
69 );
70 0 $executor->start;
71  
72 0 ▶ $user_ready->($conn) if $user_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $user_ready 0 0 0
73 0 ▶ $conn->resume_read if $conn->is_read_paused;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->is_read_paused 0 0 0
74 0 });
75 0 return;
76 }
77  
78 69 ▶sub new ($class, %option) {
Subroutine new: called CC=38 SCAR=45.9
Pod undocumented
79 croak 'new(): stream_class is internal; use connection_class'
80 69 ▶ if exists $option{stream_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"stream_class"} 0 69 69
81 croak 'new(): stream is internal; use connection_class, tuning, tls, and callbacks'
82 69 ▶ if exists $option{stream};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"stream"} 0 69 69
83 croak 'new(): HTTP Server owns on_data; use on_request/on_body callbacks'
84 69 ▶ if exists $option{on_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"on_data"} 1 68 69
85 croak 'new(): HTTP Server cannot use message framing callbacks'
86 68 ▶ if exists($option{on_message}) || exists($option{on_messages});
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"on_message"} or exists $option{"on_messages"} 0 68 68
Condition33%1/3
exists $option{"on_message"} or exists $option{"on_messages"}
l!l&&r!l&&!r
0068
MC/DC0%0/2
exists $option{"on_message"} or exists $option{"on_messages"}
exists $option{"on_message"} exists $option{"on_messages"}
Truth table33%1/3
A or B
A= exists $option{"on_message"}B= exists $option{"on_messages"}
ABresult
1X1
011
000
87 croak 'new(): on_request_final was removed; use on_request and Response->body or Transaction->response_body'
88 68 ▶ if exists $option{on_request_final};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"on_request_final"} 1 67 68
89  
90 67 my $connection_class_option = delete $option{connection_class};
91 67 ▶ my $connection_class = _load_connection_class(
Condition100%2/2
$connection_class_option // "Linux::Event::HTTP::Server::Connection"
l!l
4720
MC/DC100%1/1
$connection_class_option // "Linux::Event::HTTP::Server::Connection"
$connection_class_option
Truth table100%2/2
A
A= $connection_class_option
Aresult
11
00
92 $connection_class_option
93 // 'Linux::Event::HTTP::Server::Connection',
94 );
95  
96 67 ▶ my $http2 = exists($option{http2}) ? delete($option{http2}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'http2'} ? : 0 67 67
97 my $has_http2_max_header_list_size =
98 67 exists $option{http2_max_header_list_size};
99 my $http2_max_header_list_size =
100 $has_http2_max_header_list_size
101 ? delete($option{http2_max_header_list_size})
102 67 ▶ : 65_536;
Branch: 1 branch
BranchTrueFalseTotal
$has_http2_max_header_list_size ? : 0 67 67
103 67 ▶ croak 'new(): http2 must be zero or one'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $http2 or ref $http2 or "$http2" ne 0 and "$http2" ne 1 0 67 67
Condition33%3/9
not defined $http2 or ref $http2 or "$http2" ne 0 and "$http2" ne 1
l!l&&r!l&&!r
0067
not defined $http2 or ref $http2
l!l&&r!l&&!r
0067
"$http2" ne 0 and "$http2" ne 1
!ll&&!rl&&r
6700
MC/DC0%0/4
not defined $http2 or ref $http2 or "$http2" ne 0 and "$http2" ne 1
not defined $http2 ref $http2 "$http2" ne 0 "$http2" ne 1
Truth table20%1/5
not(A) or B or C and D
A= not defined $http2B= ref $http2C= "$http2" ne 0D= "$http2" ne 1
ABCDresult
1XXX1
01XX1
00111
000X0
00100
104 if !defined($http2) || ref($http2)
105 || ("$http2" ne '0' && "$http2" ne '1');
106 67 ▶ $http2 = $http2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$http2 ? : 0 67 67
107 67 ▶ croak 'new(): http2_max_header_list_size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
if ref $http2_max_header_list_size or not "$http2_max_header_list_size" =~ /\A[0-9]+\z/u or $http2_max_header_list_size < 1 0 67 67
Condition33%2/6
ref $http2_max_header_list_size or not "$http2_max_header_list_size" =~ /\A[0-9]+\z/u or $http2_max_header_list_size < 1
l!l&&r!l&&!r
0067
ref $http2_max_header_list_size or not "$http2_max_header_list_size" =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
0067
MC/DC0%0/3
ref $http2_max_header_list_size or not "$http2_max_header_list_size" =~ /\A[0-9]+\z/u or $http2_max_header_list_size < 1
ref $http2_max_header_list_size not "$http2_max_header_list_size" =~ /\A[0-9]+\z/u $http2_max_header_list_size < 1
Truth table25%1/4
A or not(B) or C
A= ref $http2_max_header_list_sizeB= not "$http2_max_header_list_size" =~ /\A[0-9]+\z/uC= $http2_max_header_list_size < 1
ABCresult
1XX1
01X1
0011
0000
108 if ref($http2_max_header_list_size)
109 || "$http2_max_header_list_size" !~ /\A[0-9]+\z/
110 || $http2_max_header_list_size < 1;
111 67 ▶ croak 'new(): http2_max_header_list_size requires http2 => 1'
Branch: 1 branch
BranchTrueFalseTotal
if not $http2 and $has_http2_max_header_list_size 0 67 67
Condition33%1/3
not $http2 and $has_http2_max_header_list_size
!ll&&!rl&&r
0670
MC/DC0%0/2
not $http2 and $has_http2_max_header_list_size
not $http2 $has_http2_max_header_list_size
Truth table33%1/3
not(A) and B
A= not $http2B= $has_http2_max_header_list_size
ABresult
0X0
100
111
112 if !$http2 && $has_http2_max_header_list_size;
113  
114 67 my %callbacks;
115 67 for my $name (qw(on_request on_body on_request_end)) {
116 201 my $callback = _take_callback($name, \%option);
117 201 ▶ $callbacks{$name} = $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 30 171 201
118 }
119  
120 67 my %stream_callback;
121 67 for my $name (qw(
122 on_ready on_transport_ready on_drain on_eof on_error on_close
123 )) {
124 402 my $callback = _take_callback($name, \%option);
125 402 ▶ $stream_callback{$name} = $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 4 398 402
126 }
127  
128 67 my $listener_error = _take_callback('on_listener_error', \%option);
129  
130 66 ▶ my $tuning = exists($option{tuning}) ? delete($option{tuning}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'tuning'} ? : 1 65 66
131 66 ▶ croak 'new(): tuning must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if ref $tuning ne "HASH" 0 66 66
132 if ref($tuning) ne 'HASH';
133  
134 66 my $tls_enabled = exists $option{tls};
135 66 my $tls = delete $option{tls};
136 66 ▶ croak 'new(): tls must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if $tls_enabled and ref $tls ne "HASH" 0 66 66
Condition66%2/3
$tls_enabled and ref $tls ne "HASH"
!ll&&!rl&&r
6330
MC/DC0%0/2
$tls_enabled and ref $tls ne "HASH"
$tls_enabled ref $tls ne "HASH"
Truth table66%2/3
A and B
A= $tls_enabledB= ref $tls ne "HASH"
ABresult
0X0
100
111
137 if $tls_enabled && ref($tls) ne 'HASH';
138 66 ▶ $tls = { %$tls } if $tls_enabled;
Branch: 1 branch
BranchTrueFalseTotal
if $tls_enabled 3 63 66
139  
140 66 ▶ if ($http2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http2) 0 66 66
141 0 ▶ croak 'new(): http2 requires tls'
Branch: 1 branch
BranchTrueFalseTotal
unless $tls_enabled 0 0 0
142 if !$tls_enabled;
143 0 ▶ croak 'new(): http2 currently requires the default connection_class'
Branch: 1 branch
BranchTrueFalseTotal
if defined $connection_class_option 0 0 0
144 if defined($connection_class_option);
145 croak 'new(): http2 owns TLS ALPN selection; do not supply tls => { alpn => ... }'
146 0 ▶ if exists $tls->{alpn};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tls->{"alpn"} 0 0 0
147 0 ▶ croak 'new(): HTTP/2 support requires Net::HTTP2::nghttp2 0.011 or newer'
Branch: 1 branch
BranchTrueFalseTotal
unless _http2_available() 0 0 0
148 if !_http2_available();
149 0 $tls->{alpn} = [ 'h2', 'http/1.1' ];
150  
151 0 my $user_on_ready = $stream_callback{on_ready};
152 0 ▶ $stream_callback{on_ready} = sub ($conn) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 _http2_ready(
154 $conn, $user_on_ready, 0 + $http2_max_header_list_size,
155 );
156 0 };
157 }
158  
159 croak 'new(): HTTP Server requires on_request callback or connection_class method'
160 66 ▶ if !$callbacks{on_request} && !$connection_class->can('on_request');
Branch: 1 branch
BranchTrueFalseTotal
unless $callbacks{"on_request"} or $connection_class->can("on_request") 1 65 66
Condition100%3/3
$callbacks{"on_request"} or $connection_class->can("on_request")
l!l&&r!l&&!r
19461
MC/DC100%2/2
$callbacks{"on_request"} or $connection_class->can("on_request")
$callbacks{"on_request"} $connection_class->can("on_request")
Truth table100%3/3
A or B
A= $callbacks{"on_request"}B= $connection_class->can("on_request")
ABresult
1X1
011
000
161  
162 65 my $data = delete $option{data};
163 65 my $state = bless {
164 callbacks => \%callbacks,
165 data => $data,
166 }, 'Linux::Event::HTTP::Server::_ConnectionState';
167  
168 65 my %stream = (
169 class => $connection_class,
170 data => $state,
171 tuning => $tuning,
172 %stream_callback,
173 );
174 65 ▶ $stream{tls} = $tls if $tls_enabled;
Branch: 1 branch
BranchTrueFalseTotal
if $tls_enabled 3 62 65
175  
176 65 ▶ my $listener = Linux::Event::IO::Sock::Listener->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $listener_error ? : 1 64 65
177 %option,
178 stream => \%stream,
179 (defined($listener_error) ? (on_error => $listener_error) : ()),
180 );
181  
182 63 return bless {
183 listener => $listener,
184 connection_class => $connection_class,
185 data => $data,
186 state => $state,
187 http2 => $http2,
188 http2_max_header_list_size => 0 + $http2_max_header_list_size,
189 }, $class;
190 }
191  
192 1 ▶sub listener ($self) { $self->{listener} }
Subroutine listener: called CC=1 SCAR=0.0
Pod documented
193 1 ▶sub connection_class ($self) { $self->{connection_class} }
Subroutine connection_class: called CC=1 SCAR=0.0
Pod documented
194 1 ▶sub data ($self) { $self->{data} }
Subroutine data: called CC=1 SCAR=0.0
Pod documented
195 0 ▶sub http2 ($self) { !!$self->{http2} }
Subroutine http2: not called CC=1 SCAR=6.9
Pod documented
196 0 ▶sub http2_max_header_list_size ($self) {
Subroutine http2_max_header_list_size: not called CC=1 SCAR=6.9
Pod documented
197 0 return $self->{http2_max_header_list_size};
198 }
199 0 ▶sub loop ($self) { $self->{listener}->loop }
Subroutine loop: not called CC=1 SCAR=6.9
Pod documented
200 0 ▶sub fh ($self) { $self->{listener}->fh }
Subroutine fh: not called CC=1 SCAR=6.9
Pod documented
201 0 ▶sub fd ($self) { $self->{listener}->fd }
Subroutine fd: not called CC=1 SCAR=6.9
Pod documented
202 0 ▶sub host ($self) { $self->{listener}->host }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
203 73 ▶sub port ($self) { $self->{listener}->port }
Subroutine port: called CC=1 SCAR=0.0
Pod documented
204 0 ▶sub path ($self) { $self->{listener}->path }
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
205 0 ▶sub family ($self) { $self->{listener}->family }
Subroutine family: not called CC=1 SCAR=6.9
Pod documented
206 0 ▶sub family_number ($self) { $self->{listener}->family_number }
Subroutine family_number: not called CC=1 SCAR=6.9
Pod documented
207 1 ▶sub is_tcp ($self) { $self->{listener}->is_tcp }
Subroutine is_tcp: called CC=1 SCAR=0.0
Pod documented
208 0 ▶sub is_unix ($self) { $self->{listener}->is_unix }
Subroutine is_unix: not called CC=1 SCAR=6.9
Pod documented
209 0 ▶sub state ($self) { $self->{listener}->state }
Subroutine state: not called CC=1 SCAR=6.9
Pod documented
210  
211 1 ▶sub pause ($self) {
Subroutine pause: called CC=1 SCAR=0.0
Pod documented
212 1 $self->{listener}->pause;
213 1 return $self;
214 }
215  
216 0 ▶sub resume ($self) {
Subroutine resume: not called CC=1 SCAR=6.9
Pod documented
217 0 $self->{listener}->resume;
218 0 return $self;
219 }
220  
221 63 ▶sub close ($self) {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
222 63 $self->{listener}->close;
223 63 return $self;
224 }
225  
226 19 1;
227  
228 __END__
« HTTP/_Upgrade.pm ↑ Summary HTTP/Client.pm »