Mojo/Server/Daemon.pm

filter: Statementstmt 88.0% 169 / 192 Branchbran 66.6% 60 / 90 Conditioncond 60.9% 25 / 41 MC/DCmcdc 39.1% 9 / 23 Subroutinesub 80.0% 32 / 40 Podpod 100% 4 / 4 totaltotal 76.6% 299 / 390 CC 68 SCAR 25.5
CC
68
Cov
79%
_listen
49.9
_finish
9.1
__ANON__
8.7
CRAP 12.8
« Mojolicious/Commands.pm ↑ Summary Mojo/Cookie/Response.pm »
1 package Mojo::Server::Daemon;
2 23 ▶use Mojo::Base 'Mojo::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 23 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Mojo::Transaction::WebSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Mojo::Util qw(term_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Mojo::WebSocket qw(server_handshake);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 ▶use constant DEBUG => $ENV{MOJO_SERVER_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_SERVER_DEBUG'} || 0
l!l
023
MC/DC0%0/1
$ENV{'MOJO_SERVER_DEBUG'} || 0
$ENV{'MOJO_SERVER_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_SERVER_DEBUG'}
Aresult
11
00
13  
14 63 ▶has acceptors => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 23 has [qw(backlog max_clients silent)];
16 25 ▶has inactivity_timeout => sub { $ENV{MOJO_INACTIVITY_TIMEOUT} // 30 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOJO_INACTIVITY_TIMEOUT"} // 30
l!l
223
MC/DC100%1/1
$ENV{"MOJO_INACTIVITY_TIMEOUT"} // 30
$ENV{"MOJO_INACTIVITY_TIMEOUT"}
Truth table100%2/2
A
A= $ENV{"MOJO_INACTIVITY_TIMEOUT"}
Aresult
11
00
17 23 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
18 23 ▶has keep_alive_timeout => sub { $ENV{MOJO_KEEP_ALIVE_TIMEOUT} // 5 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOJO_KEEP_ALIVE_TIMEOUT"} // 5
l!l
221
MC/DC100%1/1
$ENV{"MOJO_KEEP_ALIVE_TIMEOUT"} // 5
$ENV{"MOJO_KEEP_ALIVE_TIMEOUT"}
Truth table100%2/2
A
A= $ENV{"MOJO_KEEP_ALIVE_TIMEOUT"}
Aresult
11
00
19 3 ▶has listen => sub { [split /,/, $ENV{MOJO_LISTEN} || 'http://*:3000'] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MOJO_LISTEN'} || 'http://*:3000'
l!l
21
MC/DC100%1/1
$ENV{'MOJO_LISTEN'} || 'http://*:3000'
$ENV{'MOJO_LISTEN'}
Truth table100%2/2
A
A= $ENV{'MOJO_LISTEN'}
Aresult
11
00
20 23 has max_requests => 100;
21  
22 57 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.1
23 57 my $self = shift;
24 57 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 57 57
25 57 ▶ $self->_remove($_) for keys %{$self->{connections} // {}};
Condition100%2/2
$self->{"connections"} // {}
l!l
1938
MC/DC100%1/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table100%2/2
A
A= $self->{"connections"}
Aresult
11
00
26 57 $self->ioloop->remove($_) for @{$self->acceptors};
27 }
28  
29 37 ▶sub ports { [map { $_[0]->ioloop->acceptor($_)->port } @{$_[0]->acceptors}] }
Subroutine ports: called CC=1 SCAR=0.0
Pod documented
30  
31 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
32 0 my $self = shift;
33  
34 # Make sure the event loop can be stopped in regular intervals
35 0 my $loop = $self->ioloop;
36 0 ▶ my $int = $loop->recurring(1 => sub { });
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 ▶ local $SIG{INT} = local $SIG{TERM} = sub { $loop->stop };
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 $self->start->ioloop->start;
39 0 $loop->remove($int);
40 }
41  
42 44 ▶sub start {
Subroutine start: called CC=6 SCAR=17.9
Pod documented
43 44 my $self = shift;
44  
45 44 my $loop = $self->ioloop;
46 44 ▶ if (my $max = $self->max_clients) { $loop->max_connections($max) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $max = $self->max_clients) 4 40 44
47  
48 # Resume accepting connections
49 44 ▶ if (my $servers = $self->{servers}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $servers = $self->{'servers'}) { } 1 43 44
elsif (not @{$self->acceptors;}) { } 42 1 43
50 1 push @{$self->acceptors}, $loop->acceptor(delete $servers->{$_}) for keys %$servers;
51 }
52  
53 # Start listening
54 43 elsif (!@{$self->acceptors}) {
55 42 $self->app->server($self);
56 42 $self->_listen($_) for @{$self->listen};
57 }
58  
59 42 return $self;
60 }
61  
62 1 ▶sub stop {
Subroutine stop: called CC=2 SCAR=6.9
Pod documented
63 1 my $self = shift;
64  
65 # Suspend accepting connections but keep listen sockets open
66 1 my $loop = $self->ioloop;
67 1 ▶ while (my $id = shift @{$self->acceptors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = shift @{$self->acceptors;}) 1 1 2
68 1 my $server = $self->{servers}{$id} = $loop->acceptor($id);
69 1 $loop->remove($id);
70 1 $server->stop;
71 }
72  
73 1 return $self;
74 }
75  
76 163 ▶sub _build_tx {
Subroutine _build_tx: called CC=5 SCAR=16.3
77 163 my ($self, $id, $c) = @_;
78  
79 163 my $tx = $self->build_tx->connection($id);
80 163 $tx->res->headers->server('Mojolicious (Perl)');
81 163 my $handle = $self->ioloop->stream($id)->timeout($self->inactivity_timeout)->handle;
82 163 ▶ unless ($handle->isa('IO::Socket::UNIX')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->isa("IO::Socket::UNIX")) 163 0 163
83 163 ▶ my $addr = $c->{addr} //= [$handle->sockhost, $handle->sockport, $handle->peerhost, $handle->peerport];
Condition100%2/2
$c->{"addr"} //= [$handle->sockhost, $handle->sockport, $handle->peerhost, $handle->peerport]
l!l
13726
MC/DC100%1/1
$c->{"addr"} //= [$handle->sockhost, $handle->sockport, $handle->peerhost, $handle->peerport]
$c->{"addr"}
Truth table100%2/2
A
A= $c->{"addr"}
Aresult
11
00
84 163 $tx->local_address($addr->[0])->local_port($addr->[1]);
85 163 $tx->remote_address($addr->[2])->remote_port($addr->[3]);
86 }
87 163 ▶ $tx->req->url->base->scheme('https') if $c->{tls};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"tls"} 0 163 163
88  
89 163 weaken $self;
90 $tx->on(
91 163 ▶ request => sub {
Subroutine __ANON__: called CC=6 SCAR=18.3
92 163 my $tx = shift;
93  
94 163 my $req = $tx->req;
95 0 ▶ if (my $error = $req->error) { $self->_trace($id, $error->{message}) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $req->error) 0 163 163
96  
97 # WebSocket
98 163 ▶ if ($req->is_handshake) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_handshake) { } 1 162 163
99 1 my $ws = $self->{connections}{$id}{next} = Mojo::Transaction::WebSocket->new(handshake => $tx);
100 1 $self->emit(request => server_handshake $ws);
101 }
102  
103 # HTTP
104 162 else { $self->emit(request => $tx) }
105  
106 # Last keep-alive request or corrupted connection
107 163 my $c = $self->{connections}{$id};
108 163 ▶ $tx->res->headers->connection('close') if ($c->{requests} || 1) >= $self->max_requests || $req->error;
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"requests"} || 1) >= $self->max_requests or $req->error 1 162 163
Condition60%3/5
($c->{"requests"} || 1) >= $self->max_requests or $req->error
l!l&&r!l&&!r
10162
$c->{'requests'} || 1
l!l
1630
MC/DC0%0/2
($c->{"requests"} || 1) >= $self->max_requests or $req->error
$c->{'requests'} $req->error
Truth table33%1/3
A or B
A= $c->{'requests'}B= $req->error
ABresult
1X1
011
000
109  
110 163 ▶ $tx->on(resume => sub { $self && $self->_write($id) });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_write($id) 16 0 16
111 163 $self->_write($id);
112 }
113 163 );
114  
115 # Kept alive if we have more than one request on the connection
116 163 ▶ return ++$c->{requests} > 1 ? $tx->kept_alive(1) : $tx;
Branch: 1 branch
BranchTrueFalseTotal
++$c->{'requests'} > 1 ? : 137 26 163
117 }
118  
119 51 ▶sub _close {
Subroutine _close: called CC=2 SCAR=6.9
120 51 my ($self, $id) = @_;
121 51 ▶ if (my $tx = $self->{connections}{$id}{tx}) { $tx->closed }
Branch: 1 branch
BranchTrueFalseTotal
if (my $tx = $self->{"connections"}{$id}{"tx"}) 2 49 51
122 51 delete $self->{connections}{$id};
123 }
124  
125 0 ▶sub _trace { $_[0]->app->log->trace($_[2]) if $_[0]{connections}{$_[1]}{tx} }
Subroutine _trace: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"connections"}{$_[1]}{"tx"} 0 0 0
126  
127 164 ▶sub _finish {
Subroutine _finish: called CC=9 SCAR=22.1
128 164 my ($self, $id) = @_;
129  
130 # Always remove connection for WebSockets
131 164 my $c = $self->{connections}{$id};
132 164 ▶ return unless my $tx = $c->{tx};
Branch: 1 branch
BranchTrueFalseTotal
unless my $tx = $c->{"tx"} 1 163 164
133 163 ▶ return $self->_remove($id) if $tx->is_websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->is_websocket 1 162 163
134  
135 # Finish transaction
136 162 delete($c->{tx})->closed;
137  
138 # Upgrade connection to WebSocket
139 162 ▶ if (my $ws = delete $c->{next}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ws = delete $c->{"next"}) 1 161 162
140  
141 # Successful upgrade
142 1 ▶ if ($ws->handshake->res->code == 101) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws->handshake->res->code == 101) { } 1 0 1
143 1 $c->{tx} = $ws->established(1);
144 1 weaken $self;
145 1 ▶ $ws->on(resume => sub { $self && $self->_write($id) });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_write($id) 5 0 5
146 1 $self->_write($id);
147 1 return $ws->server_read($tx->req->content->leftovers);
148 }
149  
150 # Failed upgrade
151 0 else { $ws->closed }
152 }
153  
154 # Close connection if necessary
155 161 ▶ return $self->_remove($id) if $tx->error || !$tx->keep_alive;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error or not $tx->keep_alive 4 157 161
Condition66%2/3
$tx->error or not $tx->keep_alive
l!l&&r!l&&!r
04157
MC/DC50%1/2
$tx->error or not $tx->keep_alive
$tx->error not $tx->keep_alive
Truth table66%2/3
A or not(B)
A= $tx->errorB= not $tx->keep_alive
ABresult
1X1
011
000
156  
157 # Build new transaction for leftovers
158 157 ▶ if (length(my $leftovers = $tx->req->content->leftovers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $leftovers = $tx->req->content->leftovers)) 1 156 157
159 1 $tx = $c->{tx} = $self->_build_tx($id, $c);
160 1 $tx->server_read($leftovers);
161 }
162  
163 # Keep-alive connection
164 157 ▶ $self->ioloop->stream($id)->timeout($self->keep_alive_timeout) unless $c->{tx};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"tx"} 156 1 157
165 }
166  
167 42 ▶sub _listen {
Subroutine _listen: called CC=20 SCAR=39.1
168 42 my ($self, $listen) = @_;
169  
170 42 my $url = Mojo::URL->new($listen);
171 42 my $proto = $url->protocol;
172 42 ▶ croak qq{Invalid listen location "$listen"} unless $proto eq 'http' || $proto eq 'https' || $proto eq 'http+unix';
Branch: 1 branch
BranchTrueFalseTotal
unless $proto eq "http" or $proto eq "https" or $proto eq "http+unix" 1 41 42
Condition83%5/6
$proto eq "http" or $proto eq "https" or $proto eq "http+unix"
l!l&&r!l&&!r
4101
$proto eq "http" or $proto eq "https"
l!l&&r!l&&!r
4011
MC/DC66%2/3
$proto eq "http" or $proto eq "https" or $proto eq "http+unix"
$proto eq "http" $proto eq "https" $proto eq "http+unix"
Truth table75%3/4
A or B or C
A= $proto eq "http"B= $proto eq "https"C= $proto eq "http+unix"
ABCresult
1XX1
01X1
0011
0000
173  
174 41 my $query = $url->query;
175 41 my $options = {backlog => $self->backlog};
176 41 $options->{$_} = $query->param($_) for qw(fd single_accept reuse);
177 0 ▶ if ($proto eq 'http+unix') { $options->{path} = $url->host }
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'http+unix') { } 0 41 41
178 else {
179 41 ▶ if ((my $host = $url->host) ne '*') { $options->{address} = $host }
Branch: 1 branch
BranchTrueFalseTotal
if ((my $host = $url->host) ne "*") 41 0 41
180 0 ▶ if (my $port = $url->port) { $options->{port} = $port }
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = $url->port) 0 41 41
181 }
182  
183 41 $options->{tls_ca} = $query->param('ca');
184 41 ▶ /^(.*)_(cert|key)$/ and $options->{"tls_$2"}{$1} = $query->param($_) for @{$query->names};
Branch: 1 branch
BranchTrueFalseTotal
/^(.*)_(cert|key)$/u and $$options{"tls_$2"}{$1} = $query->param($_) 0 2 2
185 0 ▶ if (my $cert = $query->param('cert')) { $options->{tls_cert}{''} = $cert }
Branch: 1 branch
BranchTrueFalseTotal
if (my $cert = $query->param("cert")) 0 41 41
186 0 ▶ if (my $key = $query->param('key')) { $options->{tls_key}{''} = $key }
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $query->param("key")) 0 41 41
187 41 my ($ciphers, $verify, $version) = ($query->param('ciphers'), $query->param('verify'), $query->param('version'));
188 41 ▶ $options->{tls_options}{SSL_cipher_list} = $ciphers if defined $ciphers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ciphers 0 41 41
189 41 ▶ $options->{tls_options}{SSL_verify_mode} = hex $verify if defined $verify;
Branch: 1 branch
BranchTrueFalseTotal
if defined $verify 0 41 41
190 41 ▶ $options->{tls_options}{SSL_version} = $version if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 41 41
191 41 my $tls = $options->{tls} = $proto eq 'https';
192  
193 41 weaken $self;
194 41 push @{$self->acceptors}, $self->ioloop->server(
195 28 ▶ $options => sub {
Subroutine __ANON__: called CC=1 SCAR=0.8
196 28 my ($loop, $stream, $id) = @_;
197  
198 28 $self->{connections}{$id} = {tls => $tls};
199 28 warn "-- Accept $id (@{[_peer($stream->handle)]})\n" if DEBUG;
200 28 $stream->timeout($self->inactivity_timeout);
201  
202 28 ▶ $stream->on(close => sub { $self && $self->_close($id) });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_close($id) 25 1 26
203 0 ▶ $stream->on(error => sub { $self && $self->app->log->error(pop) && $self->_close($id) });
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->app->log->error(pop()) and $self->_close($id) 0 0 0
Condition0%0/3
$self and $self->app->log->error(pop())
!ll&&!rl&&r
000
MC/DC0%0/2
$self and $self->app->log->error(pop())
$self $self->app->log->error(pop())
Truth table0%0/3
A and B
A= $selfB= $self->app->log->error(pop())
ABresult
0X0
100
111
204 28 ▶ $stream->on(read => sub { $self && $self->_read($id => pop) });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_read($id, pop()) 182 0 182
205 0 ▶ $stream->on(timeout => sub { $self && $self->_trace($id, 'Inactivity timeout (see FAQ for more)') });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_trace($id, "Inactivity timeout (see FAQ for more)") 0 0 0
206 }
207 41 );
208  
209 40 ▶ return if $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->silent 40 0 40
210 0 $self->app->log->info(qq{Listening at "$url"});
211 0 $query->pairs([]);
212 0 ▶ $url->host('127.0.0.1') if $url->host eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $url->host eq "*" 0 0 0
213 0 ▶ $url->port($self->ports->[-1]) if !$options->{path} && !$url->port;
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"path"} or $url->port 0 0 0
Condition0%0/3
$options->{"path"} or $url->port
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"path"} or $url->port
$options->{"path"} $url->port
Truth table0%0/3
A or B
A= $options->{"path"}B= $url->port
ABresult
1X1
011
000
214 0 ▶ say 'Web application available at ', $options->{path} // $url;
Condition0%0/3
$options->{"path"} // $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"path"} // $url
$options->{"path"} $url
Truth table0%0/3
A // B
A= $options->{"path"}B= $url
ABresult
1X1
011
000
215 }
216  
217 0 ▶sub _peer { $_[0]->isa('IO::Socket::UNIX') ? $_[0]->peerpath : $_[0]->peerhost }
Subroutine _peer: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa('IO::Socket::UNIX') ? : 0 0 0
218  
219 182 ▶sub _read {
Subroutine _read: called CC=2 SCAR=7.0
220 182 my ($self, $id, $chunk) = @_;
221  
222 # Make sure we have a transaction
223 182 my $c = $self->{connections}{$id};
224 182 ▶ my $tx = $c->{tx} ||= $self->_build_tx($id, $c);
Condition66%2/3
$c->{"tx"} ||= $self->_build_tx($id, $c)
l!l&&r!l&&!r
201620
MC/DC0%0/2
$c->{"tx"} ||= $self->_build_tx($id, $c)
$c->{"tx"} $self->_build_tx($id, $c)
Truth table66%2/3
A ||= B
A= $c->{"tx"}B= $self->_build_tx($id, $c)
ABresult
1X1
011
000
225 182 warn term_escape "-- Server <<< Client (@{[_url($tx)]})\n$chunk\n" if DEBUG;
226 182 $tx->server_read($chunk);
227 }
228  
229 26 ▶sub _remove {
Subroutine _remove: called CC=1 SCAR=0.0
230 26 my ($self, $id) = @_;
231 26 $self->ioloop->remove($id);
232 26 $self->_close($id);
233 }
234  
235 0 ▶sub _url { shift->req->url->to_abs }
Subroutine _url: not called CC=1 SCAR=6.9
236  
237 204 ▶sub _write {
Subroutine _write: called CC=6 SCAR=17.9
238 204 my ($self, $id) = @_;
239  
240 # Protect from resume event recursion
241 204 my $c = $self->{connections}{$id};
242 204 ▶ return if !(my $tx = $c->{tx}) || $c->{writing};
Branch: 1 branch
BranchTrueFalseTotal
if not my $tx = $c->{"tx"} or $c->{"writing"} 15 189 204
Condition66%2/3
not my $tx = $c->{"tx"} or $c->{"writing"}
l!l&&r!l&&!r
015189
MC/DC50%1/2
not my $tx = $c->{"tx"} or $c->{"writing"}
not my $tx = $c->{"tx"} $c->{"writing"}
Truth table66%2/3
not(A) or B
A= not my $tx = $c->{"tx"}B= $c->{"writing"}
ABresult
1X1
011
000
243 189 local $c->{writing} = 1;
244 189 my $chunk = $tx->server_write;
245 189 warn term_escape "-- Server >>> Client (@{[_url($tx)]})\n$chunk\n" if DEBUG;
246 189 ▶ my $next = $tx->is_finished ? '_finish' : length $chunk ? '_write' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$tx->is_finished ? : 164 25 189
length $chunk ? : 20 5 25
247 189 ▶ return $self->ioloop->stream($id)->write($chunk) unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 5 184 189
248 184 weaken $self;
249 184 ▶ $self->ioloop->stream($id)->write($chunk => sub { $self && $self->$next($id) });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->$next($id) 183 1 184
250 }
251  
252 23 1;
253  
254 =encoding utf8
255  
256 =head1 NAME
257  
258 Mojo::Server::Daemon - Non-blocking I/O HTTP and WebSocket server
259  
260 =head1 SYNOPSIS
261  
262 use Mojo::Server::Daemon;
263  
264 my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
265 $daemon->unsubscribe('request')->on(request => sub ($daemon, $tx) {
266  
267 # Request
268 my $method = $tx->req->method;
269 my $path = $tx->req->url->path;
270  
271 # Response
272 $tx->res->code(200);
273 $tx->res->headers->content_type('text/plain');
274 $tx->res->body("$method request for $path!");
275  
276 # Resume transaction
277 $tx->resume;
278 });
279 $daemon->run;
280  
281 =head1 DESCRIPTION
282  
283 L<Mojo::Server::Daemon> is a full featured, highly portable non-blocking I/O HTTP and WebSocket server, with IPv6, TLS,
284 SNI, Comet (long polling), keep-alive and multiple event loop support.
285  
286 For better scalability (epoll, kqueue) and to provide non-blocking name resolution, SOCKS5 as well as TLS support, the
287 optional modules L<EV> (4.32+), L<Net::DNS::Native> (0.15+), L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL>
288 (2.009+) will be used automatically if possible. Individual features can also be disabled with the C<MOJO_NO_NNR>,
289 C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment variables.
290  
291 See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.
292  
293 =head1 SIGNALS
294  
295 The L<Mojo::Server::Daemon> process can be controlled at runtime with the following signals.
296  
297 =head2 INT, TERM
298  
299 Shut down server immediately.
300  
301 =head1 EVENTS
302  
303 L<Mojo::Server::Daemon> inherits all events from L<Mojo::Server>.
304  
305 =head1 ATTRIBUTES
306  
307 L<Mojo::Server::Daemon> inherits all attributes from L<Mojo::Server> and implements the following new ones.
308  
309 =head2 acceptors
310  
311 my $acceptors = $daemon->acceptors;
312 $daemon = $daemon->acceptors(['6be0c140ef00a389c5d039536b56d139']);
313  
314 Active acceptor ids.
315  
316 # Check port
317 mu $port = $daemon->ioloop->acceptor($daemon->acceptors->[0])->port;
318  
319 =head2 backlog
320  
321 my $backlog = $daemon->backlog;
322 $daemon = $daemon->backlog(128);
323  
324 Listen backlog size, defaults to C<SOMAXCONN>.
325  
326 =head2 inactivity_timeout
327  
328 my $timeout = $daemon->inactivity_timeout;
329 $daemon = $daemon->inactivity_timeout(5);
330  
331 Maximum amount of time in seconds a connection with an active request can be inactive before getting closed, defaults
332 to the value of the C<MOJO_INACTIVITY_TIMEOUT> environment variable or C<30>. Setting the value to C<0> will allow
333 connections to be inactive indefinitely.
334  
335 =head2 ioloop
336  
337 my $loop = $daemon->ioloop;
338 $daemon = $daemon->ioloop(Mojo::IOLoop->new);
339  
340 Event loop object to use for I/O operations, defaults to the global L<Mojo::IOLoop> singleton.
341  
342 =head2 keep_alive_timeout
343  
344 my $timeout = $daemon->keep_alive_timeout;
345 $daemon = $daemon->keep_alive_timeout(10);
346  
347 Maximum amount of time in seconds a connection without an active request can be inactive before getting closed,
348 defaults to the value of the C<MOJO_KEEP_ALIVE_TIMEOUT> environment variable or C<5>. Setting the value to C<0> will
349 allow connections to be inactive indefinitely.
350  
351 =head2 listen
352  
353 my $listen = $daemon->listen;
354 $daemon = $daemon->listen(['https://127.0.0.1:8080']);
355  
356 Array reference with one or more locations to listen on, defaults to the value of the C<MOJO_LISTEN> environment
357 variable or C<http://*:3000> (shortcut for C<http://0.0.0.0:3000>).
358  
359 # Listen on all IPv4 interfaces
360 $daemon->listen(['http://*:3000']);
361  
362 # Listen on all IPv4 and IPv6 interfaces
363 $daemon->listen(['http://[::]:3000']);
364  
365 # Listen on IPv6 interface
366 $daemon->listen(['http://[::1]:4000']);
367  
368 # Listen on IPv4 and IPv6 interfaces
369 $daemon->listen(['http://127.0.0.1:3000', 'http://[::1]:3000']);
370  
371 # Listen on UNIX domain socket "/tmp/myapp.sock" (percent encoded slash)
372 $daemon->listen(['http+unix://%2Ftmp%2Fmyapp.sock']);
373  
374 # File descriptor, as used by systemd
375 $daemon->listen(['http://127.0.0.1?fd=3']);
376  
377 # Allow multiple servers to use the same port (SO_REUSEPORT)
378 $daemon->listen(['http://*:8080?reuse=1']);
379  
380 # Listen on two ports with HTTP and HTTPS at the same time
381 $daemon->listen(['http://*:3000', 'https://*:4000']);
382  
383 # Use a custom certificate and key
384 $daemon->listen(['https://*:3000?cert=/x/server.crt&key=/y/server.key']);
385  
386 # Domain specific certificates and keys (SNI)
387 $daemon->listen(
388 ['https://*:3000?example.com_cert=/x/my.crt&example.com_key=/y/my.key']);
389  
390 # Or even a custom certificate authority
391 $daemon->listen(
392 ['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']);
393  
394 These parameters are currently available:
395  
396 =over 2
397  
398 =item ca
399  
400 ca=/etc/tls/ca.crt
401  
402 Path to TLS certificate authority file used to verify the peer certificate.
403  
404 =item cert
405  
406 cert=/etc/tls/server.crt
407 mojolicious.org_cert=/etc/tls/mojo.crt
408  
409 Path to the TLS cert file, defaults to a built-in test certificate.
410  
411 =item ciphers
412  
413 ciphers=AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH
414  
415 TLS cipher specification string. For more information about the format see
416 L<https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-STRINGS>.
417  
418 =item fd
419  
420 fd=3
421  
422 File descriptor with an already prepared listen socket.
423  
424 =item key
425  
426 key=/etc/tls/server.key
427 mojolicious.org_key=/etc/tls/mojo.key
428  
429 Path to the TLS key file, defaults to a built-in test key.
430  
431 =item reuse
432  
433 reuse=1
434  
435 Allow multiple servers to use the same port with the C<SO_REUSEPORT> socket option.
436  
437 =item single_accept
438  
439 single_accept=1
440  
441 Only accept one connection at a time.
442  
443 =item verify
444  
445 verify=0x00
446  
447 TLS verification mode.
448  
449 =item version
450  
451 version=TLSv1_2
452  
453 TLS protocol version.
454  
455 =back
456  
457 =head2 max_clients
458  
459 my $max = $daemon->max_clients;
460 $daemon = $daemon->max_clients(100);
461  
462 Maximum number of accepted connections this server is allowed to handle concurrently, before stopping to accept new
463 incoming connections, passed along to L<Mojo::IOLoop/"max_connections">.
464  
465 =head2 max_requests
466  
467 my $max = $daemon->max_requests;
468 $daemon = $daemon->max_requests(250);
469  
470 Maximum number of keep-alive requests per connection, defaults to C<100>.
471  
472 =head2 silent
473  
474 my $bool = $daemon->silent;
475 $daemon = $daemon->silent($bool);
476  
477 Disable console messages.
478  
479 =head1 METHODS
480  
481 L<Mojo::Server::Daemon> inherits all methods from L<Mojo::Server> and implements the following new ones.
482  
483 =head2 ports
484  
485 my $ports = $daemon->ports;
486  
487 Get all ports this server is currently listening on.
488  
489 # All ports
490 say for @{$daemon->ports};
491  
492 =head2 run
493  
494 $daemon->run;
495  
496 Run server and wait for L</"SIGNALS">.
497  
498 =head2 start
499  
500 $daemon = $daemon->start;
501  
502 Start or resume accepting connections through L</"ioloop">.
503  
504 # Listen on random port
505 my $port = $daemon->listen(['http://127.0.0.1'])->start->ports->[0];
506  
507 # Run multiple web servers concurrently
508 my $daemon1 = Mojo::Server::Daemon->new(listen => ['http://*:3000'])->start;
509 my $daemon2 = Mojo::Server::Daemon->new(listen => ['http://*:4000'])->start;
510 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
511  
512 =head2 stop
513  
514 $daemon = $daemon->stop;
515  
516 Stop accepting connections through L</"ioloop">.
517  
518 =head1 DEBUGGING
519  
520 You can set the C<MOJO_SERVER_DEBUG> environment variable to get some advanced diagnostics information printed to
521 C<STDERR>.
522  
523 MOJO_SERVER_DEBUG=1
524  
525 =head1 SEE ALSO
526  
527 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
528  
529 =cut
« Mojolicious/Commands.pm ↑ Summary Mojo/Cookie/Response.pm »