Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

honor no_proxy env variable

  • Loading branch information...
commit 8aea0596f44ab0c8acf268af96870ddd002f1035 1 parent 87ff461
Vounow authored drbrain committed
View
23 lib/rubygems/remote_fetcher.rb
@@ -81,6 +81,7 @@ def initialize(proxy=nil, dns=Resolv::DNS.new)
else URI.parse(proxy)
end
@user_agent = user_agent
+ @env_no_proxy = get_no_proxy_from_env
@dns = dns
end
@@ -334,6 +335,17 @@ def uri_escaper
end
##
+ # Returns list of no_proxy entries (if any) from the environment
+
+ def get_no_proxy_from_env
+ env_no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']
+
+ return [] if env_no_proxy.nil? or env_no_proxy.empty?
+
+ env_no_proxy_list = env_no_proxy.split(/\s*,\s*/)
+ end
+
+ ##
# Returns an HTTP proxy URI if one is set in the environment variables.
def get_proxy_from_env
@@ -366,7 +378,7 @@ def normalize_uri(uri)
def connection_for(uri)
net_http_args = [uri.host, uri.port]
- if @proxy_uri then
+ if @proxy_uri and not no_proxy?(uri.host) then
net_http_args += [
@proxy_uri.host,
@proxy_uri.port,
@@ -400,6 +412,15 @@ def correct_for_windows_path(path)
end
end
+ def no_proxy? host
+ host = host.downcase
+ @env_no_proxy.each do |pattern|
+ pattern = pattern.downcase
+ return true if host[-pattern.length, pattern.length ] == pattern
+ end
+ return false
+ end
+
##
# Performs a Net::HTTP request of type +request_class+ on +uri+ returning
# a Net::HTTP response object. request maintains a table of persistent
View
46 test/rubygems/test_gem_remote_fetcher.rb
@@ -85,6 +85,8 @@ def setup
ENV.delete 'HTTP_PROXY_USER'
ENV.delete 'http_proxy_pass'
ENV.delete 'HTTP_PROXY_PASS'
+ ENV.delete 'no_proxy'
+ ENV.delete 'NO_PROXY'
base_server_uri = "http://localhost:#{SERVER_PORT}"
@proxy_uri = "http://localhost:#{PROXY_PORT}"
@@ -639,6 +641,50 @@ def conn.request(req)
assert_equal "too many redirects (#{url})", e.message
end
+ def test_observe_no_proxy_env_single_host
+ orig_env_HTTP_PROXY = ENV['HTTP_PROXY']
+
+ orig_env_NO_PROXY = ENV["NO_PROXY"]
+ orig_env_no_proxy = ENV["no_proxy"]
+
+ use_ui @ui do
+ ENV["HTTP_PROXY"] = @proxy_uri
+ ENV["NO_PROXY"] = "#{URI::parse(@server_uri).host}"
+ fetcher = Gem::RemoteFetcher.new nil
+ assert_data_from_server fetcher.fetch_path(@server_uri)
+ end
+
+ ensure
+ orig_env_HTTP_PROXY.nil? ? ENV.delete('HTTP_PROXY') :
+ ENV['HTTP_PROXY'] = orig_env_HTTP_PROXY
+ orig_env_NO_PROXY.nil? ? ENV.delete('NO_PROXY') :
+ ENV['NO_PROXY'] = orig_env_NO_PROXY
+ orig_env_no_proxy.nil? ? ENV.delete('no_proxy') :
+ ENV['no_proxy'] = orig_env_no_proxy
+ end
+
+ def test_observe_no_proxy_env_list
+ orig_env_HTTP_PROXY = ENV['HTTP_PROXY']
+
+ orig_env_NO_PROXY = ENV["NO_PROXY"]
+ orig_env_no_proxy = ENV["no_proxy"]
+
+ use_ui @ui do
+ ENV["HTTP_PROXY"] = @proxy_uri
+ ENV["NO_PROXY"] = "fakeurl.com, #{URI::parse(@server_uri).host}"
+ fetcher = Gem::RemoteFetcher.new nil
+ assert_data_from_server fetcher.fetch_path(@server_uri)
+ end
+
+ ensure
+ orig_env_HTTP_PROXY.nil? ? ENV.delete('HTTP_PROXY') :
+ ENV['HTTP_PROXY'] = orig_env_HTTP_PROXY
+ orig_env_NO_PROXY.nil? ? ENV.delete('NO_PROXY') :
+ ENV['NO_PROXY'] = orig_env_NO_PROXY
+ orig_env_no_proxy.nil? ? ENV.delete('no_proxy') :
+ ENV['no_proxy'] = orig_env_no_proxy
+ end
+
def test_request
uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
util_stub_connection_for :body => :junk, :code => 200
Please sign in to comment.
Something went wrong with that request. Please try again.