2f0054
From e49f553aa8be21e5df72452e50af2e9f0b82ecad Mon Sep 17 00:00:00 2001
2f0054
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
2f0054
Date: Wed, 23 May 2018 17:31:42 +0200
2f0054
Subject: [PATCH] Resolve specific socket addresses correctly
2f0054
MIME-Version: 1.0
2f0054
Content-Type: text/plain; charset=UTF-8
2f0054
Content-Transfer-Encoding: 8bit
2f0054
2f0054
Previous code did not formatted specific (not 0.0.0.0 or ::)
2f0054
correctly:
2f0054
2f0054
$ perl -MHTTP::Daemon -e '$d=HTTP::Daemon->new(LocalAddr=>q{127.0.0.2}) or die; print $d->url, qq{\n}'
2f0054
Can't call method "sockhostname" without a package or object reference at /usr/share/perl5/vendor_perl/HTTP/Daemon.pm line 64.
2f0054
2f0054
This patch also fixes formatting numerical IPv6 addresses. It seems
2f0054
that IO::Socket::IP::sockhostname() formats unresolvable addresses too.
2f0054
2f0054
Signed-off-by: Petr Písař <ppisar@redhat.com>
2f0054
---
2f0054
 lib/HTTP/Daemon.pm | 15 +++++++++++++--
2f0054
 1 file changed, 13 insertions(+), 2 deletions(-)
2f0054
2f0054
diff --git a/lib/HTTP/Daemon.pm b/lib/HTTP/Daemon.pm
2f0054
index 1e9d48e..216c73f 100644
2f0054
--- a/lib/HTTP/Daemon.pm
2f0054
+++ b/lib/HTTP/Daemon.pm
2f0054
@@ -61,12 +61,23 @@ sub url
2f0054
 	$url .= '[' . inet_ntop(AF_INET6, $addr) . ']';
2f0054
     }
2f0054
     else {
2f0054
-	my $host = $addr->sockhostname;
2f0054
+	my $host = $self->sockhostname;
2f0054
+	# sockhostname() seems to return a stringified IP address if not
2f0054
+	# resolvable, then quote it for a port separator and an IPv6 zone separator.
2f0054
+	# But be paranoid for a case when it already contains a bracket.
2f0054
+	if (defined $host and $host =~ /:/) {
2f0054
+	    if ($host =~ /[\[\]]/) {
2f0054
+		$host = undef;
2f0054
+	    } else {
2f0054
+		$host =~ s/%/%25/g;
2f0054
+		$host  = '[' . $host . ']';
2f0054
+	    }
2f0054
+	}
2f0054
         if (!defined $host) {
2f0054
 	    if (sockaddr_family($addr) eq AF_INET6) {
2f0054
 		$host = '[' . inet_ntop(AF_INET6, $addr) . ']';
2f0054
 	    } else {
2f0054
-		$host = inet_ntop(AF_INET6, $addr);
2f0054
+		$host = inet_ntop(AF_INET, $addr);
2f0054
 	    }
2f0054
 	}
2f0054
 	$url .= $host;
2f0054
-- 
2f0054
2.14.3
2f0054