Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

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
Closed
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
24 changes: 22 additions & 2 deletions lib/rubygems/remote_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -329,21 +329,41 @@ def get_proxy_from_env
uri
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,
Expand Down
46 changes: 46 additions & 0 deletions test/rubygems/test_gem_remote_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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'
Expand Down