Fixed: don't autmoatically resolve hostnames
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Daemon.pm
index d7e1440..92faf42 100644 (file)
@@ -3,7 +3,7 @@ package Catalyst::Engine::HTTP::Daemon;
 use strict;
 use base 'Catalyst::Engine::HTTP::Base';
 
-use IO::Socket qw(AF_INET INADDR_ANY SOCK_STREAM SOMAXCONN );
+use IO::Socket qw( SOCK_STREAM SOMAXCONN );
 
 =head1 NAME
 
@@ -33,16 +33,45 @@ This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
 
 =over 4
 
-=item $c->run
+=item $c->handler
 
 =cut
 
-$SIG{'PIPE'} = 'IGNORE';
+sub handler {
+    my ( $class, $client ) = @_;
+
+    $client->timeout(5);
+
+    while ( my $request = $client->get_request ) {
+
+        $request->uri->scheme('http');    # Force URI::http
+        $request->uri->host( $request->header('Host') || $client->sockhost );
+        $request->uri->port( $client->sockport );
+
+        my $http = Catalyst::Engine::HTTP::Base::struct->new(
+            address  => $client->peerhost,
+            request  => $request,
+            response => HTTP::Response->new
+        );
+
+        $class->SUPER::handler($http);
+
+        $client->send_response( $http->response );
+    }
+
+    $client->close;
+}
+
+=item $c->run
+
+=cut
 
 sub run {
     my $class = shift;
     my $port  = shift || 3000;
     
+    $SIG{'PIPE'} = 'IGNORE';
+    
     $HTTP::Daemon::PROTO = 'HTTP/1.0'; # For now until we resolve the blocking 
                                        # issues with HTTP 1.1
 
@@ -52,40 +81,17 @@ sub run {
         ReuseAddr => 1,
         Type      => SOCK_STREAM,
     );
-
-    unless ($daemon) {
-        die("Failed to create daemon: $!\n");
+    
+    unless ( defined $daemon ) {
+        die( qq/Failed to create daemon. Reason: '$!'/ );
     }
 
     my $base = URI->new( $daemon->url )->canonical;
 
     printf( "You can connect to your server at %s\n", $base );
 
-    while ( my $connection = $daemon->accept ) {
-
-        $connection->timeout(5);
-
-        while ( my $request = $connection->get_request ) {
-
-            $request->uri->scheme('http');    # Force URI::http
-            $request->uri->host( $request->header('Host') || $base->host );
-            $request->uri->port( $base->port );
-            
-            my $hostname = gethostbyaddr( $connection->peeraddr, AF_INET );
-
-            my $http = Catalyst::Engine::HTTP::Base::struct->new(
-                address  => $connection->peerhost,
-                hostname => $hostname || $connection->peerhost,
-                request  => $request,
-                response => HTTP::Response->new
-            );
-
-            $class->handler($http);
-            $connection->send_response( $http->response );
-        }
-
-        $connection->close;
-        undef($connection);
+    while ( my $client = $daemon->accept ) {
+        $class->handler($client);
     }
 }