Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make ENV-based proxy logic in net/http honor JVM properties for jruby/jruby#2983
diff --git a/net/http.rb b/net/http.rb
index 18e7ed0..aee7bf6 100644
--- a/net/http.rb
+++ b/net/http.rb
@@ -592,41 +592,41 @@ module Net #:nodoc:
# The +address+ should be a DNS hostname or IP address, the +port+ is the
# port the server operates on. If no +port+ is given the default port for
# HTTP or HTTPS is used.
#
# If none of the +p_+ arguments are given, the proxy host and port are
# taken from the +http_proxy+ environment variable (or its uppercase
# equivalent) if present. If the proxy requires authentication you must
# supply it by hand. See URI::Generic#find_proxy for details of proxy
# detection from the environment. To disable proxy detection set +p_addr+
# to nil.
#
# If you are connecting to a custom proxy, +p_addr+ the DNS name or IP
# address of the proxy host, +p_port+ the port to use to access the proxy,
# and +p_user+ and +p_pass+ the username and password if authorization is
# required to use the proxy.
#
# In JRuby, this will default to the JSE proxy settings provided in the
# 'http.proxyHost' and 'http.proxyPort' Java system properties, if they
# are set and no alternative proxy has been provided.
#
- def HTTP.new(address, port = nil, p_addr = ENV_JAVA['http.proxyHost'], p_port = ENV_JAVA['http.proxyPort'], p_user = nil, p_pass = nil)
+ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
http = super address, port
if proxy_class? then # from Net::HTTP::Proxy()
http.proxy_from_env = @proxy_from_env
http.proxy_address = @proxy_address
http.proxy_port = @proxy_port
http.proxy_user = @proxy_user
http.proxy_pass = @proxy_pass
elsif p_addr == :ENV then
http.proxy_from_env = true
else
http.proxy_address = p_addr
http.proxy_port = p_port || default_port
http.proxy_user = p_user
http.proxy_pass = p_pass
end
http
end
diff --git a/uri/generic.rb b/uri/generic.rb
index b1195fd..a135cdf 100644
--- a/uri/generic.rb
+++ b/uri/generic.rb
@@ -1621,55 +1621,86 @@ module URI
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'
- unless proxy_uri = ENV[name]
- if proxy_uri = ENV[name.upcase]
- warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.'
- end
- end
+ proxy_uri = http_proxy_from_env
else
proxy_uri = ENV[name] || ENV[name.upcase]
end
if proxy_uri.nil? || proxy_uri.empty?
return nil
end
if self.hostname
require 'socket'
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]
+ if no_proxy = no_proxy_from_env
no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host &&
(!port || self.port == port.to_i)
return nil
end
}
end
URI.parse(proxy_uri)
end
+
+ def http_proxy_from_env
+ proxy_host = ENV_JAVA['http.proxyHost']
+
+ if proxy_host
+ begin
+ proxy_port = (ENV_JAVA['http.proxyPort'] || '80').to_i
+ if proxy_port > 0
+ proxy_uri = "http://#{proxy_host}:#{proxy_port}"
+ end
+ end
+ end
+
+ if proxy_uri.nil? || proxy_uri.empty?
+ proxy_uri = ENV['http_proxy']
+
+ if proxy_uri.nil? && (proxy_uri = ENV['HTTP_PROXY'])
+ warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.'
+ end
+ end
+
+ proxy_uri
+ end
+ private :http_proxy_from_env
+
+ def no_proxy_from_env
+ no_proxy = ENV_JAVA['http.nonProxyHosts']
+
+ if no_proxy.nil? || no_proxy.empty?
+ name = "no_proxy"
+ no_proxy = ENV[name] || ENV[name.upcase]
+ end
+
+ no_proxy
+ end
+ private :no_proxy_from_env
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.