Mojo/IOLoop/Server.pm

filter: Statementstmt 88.2% 75 / 85 Branchbran 68.4% 26 / 38 Conditioncond 62.8% 22 / 35 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 86.3% 19 / 22 Podpod 100% 7 / 7 totaltotal 76.5% 157 / 205 CC 32 SCAR 27.3
CC
32
Cov
78%
listen
33.7
_accept
8.3
DESTROY
4.0
CRAP 15.3
« Mojo/IOLoop/Subprocess.pm ↑ Summary Mojolicious/Routes/Pattern.pm »
1 package Mojo::IOLoop::Server;
2 33 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 33 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Mojo::IOLoop::TLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶has reactor => sub { Mojo::IOLoop->singleton->reactor }, weak => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 41 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=13.9
16 41 my $self = shift;
17 41 ▶ $ENV{MOJO_REUSE} =~ s/(?:^|\,)\Q$self->{reuse}\E// if $self->{reuse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reuse"} 39 2 41
18 41 ▶ $self->stop if $self->{handle} && $self->reactor;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} and $self->reactor 40 1 41
Condition66%2/3
$self->{"handle"} and $self->reactor
!ll&&!rl&&r
1040
MC/DC50%1/2
$self->{"handle"} and $self->reactor
$self->{"handle"} $self->reactor
Truth table66%2/3
A and B
A= $self->{"handle"}B= $self->reactor
ABresult
0X0
100
111
19 }
20  
21 0 ▶sub generate_port { IO::Socket::IP->new(Listen => 5, LocalAddr => '127.0.0.1')->sockport }
Subroutine generate_port: not called CC=1 SCAR=6.9
Pod documented
22  
23 1 ▶sub handle { shift->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
24  
25 4 ▶sub is_accepting { !!shift->{active} }
Subroutine is_accepting: called CC=1 SCAR=0.0
Pod documented
26  
27 51 ▶sub listen {
Subroutine listen: called CC=21 SCAR=35.2
Pod documented
28 51 ▶ my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 47 4 51
29  
30 # Look for reusable file descriptor
31 51 my $path = $args->{path};
32 51 ▶ my $address = $args->{address} || '0.0.0.0';
Condition50%1/2
$args->{'address'} || '0.0.0.0'
l!l
510
MC/DC0%0/1
$args->{'address'} || '0.0.0.0'
$args->{'address'}
Truth table50%1/2
A
A= $args->{'address'}
Aresult
11
00
33 51 my $port = $args->{port};
34 51 ▶ $ENV{MOJO_REUSE} ||= '';
Condition100%2/2
$ENV{"MOJO_REUSE"} ||= ""
l!l
3318
MC/DC100%1/1
$ENV{"MOJO_REUSE"} ||= ""
$ENV{"MOJO_REUSE"}
Truth table100%2/2
A
A= $ENV{"MOJO_REUSE"}
Aresult
11
00
35 my $fd = ($path && $ENV{MOJO_REUSE} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/)
36 51 ▶ || ($port && $ENV{MOJO_REUSE} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$path && $ENV{'MOJO_REUSE'} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u || $port && $ENV{'MOJO_REUSE'} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u ? : 0 51 51
Condition33%3/9
$path and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u or $port and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
l!l&&r!l&&!r
0051
$path and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u
!ll&&!rl&&r
5100
$port and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
!ll&&!rl&&r
5100
MC/DC0%0/4
$path and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u or $port and $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
$path $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/u $port $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
Truth table14%1/7
A and B or C and D
A= $pathB= $ENV{"MOJO_REUSE"} =~ /(?:^|\,)unix:\Q$path\E:(\d+)/uC= $portD= $ENV{"MOJO_REUSE"} =~ /(?:^|\,)\Q$address:$port\E:(\d+)/u
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
37  
38 # Allow file descriptor inheritance
39 51 local $^F = 1023;
40  
41 # Reuse file descriptor
42 51 my $handle;
43 51 ▶ my $class = $path ? 'IO::Socket::UNIX' : 'IO::Socket::IP';
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 51 51
44 51 ▶ if (defined($fd //= $args->{fd})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($fd //= $args->{'fd'})) { } 1 50 51
Condition66%2/3
$fd //= $args->{"fd"}
l!l&&r!l&&!r
0150
MC/DC50%1/2
$fd //= $args->{"fd"}
$fd $args->{"fd"}
Truth table66%2/3
A //= B
A= $fdB= $args->{"fd"}
ABresult
1X1
011
000
45 1 ▶ $handle = $class->new_from_fd($fd, 'r') or croak "Can't open file descriptor $fd: $!";
Branch: 1 branch
BranchTrueFalseTotal
$handle = $class->new_from_fd($fd, "r") or croak("Can't open file descriptor ${fd}: $!") 1 0 1
46 }
47  
48 else {
49 50 ▶ my %options = (Listen => $args->{backlog} // SOMAXCONN, Type => SOCK_STREAM);
Condition50%1/2
$args->{"backlog"} // 4096
l!l
050
MC/DC0%0/1
$args->{"backlog"} // 4096
$args->{"backlog"}
Truth table50%1/2
A
A= $args->{"backlog"}
Aresult
11
00
50  
51 # UNIX domain socket
52 50 my $reuse;
53 50 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 50 50
54 0 ▶ path($path)->remove if -S $path;
Branch: 1 branch
BranchTrueFalseTotal
if -S $path 0 0 0
55 0 $options{Local} = $path;
56 0 ▶ $handle = $class->new(%options) or croak "Can't create listen socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
$handle = $class->new(%options) or croak("Can't create listen socket: $!") 0 0 0
57 0 $reuse = $self->{reuse} = join ':', 'unix', $path, fileno $handle;
58 }
59  
60 # IP socket
61 else {
62 50 $options{LocalAddr} = $address;
63 50 $options{LocalAddr} =~ y/[]//d;
64 50 ▶ $options{LocalPort} = $port if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 50 50
65 50 $options{ReuseAddr} = 1;
66 50 $options{ReusePort} = $args->{reuse};
67 50 ▶ $handle = $class->new(%options) or croak "Can't create listen socket: $@";
Branch: 1 branch
BranchTrueFalseTotal
$handle = $class->new(%options) or croak("Can't create listen socket: $@") 50 0 50
68 50 $fd = fileno $handle;
69 50 $reuse = $self->{reuse} = join ':', $address, $handle->sockport, $fd;
70 }
71  
72 50 ▶ $ENV{MOJO_REUSE} .= length $ENV{MOJO_REUSE} ? ",$reuse" : "$reuse";
Branch: 1 branch
BranchTrueFalseTotal
length $ENV{'MOJO_REUSE'} ? : 32 18 50
73 }
74 51 $handle->blocking(0);
75 51 @$self{qw(args handle)} = ($args, $handle);
76  
77 51 ▶ croak 'IO::Socket::SSL 2.009+ required for TLS support' if !Mojo::IOLoop::TLS->can_tls && $args->{tls};
Branch: 1 branch
BranchTrueFalseTotal
if not "Mojo::IOLoop::TLS"->can_tls and $args->{"tls"} 1 50 51
Condition100%3/3
not "Mojo::IOLoop::TLS"->can_tls and $args->{"tls"}
!ll&&!rl&&r
40101
MC/DC100%2/2
not "Mojo::IOLoop::TLS"->can_tls and $args->{"tls"}
not "Mojo::IOLoop::TLS"->can_tls $args->{"tls"}
Truth table100%3/3
not(A) and B
A= not "Mojo::IOLoop::TLS"->can_tlsB= $args->{"tls"}
ABresult
0X0
100
111
78 }
79  
80 50 ▶sub port { shift->{handle}->sockport }
Subroutine port: called CC=1 SCAR=0.0
Pod documented
81  
82 111 ▶sub start {
Subroutine start: called CC=2 SCAR=7.0
Pod documented
83 111 my $self = shift;
84 111 weaken $self;
85 111 ▶ ++$self->{active} and $self->reactor->io($self->{handle} => sub { $self->_accept })->watch($self->{handle}, 1, 0);
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"active"} and $self->reactor->io($self->{"handle"}, sub { $self->_accept; } )->watch($self->{"handle"}, 1, 0) 111 0 111
86 }
87  
88 135 ▶sub stop { delete($_[0]{active}) and $_[0]->reactor->remove($_[0]{handle}) }
Subroutine stop: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
delete $_[0]{"active"} and $_[0]->reactor->remove($_[0]{"handle"}) 100 35 135
89  
90 39 ▶sub _accept {
Subroutine _accept: called CC=7 SCAR=21.2
91 39 my $self = shift;
92  
93 # Greedy accept
94 39 my $args = $self->{args};
95 39 my $accepted = 0;
96 39 ▶ while ($self->{active} && !($args->{single_accept} && $accepted++)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"active"} and not $args->{"single_accept"} && $accepted++) 73 5 78
Condition100%6/6
$self->{"active"} and not $args->{"single_accept"} && $accepted++
!ll&&!rl&&r
3273
$args->{'single_accept'} && $accepted++
!ll&&!rl&&r
6942
MC/DC100%3/3
$self->{"active"} and not $args->{"single_accept"} && $accepted++
$self->{"active"} $args->{'single_accept'} $accepted++
Truth table100%4/4
A and not(B && C)
A= $self->{"active"}B= $args->{'single_accept'}C= $accepted++
ABCresult
0XX0
1110
10X1
1101
97 73 ▶ return unless my $handle = $self->{handle}->accept;
Branch: 1 branch
BranchTrueFalseTotal
unless my $handle = $self->{"handle"}->accept 34 39 73
98 39 $handle->blocking(0);
99  
100 # Disable Nagle's algorithm
101 39 setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;
102  
103 39 ▶ $self->emit(accept => $handle) and next unless $args->{tls};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"tls"} 39 0 39
Condition40%2/5
$args->{'tls'} || $self->emit('accept', $handle) && (next)
l!l
0390
$self->emit("accept", $handle) and next
!ll
039
MC/DC0%0/2
$args->{'tls'} || $self->emit('accept', $handle) && (next)
$args->{'tls'} $self->emit("accept", $handle)
Truth table33%1/3
A || B
A= $args->{'tls'}B= $self->emit("accept", $handle)
ABresult
1X1
011
000
104  
105 # Start TLS handshake
106 0 my $tls = Mojo::IOLoop::TLS->new($handle)->reactor($self->reactor);
107 0 ▶ $tls->on(upgrade => sub { $self->emit(accept => pop) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 ▶ $tls->on(error => sub { });
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 $tls->negotiate(%$args, server => 1);
110 }
111 }
112  
113 33 1;
114  
115 =encoding utf8
116  
117 =head1 NAME
118  
119 Mojo::IOLoop::Server - Non-blocking TCP and UNIX domain socket server
120  
121 =head1 SYNOPSIS
122  
123 use Mojo::IOLoop::Server;
124  
125 # Create listen socket
126 my $server = Mojo::IOLoop::Server->new;
127 $server->on(accept => sub ($server, $handle) {...});
128 $server->listen(port => 3000);
129  
130 # Start and stop accepting connections
131 $server->start;
132 $server->stop;
133  
134 # Start reactor if necessary
135 $server->reactor->start unless $server->reactor->is_running;
136  
137 =head1 DESCRIPTION
138  
139 L<Mojo::IOLoop::Server> accepts TCP/IP and UNIX domain socket connections for L<Mojo::IOLoop>.
140  
141 =head1 EVENTS
142  
143 L<Mojo::IOLoop::Server> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
144  
145 =head2 accept
146  
147 $server->on(accept => sub ($server, $handle) {...});
148  
149 Emitted for each accepted connection.
150  
151 =head1 ATTRIBUTES
152  
153 L<Mojo::IOLoop::Server> implements the following attributes.
154  
155 =head2 reactor
156  
157 my $reactor = $server->reactor;
158 $server = $server->reactor(Mojo::Reactor::Poll->new);
159  
160 Low-level event reactor, defaults to the C<reactor> attribute value of the global L<Mojo::IOLoop> singleton. Note that
161 this attribute is weakened.
162  
163 =head1 METHODS
164  
165 L<Mojo::IOLoop::Server> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
166  
167 =head2 generate_port
168  
169 my $port = Mojo::IOLoop::Server->generate_port;
170  
171 Find a free TCP port, primarily used for tests.
172  
173 =head2 handle
174  
175 my $handle = $server->handle;
176  
177 Get handle for server, usually an L<IO::Socket::IP> object.
178  
179 =head2 is_accepting
180  
181 my $bool = $server->is_accepting;
182  
183 Check if connections are currently being accepted.
184  
185 =head2 listen
186  
187 $server->listen(port => 3000);
188 $server->listen({port => 3000});
189  
190 Create a new listen socket. Note that TLS support depends on L<IO::Socket::SSL> (2.009+).
191  
192 These options are currently available:
193  
194 =over 2
195  
196 =item address
197  
198 address => '127.0.0.1'
199  
200 Local address to listen on, defaults to C<0.0.0.0>.
201  
202 =item backlog
203  
204 backlog => 128
205  
206 Maximum backlog size, defaults to C<SOMAXCONN>.
207  
208 =item fd
209  
210 fd => 3
211  
212 File descriptor with an already prepared listen socket.
213  
214 =item path
215  
216 path => '/tmp/myapp.sock'
217  
218 Path for UNIX domain socket to listen on.
219  
220 =item port
221  
222 port => 80
223  
224 Port to listen on, defaults to a random port.
225  
226 =item reuse
227  
228 reuse => 1
229  
230 Allow multiple servers to use the same port with the C<SO_REUSEPORT> socket option.
231  
232 =item single_accept
233  
234 single_accept => 1
235  
236 Only accept one connection at a time.
237  
238 =item tls
239  
240 tls => 1
241  
242 Enable TLS.
243  
244 =item tls_ca
245  
246 tls_ca => '/etc/tls/ca.crt'
247  
248 Path to TLS certificate authority file.
249  
250 =item tls_cert
251  
252 tls_cert => '/etc/tls/server.crt'
253 tls_cert => {'mojolicious.org' => '/etc/tls/mojo.crt'}
254  
255 Path to the TLS cert file, defaults to a built-in test certificate.
256  
257 =item tls_key
258  
259 tls_key => '/etc/tls/server.key'
260 tls_key => {'mojolicious.org' => '/etc/tls/mojo.key'}
261  
262 Path to the TLS key file, defaults to a built-in test key.
263  
264 =item tls_options
265  
266 tls_options => {SSL_alpn_protocols => ['foo', 'bar'], SSL_verify_mode => 0x00}
267  
268 Additional options for L<IO::Socket::SSL>.
269  
270 =back
271  
272 =head2 port
273  
274 my $port = $server->port;
275  
276 Get port this server is listening on.
277  
278 =head2 start
279  
280 $server->start;
281  
282 Start or resume accepting connections.
283  
284 =head2 stop
285  
286 $server->stop;
287  
288 Stop accepting connections.
289  
290 =head1 SEE ALSO
291  
292 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
293  
294 =cut
« Mojo/IOLoop/Subprocess.pm ↑ Summary Mojolicious/Routes/Pattern.pm »