don't set the address in the hostname if lookup fails
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request.pm
index 19e7331..53aa8ca 100644 (file)
@@ -1,6 +1,6 @@
 package Catalyst::Request;
 
-use Socket qw( getaddrinfo getnameinfo AI_NUMERICHOST NI_DGRAM NIx_NOSERV );
+use Socket qw( getaddrinfo getnameinfo AI_NUMERICHOST NI_NAMEREQD NIx_NOSERV );
 use Carp;
 use utf8;
 use URI::http;
@@ -432,7 +432,6 @@ sub body {
 
 has hostname => (
   is        => 'rw',
-  required  => 1,
   lazy      => 1,
   default   => sub {
     my ($self) = @_;
@@ -442,14 +441,21 @@ has hostname => (
         '',
         { flags => AI_NUMERICHOST }
     );
-    return $self->address
-        if $err;
+    if ( $err ) {
+        $self->_log->warn("resolve of hostname failed: $err");
+        return $self->address;
+    }
     ( $err, my $hostname ) = getnameinfo(
         $sockaddr->{addr},
+        NI_NAMEREQD,
         # we are only interested in the hostname, not the servicename
-        NI_DGRAM|NIx_NOSERV
+        NIx_NOSERV
     );
-    return $err ? $self->address : $hostname;
+    if ( $err ) {
+        $self->_log->warn("resolve of hostname failed: $err");
+        return $self->address;
+    }
+    return $hostname;
   },
 );