/usr/share/perl5/Mojo/Server.pm is in libmojolicious-perl 5.54+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | package Mojo::Server;
use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';
use Cwd 'abs_path';
use Mojo::Loader;
use Mojo::Util 'md5_sum';
use POSIX;
use Scalar::Util 'blessed';
has app => sub { shift->build_app('Mojo::HelloWorld') };
has [qw(group user)];
has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };
sub build_app {
my ($self, $app) = @_;
local $ENV{MOJO_EXE};
return $app->new unless my $e = Mojo::Loader->new->load($app);
die ref $e ? $e : qq{Can't find application class "$app" in \@INC. (@INC)\n};
}
sub build_tx {
my $self = shift;
my $tx = $self->app->build_tx;
$tx->req->reverse_proxy(1) if $self->reverse_proxy;
return $tx;
}
sub daemonize {
# Fork and kill parent
die "Can't fork: $!" unless defined(my $pid = fork);
exit 0 if $pid;
POSIX::setsid or die "Can't start a new session: $!";
# Close filehandles
open STDIN, '</dev/null';
open STDOUT, '>/dev/null';
open STDERR, '>&STDOUT';
}
sub load_app {
my ($self, $path) = @_;
# Clean environment (reset FindBin defensively)
{
local $0 = $path = abs_path $path;
require FindBin;
FindBin->again;
local $ENV{MOJO_APP_LOADER} = 1;
local $ENV{MOJO_EXE};
# Try to load application from script into sandbox
my $app = eval "package Mojo::Server::Sandbox::@{[md5_sum $path]};"
. 'return do($path) || die($@ || $!);';
die qq{Can't load application from file "$path": $@} if !$app && $@;
die qq{File "$path" did not return an application object.\n}
unless blessed $app && $app->isa('Mojo');
$self->app($app);
};
FindBin->again;
return $self->app;
}
sub new {
my $self = shift->SUPER::new(@_);
$self->on(request => sub { shift->app->handler(shift) });
return $self;
}
sub run { croak 'Method "run" not implemented by subclass' }
sub setuidgid {
my $self = shift;
# Group (make sure secondary groups are reassigned too)
if (my $group = $self->group) {
return $self->_log(qq{Group "$group" does not exist.})
unless defined(my $gid = getgrnam $group);
return $self->_log(qq{Can't switch to group "$group": $!})
unless ($( = $) = "$gid $gid") && $) eq "$gid $gid" && $( eq "$gid $gid";
}
# User
return $self unless my $user = $self->user;
return $self->_log(qq{User "$user" does not exist.})
unless defined(my $uid = getpwnam $user);
return $self->_log(qq{Can't switch to user "$user": $!})
unless POSIX::setuid($uid);
return $self;
}
sub _log { $_[0]->app->log->error($_[1]) and return $_[0] }
1;
=encoding utf8
=head1 NAME
Mojo::Server - HTTP server base class
=head1 SYNOPSIS
package Mojo::Server::MyServer;
use Mojo::Base 'Mojo::Server';
sub run {
my $self = shift;
# Get a transaction
my $tx = $self->build_tx;
# Emit "request" event
$self->emit(request => $tx);
}
=head1 DESCRIPTION
L<Mojo::Server> is an abstract HTTP server base class.
=head1 EVENTS
L<Mojo::Server> inherits all events from L<Mojo::EventEmitter> and can emit
the following new ones.
=head2 request
$server->on(request => sub {
my ($server, $tx) = @_;
...
});
Emitted when a request is ready and needs to be handled.
$server->unsubscribe('request');
$server->on(request => sub {
my ($server, $tx) = @_;
$tx->res->code(200);
$tx->res->headers->content_type('text/plain');
$tx->res->body('Hello World!');
$tx->resume;
});
=head1 ATTRIBUTES
L<Mojo::Server> implements the following attributes.
=head2 app
my $app = $server->app;
$server = $server->app(MojoSubclass->new);
Application this server handles, defaults to a L<Mojo::HelloWorld> object.
=head2 group
my $group = $server->group;
$server = $server->group('users');
Group for server process.
=head2 reverse_proxy
my $bool = $server->reverse_proxy;
$server = $server->reverse_proxy($bool);
This server operates behind a reverse proxy, defaults to the value of the
C<MOJO_REVERSE_PROXY> environment variable.
=head2 user
my $user = $server->user;
$server = $server->user('web');
User for the server process.
=head1 METHODS
L<Mojo::Server> inherits all methods from L<Mojo::EventEmitter> and implements
the following new ones.
=head2 build_app
my $app = $server->build_app('Mojo::HelloWorld');
Build application from class.
=head2 build_tx
my $tx = $server->build_tx;
Let application build a transaction.
=head2 daemonize
$server->daemonize;
Daemonize server process.
=head2 load_app
my $app = $server->load_app('/home/sri/myapp.pl');
Load application from script.
say Mojo::Server->new->load_app('./myapp.pl')->home;
=head2 new
my $server = Mojo::Server->new;
Construct a new L<Mojo::Server> object and subscribe to L</"request"> event
with default request handling.
=head2 run
$server->run;
Run server. Meant to be overloaded in a subclass.
=head2 setuidgid
$server = $server->setuidgid;
Set L</"user"> and L</"group"> for process.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|