summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-28 10:51:31 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-28 10:51:31 +0000
commitec7c76c446fcb7fafae2fa2f7eda78c2387fac23 (patch)
tree83acc0f1f980386192d9566e012087a21ca443d5 /lib
parente4136c7bee2091c52915ef94432e673174306bb8 (diff)
URI::Generic: Separate no_proxy handling
To share with Net::HTTP. see #11195 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60053 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/uri/generic.rb36
1 files changed, 20 insertions, 16 deletions
diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb
index ffbd720dda..2a066a4cb6 100644
--- a/lib/uri/generic.rb
+++ b/lib/uri/generic.rb
@@ -10,6 +10,8 @@
#
require 'uri/common'
+autoload :IPSocket, 'socket'
+autoload :IPAddr, 'ipaddr'
module URI
@@ -1527,7 +1529,6 @@ module URI
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
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