As I studied the code relating to server restarting, I made a few small 
refactors to the related documentation to make the code clearer and easier to 
follow. The result should be functionally the same. A patch is attached which 
supplies the following updates. It may depend on related patch I submitted 
earlier. 

 * Improve the code clarity by encapsulating server restart permissions code 
in its own subroutine
  * document "_inet_addr"
  * document where inet_aton comes from
  * clarify $ip vs $ipaddr variable names.
      One is a $remote_ip, the other is an $allowed_ip
Sun Jul 22 13:45:10 EDT 2007  Mark Stosberg <[EMAIL PROTECTED]>
  * Improve the code clarity by encapsulating server restart permissions code in its own subroutine
  * document "_inet_addr"
  * document where inet_aton comes from
  * clarify $ip vs $ipaddr variable names.
      One is a $remote_ip, the other is an $allowed_ip
diff -rN -u old-catalyst_vs_cgiapp/lib/Catalyst/Engine/HTTP.pm new-catalyst_vs_cgiapp/lib/Catalyst/Engine/HTTP.pm
--- old-catalyst_vs_cgiapp/lib/Catalyst/Engine/HTTP.pm	2007-07-22 13:51:47.000000000 -0400
+++ new-catalyst_vs_cgiapp/lib/Catalyst/Engine/HTTP.pm	2007-07-22 13:51:47.000000000 -0400
@@ -8,7 +8,7 @@
 use HTTP::Headers;
 use HTTP::Status;
 use NEXT;
-use Socket;
+use Socket; # provides inet_aton
 use IO::Socket::INET ();
 use IO::Select       ();
 
@@ -201,7 +201,6 @@
     my $restart = 0;
     local $SIG{CHLD} = 'IGNORE';
 
-    my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
     my $addr = $host ? inet_aton($host) : INADDR_ANY;
     if ( $addr eq INADDR_ANY ) {
         require Sys::Hostname;
@@ -254,7 +253,9 @@
     local $SIG{PIPE} = 'IGNORE';
     
     LISTEN:
+    # If we haven't been instructed to restart...
     while ( !$restart ) {
+        # ...start listening for connections
         while ( accept( Remote, $daemon ) ) {        
             DEBUG && warn "New connection\n";
 
@@ -277,15 +278,8 @@
             next unless $method;
 
             if ( uc($method) eq 'RESTART' ) {
-                my $sockdata = $self->_socket_data( \*Remote );
-                my $ipaddr   = _inet_addr( $sockdata->{peeraddr} );
-                my $ready    = 0;
-                for my $ip ( keys %$allowed ) {
-                    my $mask = $allowed->{$ip};
-                    $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
-                    last if $ready;
-                }
-                if ($ready) {
+                my $ip_mask_map = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
+                if ( $self->_restart_is_allowed( \*Remote, $ip_mask_map ) ) {
                     $restart = 1;
                     last;
                 }
@@ -516,8 +510,27 @@
     return $data;
 }
 
+# Take a host name or IP addresses and unpack it using 
+# the pattern for "An unsigned long in "network" (big-endian) order."
+# unpack returns one more numbers as a result. We just care about the first one.
 sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
 
+sub _restart_is_allowed {
+    my ($self, $socket, $ip_mask_map) = @_;
+
+    my $sockdata = $self->_socket_data( $socket );
+    my $remote_ip   = _inet_addr( $sockdata->{peeraddr} );
+    my $ready    = 0;
+
+    for my $allowed_ip ( keys %$ip_mask_map ) {
+        my $mask = $ip_mask_map->{$allowed_ip};
+        $ready = ( $remote_ip & _inet_addr($mask) ) == _inet_addr($allowed_ip);
+        last if $ready;
+    }
+
+    return $ready;
+}
+
 =head1 SEE ALSO
 
 L<Catalyst>, L<Catalyst::Engine>.
@@ -532,6 +545,8 @@
 
 Andy Grundman, <[EMAIL PROTECTED]>
 
+Mark Stosberg, <[EMAIL PROTECTED]>
+
 =head1 THANKS
 
 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.

_______________________________________________
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/

Reply via email to