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
ass">
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",
roxy => "http://my.http-proxy.tld",
"Authorization" => "Basic " << Base64.encode64("user
ass")) 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
Applying these changes to the open-uri.rb file, you can now do this:
*****
require 'open-uri'
open("http://some.web-site.tld",
roxy => "http://my.http-proxy.tld",
roxy_user => "user",
roxy_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
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
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",
"Authorization" => "Basic " << Base64.encode64("user
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
roxy_user => true,
roxy_pass => true 101a104
new_options = {} 106a110,121
if ((k ==roxy_user) && !options.keys().include?("Authorization")) then
unless options.keys().member?proxy_pass) then
raise ArgumentError, "roxy_pass needs to be defined"
end
new_options = OpenURI.basic_auth(options[roxy_user],options[
roxy_pass])
end
if ((k ==roxy_pass) && !options.keys().include?("Authorization")) then
unless options.keys().member?proxy_user) then
raise ArgumentError, "roxy_user needs to be defined"
end
new_options = OpenURI.basic_auth(options[roxy_user],options[
roxy_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",
puts page.read
end
*****
I hope this helps those of you out there who need to get through an
authenticating proxy.
--Erik