Permalink
Browse files

add support for proxies to Net::Ping::HTTP

It now inspects the http_proxy, https_proxy and no_proxy environment variables and behaves accordingly.
  • Loading branch information...
1 parent ef4c94d commit b471ededb3e7ba0b12c6c846cd99bbf4239855b6 Kevin Olbrich committed Jan 24, 2013
Showing with 44 additions and 13 deletions.
  1. +15 −13 lib/net/ping/http.rb
  2. +29 −0 test/test_net_ping_http.rb
View
@@ -2,6 +2,7 @@
require 'net/http'
require 'net/https'
require 'uri'
+require 'open-uri'
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
# Windows because it (ironically) causes blocking problems.
@@ -40,7 +41,7 @@ def initialize(uri=nil, port=nil, timeout=5)
@follow_redirect = true
@redirect_limit = 5
@ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
- @get_request = false
+ @get_request = false
port ||= URI.parse(uri).port if uri
@@ -64,7 +65,7 @@ def initialize(uri=nil, port=nil, timeout=5)
def ping(host = @host)
super(host)
bool = false
- uri = URI.parse(host)
+ uri = URI.parse(host)
start_time = Time.now
@@ -75,7 +76,7 @@ def ping(host = @host)
elsif redirect?(response) # Check code, HTTPRedirection does not always work
if @follow_redirect
@warning = response.message
- rlimit = 0
+ rlimit = 0
while redirect?(response)
if rlimit >= redirect_limit
@@ -85,13 +86,13 @@ def ping(host = @host)
redirect = URI.parse(response['location'])
redirect = uri + redirect if redirect.relative?
response = do_ping(redirect)
- rlimit += 1
+ rlimit += 1
end
if response.is_a?(Net::HTTPSuccess)
bool = true
else
- @warning = nil
+ @warning = nil
@exception ||= response.message
end
@@ -120,25 +121,26 @@ def redirect?(response)
def do_ping(uri)
response = nil
+ proxy = uri.find_proxy || URI.parse("")
begin
uri_path = uri.path.empty? ? '/' : uri.path
- headers = { }
+ headers = { }
headers["User-Agent"] = user_agent unless user_agent.nil?
Timeout.timeout(@timeout) do
- http = Net::HTTP.new(uri.host, uri.port)
-
- if uri.scheme == 'https'
- http.use_ssl = true
- http.verify_mode = @ssl_verify_mode
- end
+ http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, uri.port)
if @get_request == true
request = Net::HTTP::Get.new(uri_path)
else
request = Net::HTTP::Head.new(uri_path)
end
- response = http.start{ |h| h.request(request) }
+ if uri.scheme == 'https'
+ http.use_ssl = true
+ http.verify_mode = @ssl_verify_mode
+ end
+
+ response = http.start { |h| h.request(request) }
end
rescue Exception => err
@exception = err.message
View
@@ -10,17 +10,25 @@
class TC_Net_Ping_HTTP < Test::Unit::TestCase
def setup
+ ENV['http_proxy'] = ENV['https_proxy'] = ENV['no_proxy'] = nil
@uri = 'http://www.google.com/index.html'
@uri_https = 'https://encrypted.google.com'
+ @proxy = 'http://username:password@proxymoxie:3128'
+ FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:get, @uri, :body => "PONG")
FakeWeb.register_uri(:head, @uri, :body => "PONG")
FakeWeb.register_uri(:head, @uri_https, :body => "PONG")
+ FakeWeb.register_uri(:get, @uri_https, :body => "PONG")
FakeWeb.register_uri(:head, "http://jigsaw.w3.org/HTTP/300/302.html",
:body => "PONG",
:location => "#{@uri}",
:status => ["302", "Found"])
+ #FakeWeb.register_uri(:get, @proxy, :body => 'proxied!')
+ #FakeWeb.register_uri(:head, @proxy, :body => 'proxied!')
+ FakeWeb.register_uri(:any, 'http://www.blabfoobarurghxxxx.com', :exception => SocketError)
+
@http = Net::Ping::HTTP.new(@uri, 80, 30)
@bad = Net::Ping::HTTP.new('http://www.blabfoobarurghxxxx.com') # One hopes not
end
@@ -173,6 +181,27 @@ def setup
assert_true(@http.ping)
end
+ test 'ping with http proxy' do
+ ENV['http_proxy'] = "http://proxymoxie:3128"
+ @http = Net::Ping::HTTP.new(@uri)
+ @http.get_request = true
+ assert_true(@http.ping)
+ end
+
+ test 'ping with https proxy' do
+ ENV['https_proxy'] = "http://proxymoxie:3128"
+ @http = Net::Ping::HTTP.new(@uri_https)
+ @http.get_request = true
+ assert_true(@http.ping)
+ end
+
+ test 'ping with no_proxy' do
+ ENV['no_proxy'] = "google.com"
+ @http = Net::Ping::HTTP.new(@uri)
+ @http.get_request = true
+ assert_true(@http.ping)
+ end
+
def teardown
@uri = nil
@http = nil

1 comment on commit b471ede

FYI, it appears that proxy information is now used by default for Net::HTTP::New in Ruby 2.x.

Please sign in to comment.