use NEXT;
use Socket;
use IO::Socket::INET ();
-use IO::Select ();
+use IO::Select ();
# For PAR
require Catalyst::Engine::HTTP::Restarter;
=head1 METHODS
-=over 4
-
-=item $self->finalize_headers($c)
+=head2 $self->finalize_headers($c)
=cut
$self->NEXT::finalize_headers($c);
}
-=item $self->finalize_read($c)
+=head2 $self->finalize_read($c)
=cut
return $self->NEXT::finalize_read($c);
}
-=item $self->prepare_read($c)
+=head2 $self->prepare_read($c)
=cut
return $self->NEXT::prepare_read($c);
}
-=item $self->read_chunk($c, $buffer, $length)
+=head2 $self->read_chunk($c, $buffer, $length)
=cut
}
}
-=item run
+=head2 run
=cut
$options ||= {};
+ if ($options->{background}) {
+ my $child = fork;
+ die "Can't fork: $!" unless defined($child);
+ exit if $child;
+ }
+
my $restart = 0;
local $SIG{CHLD} = 'IGNORE';
print "You can connect to your server at $url\n";
+ if ($options->{background}) {
+ open STDIN, "+</dev/null" or die $!;
+ open STDOUT, ">&STDIN" or die $!;
+ open STDERR, ">&STDIN" or die $!;
+ if ( $^O !~ /MSWin32/ ) {
+ require POSIX;
+ POSIX::setsid()
+ or die "Can't start a new session: $!";
+ }
+ }
+
+ if (my $pidfile = $options->{pidfile}) {
+ if (! open PIDFILE, "> $pidfile") {
+ warn("Cannot open: $pidfile: $!");
+ }
+ print PIDFILE "$$\n";
+ close PIDFILE;
+ }
+
$self->_keep_alive( $options->{keepalive} || 0 );
- my $parent = $$;
my $pid = undef;
while ( accept( Remote, $daemon ) )
{ # TODO: get while ( my $remote = $daemon->accept ) to work
my $sockdata = $self->_socket_data( \*Remote );
my $ipaddr = _inet_addr( $sockdata->{peeraddr} );
my $ready = 0;
- while ( my ( $ip, $mask ) = each %$allowed and not $ready ) {
+ foreach my $ip ( keys %$allowed ) {
+ my $mask = $allowed->{$ip};
$ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
+ last if $ready;
}
if ($ready) {
$restart = 1;
if ($restart) {
$SIG{CHLD} = 'DEFAULT';
wait;
+
+ ### if the standalone server was invoked with perl -I .. we will loose
+ ### those include dirs upon re-exec. So add them to PERL5LIB, so they
+ ### are available again for the exec'ed process --kane
+ use Config;
+ $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
+
exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
}
sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
-=back
-
=head1 SEE ALSO
L<Catalyst>, L<Catalyst::Engine>.