| 1 | package Mojo::Server; | ||||||||||||||||||||||||||||
| 2 | 23 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
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::File qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use Mojo::Loader qw(load_class); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use Mojo::Util qw(md5_sum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | has app => sub { shift->build_app('Mojo::HelloWorld') }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 25 | ▶ | has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} || !!@{shift->trusted_proxies} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 26 | ▶ | has trusted_proxies => sub { [split /\s*,\s*/, ($ENV{MOJO_TRUSTED_PROXIES} // '')] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOJO_TRUSTED_PROXIES"} // ""
MC/DC100%1/1
$ENV{"MOJO_TRUSTED_PROXIES"} // ""
$ENV{"MOJO_TRUSTED_PROXIES"}
Truth table100%2/2
A
A= $ENV{"MOJO_TRUSTED_PROXIES"}
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 23 | our @ARGS_OVERRIDE; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 48 | ▶ | sub build_app { | ||||||||||||||||||||||||||
|
Subroutine
build_app: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 48 | my ($self, $app) = (shift, shift); | |||||||||||||||||||||||||||
| 19 | 48 | local $ENV{MOJO_EXE}; | |||||||||||||||||||||||||||
| 20 | 48 | ▶ | return $self->app($app->new(@_))->app unless my $e = load_class $app; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | die ref $e ? $e : qq{Can't find application class "$app" in \@INC. (@INC)\n}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 163 | ▶ | sub build_tx { | ||||||||||||||||||||||||||
|
Subroutine
build_tx: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 163 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | 163 | my $tx = $self->app->build_tx; | |||||||||||||||||||||||||||
| 27 | 163 | push @{$tx->req->trusted_proxies}, @{$self->trusted_proxies}; | |||||||||||||||||||||||||||
| 28 | 163 | ▶ | $tx->req->reverse_proxy(1) if $self->reverse_proxy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 163 | return $tx; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub daemonize { | ||||||||||||||||||||||||||
|
Subroutine
daemonize: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Fork and kill parent | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | die "Can't fork: $!" unless defined(my $pid = fork); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | exit 0 if $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | POSIX::setsid == -1 and die "Can't start a new session: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Close filehandles | ||||||||||||||||||||||||||||
| 40 | 0 | open STDIN, '<', '/dev/null'; | |||||||||||||||||||||||||||
| 41 | 0 | open STDOUT, '>', '/dev/null'; | |||||||||||||||||||||||||||
| 42 | 0 | open STDERR, '>&', STDOUT; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | sub load_app { | ||||||||||||||||||||||||||
|
Subroutine
load_app: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 6 | ▶ | my ($self, $path, @args) = (shift, shift, ref $_[0] ? %{shift()} : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Clean environment (reset FindBin defensively) | ||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | 6 | local $0 = $path = path($path)->to_abs->to_string; | |||||||||||||||||||||||||||
| 51 | 6 | require FindBin; | |||||||||||||||||||||||||||
| 52 | 6 | FindBin->again; | |||||||||||||||||||||||||||
| 53 | 6 | local @ENV{qw(MOJO_APP_LOADER MOJO_EXE)} = (1, undef); | |||||||||||||||||||||||||||
| 54 | 6 | local @ARGS_OVERRIDE = @args; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Try to load application from script into sandbox | ||||||||||||||||||||||||||||
| 57 | 6 | my $app = eval sprintf <<'END_CODE', md5_sum($path); | |||||||||||||||||||||||||||
| 58 | package Mojo::Server::Sandbox::%s; | ||||||||||||||||||||||||||||
| 59 | do $path or die $@ || $! || "$path did not return a true value"; | ||||||||||||||||||||||||||||
| 60 | END_CODE | ||||||||||||||||||||||||||||
| 61 | 6 | ▶ | die qq{Can't load application from file "$path": $@} if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 5 | ▶ | die qq{File "$path" did not return an application object.\n} unless blessed $app && $app->can('handler'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($app) and $app->can("handler")
MC/DC50%1/2
builtin::blessed($app) and $app->can("handler")
builtin::blessed($app)
$app->can("handler")
Truth table66%2/3
A and B
A= builtin::blessed($app)B= $app->can("handler")
| |||||||||||||||||||||||||||||
| 63 | 4 | $self->app($app); | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 4 | FindBin->again; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | return $self->app; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 117 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 117 | my $self = shift->SUPER::new(@_); | |||||||||||||||||||||||||||
| 72 | 117 | ▶ | $self->on(request => sub { shift->app->handler(shift) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 117 | return $self; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | sub run { croak 'Method "run" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 23 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =encoding utf8 | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 NAME | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Mojo::Server - HTTP/WebSocket server base class | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | package Mojo::Server::MyServer; | ||||||||||||||||||||||||||||
| 89 | use Mojo::Base 'Mojo::Server', -signatures; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub run ($self) { | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Get a transaction | ||||||||||||||||||||||||||||
| 94 | my $tx = $self->build_tx; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Emit "request" event | ||||||||||||||||||||||||||||
| 97 | $self->emit(request => $tx); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | L<Mojo::Server> is an abstract base class for HTTP/WebSocket servers and server interfaces, like L<Mojo::Server::CGI>, | ||||||||||||||||||||||||||||
| 103 | L<Mojo::Server::Daemon>, L<Mojo::Server::Hypnotoad>, L<Mojo::Server::Morbo>, L<Mojo::Server::Prefork> and | ||||||||||||||||||||||||||||
| 104 | L<Mojo::Server::PSGI>. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | L<Mojo::Server> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 request | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $server->on(request => sub ($server, $tx) {...}); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Emitted when a request is ready and needs to be handled. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | $server->on(request => sub ($server, $tx) { | ||||||||||||||||||||||||||||
| 117 | $tx->res->code(200); | ||||||||||||||||||||||||||||
| 118 | $tx->res->headers->content_type('text/plain'); | ||||||||||||||||||||||||||||
| 119 | $tx->res->body('Hello World!'); | ||||||||||||||||||||||||||||
| 120 | $tx->resume; | ||||||||||||||||||||||||||||
| 121 | }); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<Mojo::Server> implements the following attributes. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 app | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | my $app = $server->app; | ||||||||||||||||||||||||||||
| 130 | $server = $server->app(MojoSubclass->new); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Application this server handles, defaults to a L<Mojo::HelloWorld> object. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 reverse_proxy | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | my $bool = $server->reverse_proxy; | ||||||||||||||||||||||||||||
| 137 | $server = $server->reverse_proxy($bool); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | This server operates behind a reverse proxy, defaults to the value of the C<MOJO_REVERSE_PROXY> environment variable | ||||||||||||||||||||||||||||
| 140 | or true if L</trusted_proxies> is not empty. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 trusted_proxies | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | my $proxies = $server->trusted_proxies; | ||||||||||||||||||||||||||||
| 145 | $server = $server->trusted_proxies(['10.0.0.0/8', '127.0.0.1', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7']); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | This server expects requests from trusted reverse proxies, defaults to the value of the C<MOJO_TRUSTED_PROXIES> | ||||||||||||||||||||||||||||
| 148 | environment variable split on commas with optional whitespace. These proxies should be addresses or networks in CIDR | ||||||||||||||||||||||||||||
| 149 | form. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 METHODS | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | L<Mojo::Server> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 build_app | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | my $app = $server->build_app('MyApp'); | ||||||||||||||||||||||||||||
| 158 | my $app = $server->build_app('MyApp', log => Mojo::Log->new); | ||||||||||||||||||||||||||||
| 159 | my $app = $server->build_app('MyApp', {log => Mojo::Log->new}); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Build application from class and assign it to L</"app">. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 build_tx | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | my $tx = $server->build_tx; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Let application build a transaction. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 daemonize | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | $server->daemonize; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Daemonize server process. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 load_app | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | my $app = $server->load_app('/home/sri/myapp.pl'); | ||||||||||||||||||||||||||||
| 178 | my $app = $server->load_app('/home/sri/myapp.pl', log => Mojo::Log->new); | ||||||||||||||||||||||||||||
| 179 | my $app = $server->load_app('/home/sri/myapp.pl', {log => Mojo::Log->new}); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Load application from script and assign it to L</"app">. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | say Mojo::Server->new->load_app('./myapp.pl')->home; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 new | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | my $server = Mojo::Server->new; | ||||||||||||||||||||||||||||
| 188 | my $server = Mojo::Server->new(reverse_proxy => 1); | ||||||||||||||||||||||||||||
| 189 | my $server = Mojo::Server->new({reverse_proxy => 1}); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Construct a new L<Mojo::Server> object and subscribe to L</"request"> event with default request handling. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 run | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | $server->run; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Run server. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||