my ( $self, $class, $port, $host, $options ) = @_;
$options ||= {};
-
+
our $GOT_HUP;
local $GOT_HUP = 0;
-
+
local $SIG{HUP} = sub { $GOT_HUP = 1; };
local $SIG{CHLD} = 'IGNORE';
+ my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
+
# Handle requests
# Setup socket
$host = $host ? inet_aton($host) : INADDR_ANY;
socket( HTTPDaemon, PF_INET, SOCK_STREAM, getprotobyname('tcp') )
- || die "Couldn't assign TCP socket: $!";
+ || die "Couldn't assign TCP socket: $!";
setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) )
- || die "Couldn't set TCP socket options: $!";
+ || die "Couldn't set TCP socket options: $!";
bind( HTTPDaemon, sockaddr_in( $port, $host ) )
- || die "Couldn't bind socket to $port on $host: $!";
+ || die "Couldn't bind socket to $port on $host: $!";
listen( HTTPDaemon, SOMAXCONN )
- || die "Couldn't listen to socket on $port on $host: $!";
+ || die "Couldn't listen to socket on $port on $host: $!";
my $url = 'http://';
if ( $host eq INADDR_ANY ) {
require Sys::Hostname;
}
}
}
+ unless ( uc($method) eq 'KILL' ) {
- # Pass flow control to Catalyst
- $class->handle_request;
+ # Pass flow control to Catalyst
+ $class->handle_request;
+ }
+ else {
+ my $ipaddr = _inet_addr($peeraddr);
+ my $ready = 0;
+ while ( my ( $ip, $mask ) = each %$allowed and not $ready ) {
+ $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
+ }
+ if ($ready) {
+ $GOT_HUP = 1;
+ last;
+ }
+ }
exit if defined $pid;
}
continue {
if ($GOT_HUP) {
$SIG{CHLD} = 'DEFAULT';
wait;
- exec $^X . ' "' . $0 . '" ' . join(' ', @{$options->{argv}});
+ exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
}
}
return $line;
}
+sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
+
=back
=head1 SEE ALSO
# Setup restarter
my $restarter;
my $parent = $$;
-
+
unless ( $restarter = fork ) {
# Prepare
close STDIN;
close STDOUT;
-
+
my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
directory => File::Spec->catdir( $FindBin::Bin, '..' ),
regex => $options->{restart_regex},
delay => $options->{restart_delay},
);
+ $host ||= '127.0.0.1';
while (1) {
+
# poll for changed files
my @changed_files = $watcher->watch();
-
+
# check if our parent process has died
- exit if ( getppid == 1 );
-
+ exit if $^O ne 'MSWin32' and getppid == 1;
+
# Restart if any files have changed
- if ( @changed_files ) {
+ if (@changed_files) {
my $files = join ', ', @changed_files;
print STDERR qq/File(s) "$files" modified, restarting\n\n/;
- kill( 1, $parent );
+
+ require IO::Socket::INET;
+ require HTTP::Headers;
+ require HTTP::Request;
+
+ my $client = IO::Socket::INET->new(
+ PeerAddr => $host,
+ PeerPort => $port
+ )
+ or die "can't create client socket (is server running?): ",
+ $!;
+
+ # build the Kill request
+ my $req =
+ HTTP::Request->new( 'KILL', '/',
+ HTTP::Headers->new( 'Connection' => 'close' ) );
+ $req->protocol('HTTP/1.0');
+
+ $client->send( $req->as_string )
+ or die "can't send restart instruction: ", $!;
+ $client->close();
exit;
}
}