From ce3c0b1e9e73828fb3052f4107381eb68b1d6ecd Mon Sep 17 00:00:00 2001
From: Gisle Aas <gisle@aas.no>
Date: Sat, 24 Oct 2009 00:03:58 +0200
Subject: [PATCH] Added more tests for setting IPv6 addresses using the host method
Revert to not stripping the brackets from the return value of
$uri->host_port since that gives surprising behavious when trying
to set the value.
---
URI/_server.pm | 11 +++++++----
t/rfc2732.t | 26 +++++++++++++++++++++-----
2 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/URI/_server.pm b/URI/_server.pm
index beeb9ca..efed014 100644
--- a/URI/_server.pm
+++ b/URI/_server.pm
@@ -38,7 +38,11 @@ sub host
$new = "" unless defined $new;
if (length $new) {
$new =~ s/[@]/%40/g; # protect @
- $port = $1 if $new =~ s/(:\d+)$//;
+ if ($new =~ /^[^:]*:\d*\z/ || $new =~ /]:\d*\z/) {
+ $new =~ s/(:\d*)\z// || die "Assert";
+ $port = $1;
+ }
+ $new = "[$new]" if $new =~ /:/ && $new !~ /^\[/; # IPv6 address
}
$self->authority("$ui$new$port");
}
@@ -79,9 +83,8 @@ sub host_port
$self->host(shift) if @_;
return undef unless defined $old;
$old =~ s/.*@//; # zap userinfo
- $old =~ s/:$//; # empty port does not could
- $old =~ s{^\[(.*)\]}{$1}; # strip brackets around IPv6 address
- $old .= ":" . $self->port unless $old =~ /:/;
+ $old =~ s/:$//; # empty port should be treated the same a no port
+ $old .= ":" . $self->port unless $old =~ /:\d+$/;
$old;
}
diff --git a/t/rfc2732.t b/t/rfc2732.t
index 54e07d4..0787e30 100644
--- a/t/rfc2732.t
+++ b/t/rfc2732.t
@@ -3,22 +3,38 @@
# Test URIs containing IPv6 addresses
use strict;
-use Test::More tests => 11;
+use Test::More tests => 19;
use URI;
my $uri = URI->new("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html");
is $uri->as_string, "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html";
-
is $uri->host, "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210";
-
-is $uri->host_port, "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:80";
-
+is $uri->host_port, "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80";
is $uri->port, "80";
+$uri->port(undef);
+is $uri->as_string, "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]/index.html";
+is $uri->host_port, "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80";
+$uri->port(80);
+
$uri->host("host");
is $uri->as_string, "
http://host:80/index.html";
+$uri->host("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210");
+is $uri->as_string, "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html";
+$uri->host_port("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:88");
+is $uri->as_string, "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:88/index.html";
+$uri->host_port("[::1]:80");
+is $uri->as_string, "http://[::1]:80/index.html";
+$uri->host("::1:80");
+is $uri->as_string, "http://[::1:80]:80/index.html";
+$uri->host("[::1:80]");
+is $uri->as_string, "http://[::1:80]:80/index.html";
+$uri->host("[::1]:88");
+is $uri->as_string, "http://[::1]:88/index.html";
+
+
$uri = URI->new("ftp://ftp:@[3ffe:2a00:100:7031::1]");
is $uri->as_string, "ftp://ftp:@[3ffe:2a00:100:7031::1]";
--
1.6.2.95.g934f7