From: Tanaka Akira Date: 2010-09-10T06:44:10+09:00 Subject: [ruby-core:32258] Re: [Ruby 1.8-Bug#3788] URI cannot parse IPv6 addresses propertly 2010/9/8 Adam Majer : > Issue #3788 has been updated by Adam Majer. > In my opinion, adding dnssafehost to URI is bad design. I think adding a method is a good design. > My expectation was that URI.host should return unescaped hostname/IP and URI.host= should take unescaped as argument and escape them to produce a valid URI. This interface could then be easily extended to work with IRI (International domains). > > > maybe the solution is to rename current members host and host= to host_rfc3986 and host_rfc3986=. Similar syntax already exists in other classes like Time. It introduces an incompatibility. Also, the primitive method to get/set the host part will be version dependent. I think adding a new method for deal brackets is better. % svn diff --diff-cmd diff -x '-u -p' Index: lib/uri/generic.rb =================================================================== --- lib/uri/generic.rb (revision 29195) +++ lib/uri/generic.rb (working copy) @@ -412,6 +412,38 @@ module URI v end + # extract the host part of the URI and unwrap brackets for IPv6 addresses. + # + # This method is same as URI::Generic#host except + # brackets for IPv6 (andn future IP) addresses are removed. + # + # u = URI("http://[::1]/bar") + # p u.hostname #=> "::1" + # p u.host #=> "[::1]" + # + def hostname + v = self.host + /\A\[(.*)\]\z/ =~ v ? $1 : v + end + + # set the host part of the URI as the argument with brackets for IPv6 addresses. + # + # This method is same as URI::Generic#host= except + # the argument can be bare IPv6 address. + # + # u = URI("http://foo/bar") + # p u.to_s #=> "http://foo/bar" + # u.hostname = "::1" + # p u.to_s #=> "http://[::1]/bar" + # + # If the arugument seems IPv6 address, + # it is wrapped by brackets. + # + def hostname=(v) + v = "[#{v}]" if /\A\[.*\]\z/ !~ v && /:/ =~ v + self.host = v + end + def check_port(v) return v unless v > http://redmine.ruby-lang.org/issues/show/3788 -- Tanaka Akira