File: Core.pm

package info (click to toggle)
libfcgi-engine-perl 0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 360 kB
  • sloc: perl: 3,226; makefile: 2
file content (250 lines) | stat: -rw-r--r-- 5,869 bytes parent folder | download | duplicates (3)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package FCGI::Engine::Core;
use Moose;

use Class::Load ();
use FCGI;
use MooseX::Daemonize::Pid::File;
use FCGI::Engine::Types;
use FCGI::Engine::ProcManager;

use constant DEBUG => 0;

our $VERSION   = '0.22';
our $AUTHORITY = 'cpan:STEVAN';

with 'MooseX::Getopt',
     'MooseX::Daemonize::Core';

has 'listen' => (
    metaclass   => 'Getopt',
    is          => 'ro',
    isa         => 'FCGI::Engine::Listener',
    coerce      => 1,
    cmd_aliases => 'l',
    predicate   => 'is_listening',
);

has 'nproc' => (
    metaclass   => 'Getopt',
    is          => 'ro',
    isa         => 'Int',
    default     => sub { 1 },
    cmd_aliases => 'n',
);

has 'pidfile' => (
    metaclass   => 'Getopt',
    is          => 'ro',
    isa         => 'MooseX::Daemonize::Pid::File',
    coerce      => 1,
    cmd_aliases => 'p',
    predicate   => 'has_pidfile',
);

has 'detach' => (
    metaclass   => 'Getopt',
    is          => 'ro',
    isa         => 'Bool',
    cmd_flag    => 'daemon',
    cmd_aliases => 'd',
    predicate   => 'should_detach',
);

has 'manager' => (
    metaclass   => 'Getopt',
    is          => 'ro',
    isa         => 'Str',
    default     => sub { 'FCGI::Engine::ProcManager' },
    cmd_aliases => 'M',
);

has 'use_manager' => (
    metaclass => 'Getopt',
    is        => 'ro',
    isa       => 'Bool',
    default   => 0,
);

# options to specify in your script

has 'pre_fork_init' => (
    metaclass => 'NoGetopt',
    is        => 'ro',
    isa       => 'CodeRef',
    predicate => 'has_pre_fork_init',
);

## methods ...

sub BUILD {
    my $self = shift;
    ($self->has_pidfile)
        || confess "You must specify a pidfile if you are listening"
            if $self->is_listening;
}

sub initialize {
    my ( $self, %addtional_options ) = @_;

    $self->pre_fork_init->(%addtional_options)
        if $self->has_pre_fork_init;

    inner();
}

sub create_socket {
    my $self   = shift;
    my $socket = 0;
    if ($self->is_listening) {
        my $old_umask = umask;
        umask(0);
        $socket = FCGI::OpenSocket($self->listen, 100);
        umask($old_umask);
    }
    $socket;
}

sub create_environment { +{} }

sub create_request {
    my ( $self, $socket, $env ) = @_;
    return FCGI::Request(
        \*STDIN,
        \*STDOUT,
        \*STDERR,
        $env,
        $socket,
        &FCGI::FAIL_ACCEPT_ON_INTR
    );
}

sub create_proc_manager {
    my ( $self, %addtional_options ) = @_;

    # make sure any subclasses are loaded ...
    Class::Load::load_class( $self->manager );

    return $self->manager->new({
        n_processes => $self->nproc,
        pidfile     => $self->pidfile,
        %addtional_options
    });
}

sub prepare_environment {
    my ($self, $_env) = @_;

    my $env = inner();

    # Cargo-culted from Catalyst::Engine::FastCGI
    # and Plack::Server::FCGI, thanks guys :)
    if ( $env->{SERVER_SOFTWARE} ) {
        if ( $env->{SERVER_SOFTWARE} =~ /lighttpd/ ) {
            $env->{PATH_INFO}   ||= delete $env->{SCRIPT_NAME};
            $env->{SCRIPT_NAME} ||= '';
            $env->{SERVER_NAME} =~ s/:\d+$//; # cut off port number
        }
        elsif ( $env->{SERVER_SOFTWARE} =~ /^nginx/ ) {
            my $script_name = $env->{SCRIPT_NAME};
            $env->{PATH_INFO} =~ s/^$script_name//g;
        }
    }

    $env;
}

sub handle_request { confess __PACKAGE__ . " is abstract, override handle_request" }

sub run {
    my ($self, %addtional_options) = @_;

    $self->initialize( %addtional_options );

    my $socket  = $self->create_socket;
    my $env     = $self->create_environment;
    my $request = $self->create_request( $socket, $env );

    my $proc_manager;

    if ($self->is_listening) {

        $self->daemon_fork && return if $self->detach;

        $proc_manager = $self->create_proc_manager( %addtional_options );

        $self->daemon_detach(
            # Not sure we need this ...
            no_double_fork       => 1,
            # we definetely need this ...
            dont_close_all_files => 1,
        ) if $self->detach;

        $proc_manager->manage;
    }

    # We do not listen but we do want more than one processes being forked and
    # want to take the benefit of running the process manager as well. This
    # makes sense if the FastCGI script is started directly via Apache.
    elsif ( $self->use_manager ) {
        $proc_manager = $self->create_proc_manager( %addtional_options );
        $proc_manager->manage;
    }

    while ($request->Accept() >= 0) {

        $proc_manager && $proc_manager->pre_dispatch;

        $self->handle_request(
            $self->prepare_environment( $env ),
            $request
        );

        $proc_manager && $proc_manager->post_dispatch;
    }
}

1;

__END__

=pod

=head1 NAME

FCGI::Engine::Core - A base class for various FCGI::Engine flavors

=head1 DESCRIPTION

This is a base class for various FCGI::Engine flavors, it should be
possible to subclass this to add different approaches to FCGI::Engine.

The basic L<FCGI::Engine> shows a Catalyst/CGI::Application style
approach with a simple handler class, while the L<FCGI::Engine::PSGI>
shows how this can be used to run things like PSGI applications.

This class is mostly of interest to other FCGI::Engine flavor
developers, who should pretty much just read the source. The relevant
docs are to be found in L<FCGI::Engine> and L<FCGI::Engine::PSGI>.

=head1 BUGS

All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.

=head1 AUTHOR

Stevan Little E<lt>stevan@iinteractive.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2007-2010 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut