Proxy Authentication in open-uri

E

Erik Wessel

This is the first time I've posted, so bear with me if I defy any
conventions.

A problem I've run into a couple times while writing programs for
myself at work (and using Ruby Gems, as well) has been that our company
uses an authenticating proxy. There's apparently no way to cleanly
write something that authenticates to a proxy with open-uri. I found a
post on this group by Tanaka Akira saying that open-uri doesn't have
that functionality because he's not sure how to encode the user/pass
correctly. There's a quick work-around that I've discovered, having
this problem with a lot of programming I've done here at work.

The encoding is a simple base64 encoding, with the right syntax in a
header on the request:

Authorization: Basic <base64 encoded string "name:pass">

This is for basic HTTP proxies. I have no idea how it works on other
proxies, like SOCKS.

So, to get open-uri to work you could do something ugly like:

*****
require 'open-uri'
require 'base64'

open("http://some.web-site.tld",
:proxy => "http://my.http-proxy.tld",
"Authorization" => "Basic " << Base64.encode64("user:pass")) do |page|
puts page.read
end
*****

However I've hacked at open-uri.rb a little (and I mean *hacked*, with
an axe), and come up with this:

diff 1.8/open-uri.rb site_ruby/1.8/open-uri.rb
97a98,99
:proxy_user => true,
:proxy_pass => true 101a104
new_options = {} 106a110,121
if ((k == :proxy_user) && !options.keys().include?("Authorization")) then
unless options.keys().member?:)proxy_pass) then
raise ArgumentError, ":proxy_pass needs to be defined"
end
new_options = OpenURI.basic_auth(options[:proxy_user],options[:proxy_pass])
end
if ((k == :proxy_pass) && !options.keys().include?("Authorization")) then
unless options.keys().member?:)proxy_user) then
raise ArgumentError, ":proxy_user needs to be defined"
end
new_options = OpenURI.basic_auth(options[:proxy_user],options[:proxy_pass])
end 107a123,128
options.update(new_options)
end

def OpenURI.basic_auth(username,password)
require 'base64'
{"Authorization" => "Basic " << Base64.encode64("#{username}:#{password}")}

Applying these changes to the open-uri.rb file, you can now do this:


*****
require 'open-uri'

open("http://some.web-site.tld",
:proxy => "http://my.http-proxy.tld",
:proxy_user => "user",
:proxy_pass => "pass") do |page|
puts page.read
end
*****

I hope this helps those of you out there who need to get through an
authenticating proxy.

--Erik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top