diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-10-06 03:30:49 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-10-06 03:30:49 +0000 |
commit | 5fd45a4b79dd26f9e7b6dc41142912df911e4d7d (patch) | |
tree | 7cdb7ea6a408e863811acd5ba6c44ad0380fe6b1 /lib/uri | |
parent | 915ae780c37478fea358d6c77513a728e86a10f2 (diff) | |
download | ruby-5fd45a4b79dd26f9e7b6dc41142912df911e4d7d.tar.gz |
* lib/uri/generic.rb (URI::Generic#hostname): new method.
(URI::Generic#hostname=): ditto.
* lib/open-uri.rb: use URI#hostname
* lib/net/http.rb: ditto.
[ruby-core:32056]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/uri')
-rw-r--r-- | lib/uri/generic.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index f20b2d26e1..f8354e1835 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -205,8 +205,31 @@ module URI self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2) self.set_port(self.default_port) if self.default_port && !@port end + attr_reader :scheme + + # returns the host component of the URI. + # + # URI("http://foo/bar/baz").host #=> "foo" + # + # It returns nil if no host component. + # + # URI("mailto:foo@example.org").host #=> nil + # + # The component doesn't contains the port number. + # + # URI("http://foo:8080/bar/baz").host #=> "foo" + # + # Since IPv6 addresses are wrapped by brackets in URIs, + # this method returns IPv6 addresses wrapped by brackets. + # This form is not appropriate to pass socket methods such as TCPSocket.open. + # If unwrapped host names are required, use "hostname" method. + # + # URI("http://[::1]/bar/baz").host #=> "[::1]" + # URI("http://[::1]/bar/baz").hostname #=> "::1" + # attr_reader :host + attr_reader :port attr_reader :registry attr_reader :path @@ -412,6 +435,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 |