Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make JRuby inherit Java proxy settings

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 ?

like image 801
Dmitry Khalatov Avatar asked Oct 13 '22 17:10

Dmitry Khalatov


2 Answers

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
like image 171
banzaiman Avatar answered Nov 09 '22 13:11

banzaiman


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.

like image 44
damau Avatar answered Nov 09 '22 13:11

damau