[前][次][番号順一覧][スレッド一覧]

ruby-changes:17411

From: akr <ko1@a...>
Date: Wed, 6 Oct 2010 12:30:57 +0900 (JST)
Subject: [ruby-changes:17411] Ruby:r29416 (trunk): * lib/uri/generic.rb (URI::Generic#hostname): new method.

akr	2010-10-06 12:30:49 +0900 (Wed, 06 Oct 2010)

  New Revision: 29416

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29416

  Log:
    * 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]

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/http.rb
    trunk/lib/open-uri.rb
    trunk/lib/uri/generic.rb
    trunk/test/uri/test_generic.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 29415)
+++ ChangeLog	(revision 29416)
@@ -1,3 +1,14 @@
+Wed Oct  6 12:28:22 2010  Tanaka Akira  <akr@f...>
+
+	* 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]
+
 Wed Oct  6 11:52:12 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* io.c (fptr_finalize): write_mutex might have been destroyed
Index: lib/open-uri.rb
===================================================================
--- lib/open-uri.rb	(revision 29415)
+++ lib/open-uri.rb	(revision 29416)
@@ -263,17 +263,17 @@
       # HTTP or HTTPS
       if proxy
         if proxy_user && proxy_pass
-          klass = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_user, proxy_pass)
+          klass = Net::HTTP::Proxy(proxy_uri.hostname, proxy_uri.port, proxy_user, proxy_pass)
         else
-          klass = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port)
+          klass = Net::HTTP::Proxy(proxy_uri.hostname, proxy_uri.port)
         end
       end
-      target_host = target.host
+      target_host = target.hostname
       target_port = target.port
       request_uri = target.request_uri
     else
       # FTP over HTTP proxy
-      target_host = proxy_uri.host
+      target_host = proxy_uri.hostname
       target_port = proxy_uri.port
       request_uri = target.to_s
       if proxy_user && proxy_pass
@@ -736,10 +736,10 @@
         proxy_uri = ENV[name] || ENV[name.upcase]
       end
 
-      if proxy_uri && self.host
+      if proxy_uri && self.hostname
         require 'socket'
         begin
-          addr = IPSocket.getaddress(self.host)
+          addr = IPSocket.getaddress(self.hostname)
           proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
         rescue SocketError
         end
@@ -804,7 +804,7 @@
 
       # The access sequence is defined by RFC 1738
       ftp = Net::FTP.new
-      ftp.connect(self.host, self.port)
+      ftp.connect(self.hostname, self.port)
       ftp.passive = true if !options[:ftp_active_mode]
       # todo: extract user/passwd from .netrc.
       user = 'anonymous'
Index: lib/uri/generic.rb
===================================================================
--- lib/uri/generic.rb	(revision 29415)
+++ lib/uri/generic.rb	(revision 29416)
@@ -205,8 +205,31 @@
       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@e...").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 @@
       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
 
Index: lib/net/http.rb
===================================================================
--- lib/net/http.rb	(revision 29415)
+++ lib/net/http.rb	(revision 29416)
@@ -64,7 +64,7 @@
   #     require 'uri'
   #
   #     url = URI.parse('http://www.example.com/index.html')
-  #     res = Net::HTTP.start(url.host, url.port) {|http|
+  #     res = Net::HTTP.start(url.hostname, url.port) {|http|
   #       http.get('/index.html')
   #     }
   #     puts res.body
@@ -75,7 +75,7 @@
   #
   #     url = URI.parse('http://www.example.com/index.html')
   #     req = Net::HTTP::Get.new(url.path)
-  #     res = Net::HTTP.start(url.host, url.port) {|http|
+  #     res = Net::HTTP.start(url.hostname, url.port) {|http|
   #       http.request(req)
   #     }
   #     puts res.body
@@ -101,7 +101,7 @@
   #     req = Net::HTTP::Post.new(url.path)
   #     req.basic_auth 'jack', 'pass'
   #     req.set_form_data({'from' => '2005-01-01', 'to' => '2005-03-31'}, ';')
-  #     res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
+  #     res = Net::HTTP.new(url.hostname, url.port).start {|http| http.request(req) }
   #     case res
   #     when Net::HTTPSuccess, Net::HTTPRedirection
   #       # OK
@@ -390,7 +390,7 @@
         }
       else
         uri = uri_or_host
-        new(uri.host, uri.port).start {|http|
+        new(uri.hostname, uri.port).start {|http|
           return http.request_get(uri.request_uri, &block)
         }
       end
@@ -415,7 +415,7 @@
       req = Post.new(url.path)
       req.form_data = params
       req.basic_auth url.user, url.password if url.user
-      new(url.host, url.port).start {|http|
+      new(url.hostname, url.port).start {|http|
         http.request(req)
       }
     end
Index: test/uri/test_generic.rb
===================================================================
--- test/uri/test_generic.rb	(revision 29415)
+++ test/uri/test_generic.rb	(revision 29416)
@@ -695,4 +695,14 @@
     assert_raise(URI::InvalidURIError) { uri.path = 'bar' }
     assert_raise(URI::InvalidURIError) { uri.query = 'bar' }
   end
+
+  def test_ipv6
+    assert_equal("[::1]", URI("http://[::1]/bar/baz").host)
+    assert_equal("::1", URI("http://[::1]/bar/baz").hostname)
+
+    u = URI("http://foo/bar")
+    assert_equal("http://foo/bar", u.to_s)
+    u.hostname = "::1"
+    assert_equal("http://[::1]/bar", u.to_s)
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]