Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1568,9 +1568,13 @@ def find_proxy(env=ENV)
end

def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
no_proxy_entries = no_proxy.scan(/([^:,\s]+)(?::(\d+))?/)
return false if no_proxy_entries.any? { |entry, _| entry == '*' }

hostname = hostname.downcase
dothostname = ".#{hostname}"
no_proxy.scan(/([^:,\s]+)(?::(\d+))?/) {|p_host, p_port|

no_proxy_entries.each {|p_host, p_port|
if !p_port || port == p_port.to_i
if p_host.start_with?('.')
return false if hostname.end_with?(p_host.downcase)
Expand Down
6 changes: 6 additions & 0 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ def test_find_proxy_no_proxy
assert_equal(URI('http://127.0.0.1:8080'), URI("http://example.org/").find_proxy(env))
assert_nil(URI("http://www.example.org/").find_proxy(env))
}
with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'*') {|env|
assert_nil(URI("http://www.example.org/").find_proxy(env))
}
ensure
IPSocket.singleton_class.class_eval do
undef getaddress
Expand Down Expand Up @@ -1008,8 +1011,10 @@ 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],
['example.com', nil, 80, 'example.org,*', false],
['foo.example.com', nil, 80, 'example.com', false],
['foo.example.com', nil, 80, '.example.com', false],
['foo.example.com', nil, 80, '*.example.com', true],
['example.com', nil, 80, '.example.com', true],
['xample.com', nil, 80, '.example.com', true],
['fooexample.com', nil, 80, '.example.com', true],
Expand All @@ -1020,6 +1025,7 @@ def test_use_proxy_p
['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],
['10.224.1.1', '10.224.1.1', 80, '*', 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}')"
Expand Down