Living behind a proxy

2 views
Skip to first unread message

Herb

unread,
May 2, 2007, 6:31:22 PM5/2/07
to AsProjectUsers
Hi Folks!

I'm not quite sure if it fits into the users group, but I'll give it a
shot: I did a q'n'd hack adding basic proxy support to the
RemoteFileLoader task. Here's the diff:

lib/tasks/remote_file_loader.rb
6a7,15
>
> # Net::HTTP::Proxy returns a standard Net::HTTP Object
> # if host is nil so we can just go this way
> proxy_uri = URI.parse(ENV['HTTP_PROXY'].to_s)
> if proxy_uri.host
> print("Using proxy at #{proxy_uri.host}, port #{proxy_uri.port.to_s} (from HTTP_PROXY environment variable)\n")
> end
> proxy_user, proxy_pass = proxy_uri.userinfo.split(/:/) if proxy_uri.userinfo
> http_object = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_user, proxy_pass)
17c26
< response = Net::HTTP.get_response(uri.host, uri.path)
---
> response = http_object.get_response(uri.host, uri.path)


Hope this is as useful for some folks as it is for me ;) ...


Cheers

Herb

Luke Bayes

unread,
May 2, 2007, 6:44:03 PM5/2/07
to asproje...@googlegroups.com
Hey Herb,

Thanks so much for the contribution!

Should this code work across platforms? Actually more specifically, is that the environment variable name that is typically used across platforms?

Thanks,


Luke Bayes
AsProject



>         response = http_object.get_response( uri.host, uri.path)

Herb

unread,
May 2, 2007, 7:23:33 PM5/2/07
to AsProjectUsers
Yes, the ENV object is the standard way in ruby to access environment
variables across platforms.

Just did a quick check on the remote_fetcher.rb code from RubyGems
source itself:

# Returns an HTTP proxy URI if one is set in the environment
variables.
def get_proxy_from_env
env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
uri = env_proxy ? URI.parse(env_proxy) : nil
if uri and uri.user.nil? and uri.password.nil? then
# Probably we have http_proxy_* variables?
uri.user = escape(ENV['http_proxy_user'] ||
ENV['HTTP_PROXY_USER'])
uri.password = escape(ENV['http_proxy_pass'] ||
ENV['HTTP_PROXY_PASS'])
end
uri
end

As I mentioned my code is really qnd and looking at above method,
there's definately some more work to be done like escaping, explicitly
checking for nil objects and so on.

I thought about messing with the download progress issue anyway, so
maybe we can kill two birds with one stone on this one...


And thanks for providing asunit and asproject ;)


Cheers

Herb

Luke Bayes

unread,
May 2, 2007, 7:25:46 PM5/2/07
to asproje...@googlegroups.com
Hey Herb,

Thanks so much!

If you want to address the download output issue, that would be awesome!


Thanks,


Luke

On 5/2/07, Herb <hed...@googlemail.com> wrote:

Yes, the ENV object is the standard way in ruby to access environment
variables across platforms.

Just did a quick check on the remote_fetcher.rb code from RubyGems
source itself:

# Returns an HTTP proxy URI if one is set in the environment
variables.
  def get_proxy_from_env
    env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
    uri = env_proxy ? URI.parse(env_proxy) : nil
    if uri and uri.user.nil? and uri.password.nil ? then
Reply all
Reply to author
Forward
0 new messages