Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

fix for issue 195 https://github.com/rubygems/rubygems/issues/195 (honor NO_PROXY env) #199

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+68 −2
Split
@@ -77,6 +77,7 @@ def initialize(proxy = nil)
else URI.parse(proxy)
end
@user_agent = user_agent
+ @env_no_proxy = get_no_proxy_from_env
end
##
@@ -239,7 +240,6 @@ def fetch_path(uri, mtime = nil, head = false)
end
data = send "fetch_#{uri.scheme}", uri, mtime, head
-
if data and !head and uri.to_s =~ /gz$/
begin
data = Gem.gunzip data
@@ -330,20 +330,40 @@ def get_proxy_from_env
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
+
+ ##
# Normalize the URI by adding "http://" if it is missing.
def normalize_uri(uri)
(uri =~ /^(https?|ftp|file):/) ? uri : "http://#{uri}"
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
+
##
# Creates or an HTTP connection based on +uri+, or retrieves an existing
# connection, using a proxy if needed.
def connection_for(uri)
net_http_args = [uri.host, uri.port]
- if @proxy_uri then
+ if @proxy_uri && !no_proxy?(uri.host) then
net_http_args += [
@proxy_uri.host,
@proxy_uri.port,
@@ -84,6 +84,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}"
@@ -572,6 +574,50 @@ def test_implicit_proxy_no_env
end
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_fetch_http
fetcher = Gem::RemoteFetcher.new nil
url = 'http://gems.example.com/redirect'