Skip to content

Commit 60d0074

Browse files
committed
Split .use_proxy? into 2 dedicated methods
1 parent 913690f commit 60d0074

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

lib/uri/generic.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ def find_proxy(env=ENV)
15541554

15551555
name = 'no_proxy'
15561556
if self.hostname && (no_proxy = env[name] || env[name.upcase])
1557-
if no_proxy && !URI::Generic.use_proxy?(self.hostname, nil, self.port, no_proxy)
1557+
if no_proxy && !URI::Generic.hostname_use_proxy?(self.hostname, self.port, no_proxy)
15581558
return nil
15591559
end
15601560

@@ -1564,13 +1564,13 @@ def find_proxy(env=ENV)
15641564
rescue SocketError
15651565
end
15661566

1567-
return nil if no_proxy && !URI::Generic.use_proxy?(self.hostname, addr, self.port, no_proxy)
1567+
return nil if no_proxy && !URI::Generic.addr_use_proxy?(addr, self.port, no_proxy)
15681568
end
15691569

15701570
URI.parse(proxy_uri)
15711571
end
15721572

1573-
def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
1573+
def self.hostname_use_proxy?(hostname, port, no_proxy) # :nodoc:
15741574
hostname = hostname.downcase
15751575
dothostname = ".#{hostname}"
15761576
no_proxy.scan(/([^:,\s]+)(?::(\d+))?/) {|p_host, p_port|
@@ -1580,12 +1580,20 @@ def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
15801580
else
15811581
return false if dothostname.end_with?(".#{p_host.downcase}")
15821582
end
1583-
if addr
1584-
begin
1585-
return false if IPAddr.new(p_host).include?(addr)
1586-
rescue IPAddr::InvalidAddressError
1587-
next
1588-
end
1583+
end
1584+
}
1585+
true
1586+
end
1587+
1588+
def self.addr_use_proxy?(addr, port, no_proxy) # :nodoc:
1589+
return false if addr.nil?
1590+
1591+
no_proxy.scan(/([^:,\s]+)(?::(\d+))?/) {|p_host, p_port|
1592+
if !p_port || port == p_port.to_i
1593+
begin
1594+
return false if IPAddr.new(p_host).include?(addr)
1595+
rescue IPAddr::InvalidAddressError
1596+
next
15891597
end
15901598
end
15911599
}

test/uri/test_generic.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,26 +1034,34 @@ def test_find_proxy_case_sensitive_env
10341034
}
10351035
end
10361036

1037-
def test_use_proxy_p
1037+
def test_hostname_use_proxy_p
10381038
[
1039-
['example.com', nil, 80, '', true],
1040-
['example.com', nil, 80, 'example.com:80', false],
1041-
['example.com', nil, 80, 'example.org,example.com:80,example.net', false],
1042-
['foo.example.com', nil, 80, 'example.com', false],
1043-
['foo.example.com', nil, 80, '.example.com', false],
1044-
['example.com', nil, 80, '.example.com', true],
1045-
['xample.com', nil, 80, '.example.com', true],
1046-
['fooexample.com', nil, 80, '.example.com', true],
1047-
['foo.example.com', nil, 80, 'example.com:80', false],
1048-
['foo.eXample.com', nil, 80, 'example.com:80', false],
1049-
['foo.example.com', nil, 80, 'eXample.com:80', false],
1050-
['foo.example.com', nil, 80, 'example.com:443', true],
1051-
['127.0.0.1', '127.0.0.1', 80, '10.224.0.0/22', true],
1052-
['10.224.1.1', '10.224.1.1', 80, '10.224.1.1', false],
1053-
['10.224.1.1', '10.224.1.1', 80, '10.224.0.0/22', false],
1054-
].each do |hostname, addr, port, no_proxy, expected|
1055-
assert_equal expected, URI::Generic.use_proxy?(hostname, addr, port, no_proxy),
1056-
"use_proxy?('#{hostname}', '#{addr}', #{port}, '#{no_proxy}')"
1039+
['example.com', 80, '', true],
1040+
['example.com', 80, 'example.com:80', false],
1041+
['example.com', 80, 'example.org,example.com:80,example.net', false],
1042+
['foo.example.com', 80, 'example.com', false],
1043+
['foo.example.com', 80, '.example.com', false],
1044+
['example.com', 80, '.example.com', true],
1045+
['xample.com', 80, '.example.com', true],
1046+
['fooexample.com', 80, '.example.com', true],
1047+
['foo.example.com', 80, 'example.com:80', false],
1048+
['foo.eXample.com', 80, 'example.com:80', false],
1049+
['foo.example.com', 80, 'eXample.com:80', false],
1050+
['foo.example.com', 80, 'example.com:443', true],
1051+
].each do |hostname, port, no_proxy, expected|
1052+
assert_equal expected, URI::Generic.hostname_use_proxy?(hostname, port, no_proxy),
1053+
"use_proxy?('#{hostname}', #{port}, '#{no_proxy}')"
1054+
end
1055+
end
1056+
1057+
def test_addr_use_proxy_p
1058+
[
1059+
['127.0.0.1', 80, '10.224.0.0/22', true],
1060+
['10.224.1.1', 80, '10.224.1.1', false],
1061+
['10.224.1.1', 80, '10.224.0.0/22', false],
1062+
].each do |addr, port, no_proxy, expected|
1063+
assert_equal expected, URI::Generic.addr_use_proxy?(addr, port, no_proxy),
1064+
"use_proxy?('#{addr}', #{port}, '#{no_proxy}')"
10571065
end
10581066
end
10591067

0 commit comments

Comments
 (0)