Bug #4388 » open-uri_with_http_proxy.patch
lib/open-uri.rb | ||
---|---|---|
end
|
||
case opt_proxy
|
||
when true
|
||
find_proxy = lambda {|u| pxy = u.find_proxy; pxy ? [pxy, nil, nil] : nil}
|
||
find_proxy = lambda {|u|
|
||
if pxy = u.find_proxy
|
||
(proxy_user, proxy_pass) = pxy.userinfo.to_s.split(":")
|
||
[pxy, proxy_user, proxy_pass]
|
||
else
|
||
nil
|
||
end
|
||
}
|
||
when nil, false
|
||
find_proxy = lambda {|u| nil}
|
||
when String
|
||
... | ... | |
proxy_uri = ENV[name] || ENV[name.upcase]
|
||
end
|
||
if proxy_uri && self.hostname
|
||
require 'socket'
|
||
begin
|
||
addr = IPSocket.getaddress(self.hostname)
|
||
proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
|
||
rescue SocketError
|
||
end
|
||
end
|
||
if proxy_uri
|
||
proxy_uri = URI.parse(proxy_uri)
|
||
if self.hostname
|
||
require 'socket'
|
||
begin
|
||
addr = IPSocket.getaddress(self.hostname)
|
||
return nil if /\A127\.|\A::1\z/ =~ addr && proxy_uri.userinfo.nil?
|
||
rescue SocketError
|
||
end
|
||
end
|
||
name = 'no_proxy'
|
||
if no_proxy = ENV[name] || ENV[name.upcase]
|
||
no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
|
test/open-uri/test_open-uri.rb | ||
---|---|---|
}
|
||
end
|
||
def test_proxy_http_basic_authentication_from_env
|
||
with_http {|srv, dr, url|
|
||
log = ''
|
||
proxy_user = 'user'
|
||
proxy_pass = 'pass'
|
||
proxy = WEBrick::HTTPProxyServer.new({
|
||
:ServerType => Thread,
|
||
:Logger => WEBrick::Log.new(NullLog),
|
||
:AccessLog => [[NullLog, ""]],
|
||
:ProxyAuthProc => lambda {|req, res|
|
||
log << req.request_line
|
||
auth = ["#{proxy_user}:#{proxy_pass}"].pack('m').chomp
|
||
if req["Proxy-Authorization"] != "Basic #{auth}"
|
||
raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
|
||
end
|
||
},
|
||
:BindAddress => '127.0.0.1',
|
||
:Port => 0})
|
||
_, proxy_port, _, proxy_host = proxy.listeners[0].addr
|
||
proxy_url = "http://#{proxy_user}:#{proxy_pass}@#{proxy_host}:#{proxy_port}"
|
||
::ENV['http_proxy'] = proxy_url
|
||
begin
|
||
th = proxy.start
|
||
open("#{dr}/proxy", "w") {|f| f << "proxy" }
|
||
open("#{url}/proxy") {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
assert_match(/#{Regexp.quote url}/, log); log.clear
|
||
assert_equal("", log); log.clear
|
||
ensure
|
||
proxy.shutdown
|
||
end
|
||
}
|
||
end
|
||
def test_redirect
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
|