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

ruby-changes:47938

From: naruse <ko1@a...>
Date: Thu, 28 Sep 2017 19:51:37 +0900 (JST)
Subject: [ruby-changes:47938] naruse:r60053 (trunk): URI::Generic: Separate no_proxy handling

naruse	2017-09-28 19:51:31 +0900 (Thu, 28 Sep 2017)

  New Revision: 60053

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60053

  Log:
    URI::Generic: Separate no_proxy handling
    
    To share with Net::HTTP. see #11195

  Modified files:
    trunk/lib/uri/generic.rb
    trunk/test/uri/test_generic.rb
Index: test/uri/test_generic.rb
===================================================================
--- test/uri/test_generic.rb	(revision 60052)
+++ test/uri/test_generic.rb	(revision 60053)
@@ -906,6 +906,23 @@ class URI::TestGeneric < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/uri/test_generic.rb#L906
     }
   end
 
+  def test_use_proxy_p
+    [
+      ['example.com', nil, 80, '', true],
+      ['example.com', nil, 80, 'example.com:80', false],
+      ['example.com', nil, 80, 'example.org,example.com:80,example.net', false],
+      ['foo.example.com', nil, 80, 'example.com', false],
+      ['foo.example.com', nil, 80, 'example.com:80', false],
+      ['foo.example.com', nil, 80, 'example.com:443', true],
+      ['127.0.0.1', '127.0.0.1', 80, '10.224.0.0/22', true],
+      ['10.224.1.1', '10.224.1.1', 80, '10.224.1.1', false],
+      ['10.224.1.1', '10.224.1.1', 80, '10.224.0.0/22', false],
+    ].each do |hostname, addr, port, no_proxy, expected|
+      assert_equal expected, URI::Generic.use_proxy?(hostname, addr, port, no_proxy),
+        "use_proxy?('#{hostname}', '#{addr}', #{port}, '#{no_proxy}')"
+    end
+  end
+
   class CaseInsensitiveEnv
     def initialize(h={})
       @h = {}
Index: lib/uri/generic.rb
===================================================================
--- lib/uri/generic.rb	(revision 60052)
+++ lib/uri/generic.rb	(revision 60053)
@@ -10,6 +10,8 @@ https://github.com/ruby/ruby/blob/trunk/lib/uri/generic.rb#L10
 #
 
 require 'uri/common'
+autoload :IPSocket, 'socket'
+autoload :IPAddr, 'ipaddr'
 
 module URI
 
@@ -1527,7 +1529,6 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/generic.rb#L1529
       end
 
       if self.hostname
-        require 'socket'
         begin
           addr = IPSocket.getaddress(self.hostname)
           return nil if /\A127\.|\A::1\z/ =~ addr
@@ -1537,23 +1538,26 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/generic.rb#L1538
 
       name = 'no_proxy'
       if no_proxy = env[name] || env[name.upcase]
-        no_proxy.scan(/(?!\.)([^:,\s]+)(?::(\d+))?/) {|host, port|
-          if (!port || self.port == port.to_i)
-            if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host
-              return nil
-            elsif addr
-              require 'ipaddr'
-              return nil if
-                begin
-                  IPAddr.new(host)
-                rescue IPAddr::InvalidAddressError
-                  next
-                end.include?(addr)
-            end
-          end
-        }
+        return nil unless URI::Generic.use_proxy?(self.hostname, addr, self.port, no_proxy)
       end
       URI.parse(proxy_uri)
     end
+
+    def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
+      no_proxy.scan(/(?!\.)([^:,\s]+)(?::(\d+))?/) {|p_host, p_port|
+        if !p_port || port == p_port.to_i
+          if /(\A|\.)#{Regexp.quote p_host}\z/i =~ hostname
+            return false
+          elsif addr
+            begin
+              return false if IPAddr.new(p_host).include?(addr)
+            rescue IPAddr::InvalidAddressError
+              next
+            end
+          end
+        end
+      }
+      true
+    end
   end
 end

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

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