diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-10-11 07:53:56 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-10-11 07:53:56 +0000 |
commit | 1ab0740838a5ff69ca6762817c9167cd1be741a5 (patch) | |
tree | 600ae5f38cd07ff5797d2a31a07f7b5f083c667e /lib/uri | |
parent | ea8990d8670f71d6b1fa3d55086c9f147348452c (diff) |
Add an optional argument, env, to URI.find_proxy.
* lib/uri/generic.rb (URI.find_proxy): Add an optional argument, env.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56392 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/uri')
-rw-r--r-- | lib/uri/generic.rb | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 81d46f72a5..0ac91d5f39 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1486,6 +1486,8 @@ module URI # 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. # @@ -1494,41 +1496,41 @@ module URI # 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. - def find_proxy + def find_proxy(env=ENV) raise BadURIError, "relative URI: #{self}" if self.relative? name = self.scheme.downcase + '_proxy' proxy_uri = nil - if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI? + 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 } + 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 + if k == 'http_proxy' && env[k.upcase] == nil # http_proxy is safe to use because ENV is case sensitive. - proxy_uri = ENV[name] + 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] + proxy_uri = env.to_hash[name] end if !proxy_uri # Use CGI_HTTP_PROXY. cf. libwww-perl. - proxy_uri = ENV["CGI_#{name.upcase}"] + proxy_uri = env["CGI_#{name.upcase}"] end elsif name == 'http_proxy' - unless proxy_uri = ENV[name] - if proxy_uri = ENV[name.upcase] + unless proxy_uri = env[name] + if proxy_uri = env[name.upcase] warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.' end end else - proxy_uri = ENV[name] || ENV[name.upcase] + proxy_uri = env[name] || env[name.upcase] end if proxy_uri.nil? || proxy_uri.empty? |