Faraday: Overriding environmental proxy settings

Created on 28 Nov 2017  ·  5Comments  ·  Source: lostisland/faraday

I am unable to figure out if there's a method with Faraday for overriding environment variable proxy settings (http_proxy, https_proxy, no_proxy). I've tried setting proxy=nil in a Faraday.new argument and configuration block, but that does not seem to work:

> c = Faraday.new('https://www.google.com', proxy: nil)
 => #<Faraday::Connection:0x00007fd1e2176180 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.13.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x00007fd1e2175d20 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTPS https://www.google.com/>, @proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://127.0.0.1:3128/>>, @temp_proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://127.0.0.1:3128/>>>
> c = Faraday.new('https://www.google.com') { |c| c.proxy=nil 
c.adapter :net_http
}
(Object doesn't support #inspect)
> c = Faraday.new('https://www.google.com', proxy: '')
 => #<Faraday::Connection:0x00007fd1e28f2300 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.13.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x00007fd1e28f1d60 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTPS https://www.google.com/>, @proxy=#<Faraday::ProxyOptions uri=#<URI::Generic >>, @temp_proxy=#<Faraday::ProxyOptions uri=#<URI::Generic >>>
> c = Faraday.new('https://www.google.com/') { |co|
2.4.2 :025 >     co.proxy = ''
2.4.2 :026?>   co.adapter :net_http
2.4.2 :027?>   }
 => #<Faraday::Connection:0x00007fe0af1bd4c8 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.13.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x00007fe0af1bd1d0 @handlers=[Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTPS https://nexus-gss.uscis.dhs.gov/>, @proxy=#<Faraday::ProxyOptions uri=#<URI::Generic >>, @temp_proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://127.0.0.1:3128/>>>

That last result is ProxyOptions.from(URI::Generic) or somesuch being assigned as the proxy, and it is still yielding a non-nil proxy

https://github.com/lostisland/faraday/blob/master/lib/faraday/adapter/net_http.rb#L88-L94

Doesn't really seem to handle this case properly. Any ideas?

(While code diving it seemed that the options parsing probably doesn't handle explicit nil settings well)

feature help wanted

Most helpful comment

@guille-moe the solution I'm planning to fix @jeffb-stell issue should allow you to also silence those warnings specifically.

All 5 comments

Hi @jeffb-stell,

my understanding is that you want to ignore the environment proxy value for Faraday.
I don't really understand why you would like to do that, but apparently that case was never considered until now.
As you already found out for yourself, the ProxyOptions gets a little crazy if you pass nil and that's why none of your solutions work.
I can try to fix this with a PR, but that will only be available in Faraday 0.14.0 or 0.14.1 when it will get releases, so you'll need to wait and update when the time comes (haps to review a PR if you want to contribute!).

In the meantime, a possible quick fix would be to launch your application with the ENV variable overridden. Just prepend env -u http_proxy to the command you use to launch it. For example:

# For the Ruby console
env -u http_proxy irb

# For Rails
env -u http_proxy rails

Hi @iMacTia,

Same problem, I don't really want to change / disable anything but with the warn on no_proxy env (https://github.com/lostisland/faraday/blob/4d1ddc130bd224adf25a19eaf0e81fd236a47285/lib/faraday/connection.rb#L460) when I use faraday (or a gem which is it) inside a docker container on macOS by default I get a no_proxy env: https://docs.docker.com/docker-for-mac/networking/ so many warn ....

What is the best solution for me or improvement for Faraday ? (Can propose a PR soon)

Could it be called something explicit like ENV['FARADAY_SILENCE_INTERNAL_WARNINGS']?

Or, @guille-moe is RUBYOPT=-W0 - silence warn throughout, not practical in your use-case?

@guille-moe the solution I'm planning to fix @jeffb-stell issue should allow you to also silence those warnings specifically.

@guille-moe @jeffb-stell
I've introduced a new setting in #754, you can now disable the env proxy detection like this:

Faraday.ignore_env_proxy = true # new setting, defaults to false
ENV['http_proxy'] = 'http://test.proxy.com:80'

conn = Faraday::Connection.new(....)
# conn will NOT use the env proxy

I haven't merged the PR yet, if you could spend some minutes to test it into your applications then that would make me more confident in merging it 😄
Also suggestions on additional tests are welcome 👍

Was this page helpful?
0 / 5 - 0 ratings