Make JRuby inherit Java proxy settings

2019-06-20 15:11发布

I would like to make HTTP requests from Rails code running on top of JRuby.

How can I make it to re-use http.proxyHost, http.proxyPort and http.nonProxyHosts settings, given to JVM running it ?

2条回答
闹够了就滚
2楼-- · 2019-06-20 15:29

I have had the same issue. I found that java or net::http doesn't obey the nonProxyHosts option. The best way to get around this is to modify the ENV_JAVA settings to account for this.

The steps I took to ensure nonProxyHosts was used were the following:

1) JAVA_OPTS="-Dhttp.proxyHost=192*** -Dhttp.proxyPort=1234 -Dhttp.nonProxyHosts=local|127.0.0.1"
OR
1) JRUBY_OPTS="-J-Dhttp.proxyHost=192*** -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts=local|127.0.0.1"

Keep in mind that at least for java1.7 the nonProxyHosts should not have quotations see here.

Now I find that either net::http or java itself doesn't actually honour the nonProxyHosts option.

However you can get around this by doing the following in JRuby

a = URI("http://someurl")
Net::HTTP.new(a).proxy?.should == true
regex = /$#{ENV_JAVA["http.nonProxyHosts"]}/ #dollar needed to behave as expected
if a.hostname.match(regex)
   ENV_JAVA["http.proxyHost"]=nil
end
Net::HTTP.new(a).proxy?.should == false

Hope that helps.

查看更多
Viruses.
3楼-- · 2019-06-20 15:35

To pass JVM flags through JRuby, use -J.... In this case:

jruby -J-Dhttp.proxyHost=foo -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts="*.bar.com" ...

This is explained in JRuby's help text.

-J[java option] pass an option on to the JVM (e.g. -J-Xmx512m)
                use --properties to list JRuby properties
                run 'java -help' for a list of other Java options
查看更多
登录 后发表回答