Method: Bundler::URI::Generic#find_proxy

Defined in:
lib/bundler/vendor/uri/lib/uri/generic.rb

#find_proxy(env = ENV) ⇒ Object

Returns a proxy Bundler::URI. The proxy Bundler::URI is obtained from environment variables such as http_proxy, ftp_proxy, no_proxy, etc. If there is no proper proxy, nil is returned.

If the optional parameter env is specified, it is used instead of ENV.

Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.) are examined, too.

But http_proxy and HTTP_PROXY is treated specially under CGI environment. It’s because HTTP_PROXY may be set by Proxy: header. So HTTP_PROXY is not used. http_proxy is not used too if the variable is case insensitive. CGI_HTTP_PROXY can be used instead.

Raises:



1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
# File 'lib/bundler/vendor/uri/lib/uri/generic.rb', line 1490

def find_proxy(env=ENV)
  raise BadURIError, "relative Bundler::URI: #{self}" if self.relative?
  name = self.scheme.downcase + '_proxy'
  proxy_uri = nil
  if name == 'http_proxy' && env.include?('REQUEST_METHOD') # CGI?
    # HTTP_PROXY conflicts with *_proxy for proxy settings and
    # HTTP_* for header information in CGI.
    # So it should be careful to use it.
    pairs = env.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
    case pairs.length
    when 0 # no proxy setting anyway.
      proxy_uri = nil
    when 1
      k, _ = pairs.shift
      if k == 'http_proxy' && env[k.upcase] == nil
        # http_proxy is safe to use because ENV is case sensitive.
        proxy_uri = env[name]
      else
        proxy_uri = nil
      end
    else # http_proxy is safe to use because ENV is case sensitive.
      proxy_uri = env.to_hash[name]
    end
    if !proxy_uri
      # Use CGI_HTTP_PROXY.  cf. libwww-perl.
      proxy_uri = env["CGI_#{name.upcase}"]
    end
  elsif name == 'http_proxy'
    if RUBY_ENGINE == 'jruby' && p_addr = ENV_JAVA['http.proxyHost']
      p_port = ENV_JAVA['http.proxyPort']
      if p_user = ENV_JAVA['http.proxyUser']
        p_pass = ENV_JAVA['http.proxyPass']
        proxy_uri = "http://#{p_user}:#{p_pass}@#{p_addr}:#{p_port}"
      else
        proxy_uri = "http://#{p_addr}:#{p_port}"
      end
    else
      unless proxy_uri = env[name]
        if proxy_uri = env[name.upcase]
          warn 'The environment variable HTTP_PROXY is discouraged.  Use http_proxy.', uplevel: 1
        end
      end
    end
  else
    proxy_uri = env[name] || env[name.upcase]
  end

  if proxy_uri.nil? || proxy_uri.empty?
    return nil
  end

  if self.hostname
    begin
      addr = IPSocket.getaddress(self.hostname)
      return nil if /\A127\.|\A::1\z/ =~ addr
    rescue SocketError
    end
  end

  name = 'no_proxy'
  if no_proxy = env[name] || env[name.upcase]
    return nil unless Bundler::URI::Generic.use_proxy?(self.hostname, addr, self.port, no_proxy)
  end
  Bundler::URI.parse(proxy_uri)
end