Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #15 from 6fusion/master

add support for proxies to Net::Ping::HTTP
  • Loading branch information...
commit 54d8fb41d79488e554b702522c50427abb2f72aa 2 parents ef58c3a + f020c68
@djberg96 authored
View
1  .gitignore
@@ -0,0 +1 @@
+Gemfile.lock
View
2  Gemfile
@@ -0,0 +1,2 @@
+source :rubygems
+gemspec
View
33 lib/net/ping/http.rb
@@ -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.
@@ -33,6 +34,9 @@ class Ping::HTTP < Ping
# Use GET request instead HEAD. The default is false.
attr_accessor :get_request
+ # was this ping proxied?
+ attr_accessor :proxied
+
# Creates and returns a new Ping::HTTP object. The default port is the
# port associated with the URI. The default timeout is 5 seconds.
#
@@ -40,7 +44,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 +68,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 +79,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 +89,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 +124,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)
+ @proxied = http.proxy?
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
2  lib/net/ping/ping.rb
@@ -10,7 +10,7 @@ module Net
#
class Ping
# The version of the net-ping library.
- VERSION = '1.5.2'
+ VERSION = '1.5.3'
# The host to ping. In the case of Ping::HTTP, this is the URI.
attr_accessor :host
View
30 test/test_net_ping_http.rb
@@ -10,17 +10,23 @@
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(: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 +179,30 @@ 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)
+ assert_true(@http.proxied)
+ 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)
+ assert_true(@http.proxied)
+ 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)
+ assert_false(@http.proxied)
+ end
+
def teardown
@uri = nil
@http = nil
Please sign in to comment.
Something went wrong with that request. Please try again.