Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Merged
merged 7 commits into from

3 participants

@olbrich

It now gets proxy information from the http_proxy, https_proxy, and no_proxy environment variables and uses that when making requests. Fixes #14

Kevin Olbrich added some commits
Kevin Olbrich add Gemfile for bundler 3df0f66
Kevin Olbrich fix broken test ef4c94d
Kevin Olbrich add support for proxies to Net::Ping::HTTP
It now inspects the http_proxy, https_proxy and no_proxy environment variables and behaves accordingly.
b471ede
Kevin Olbrich use gemspec for bundler b3d60dc
Kevin Olbrich one more tweak to Gemfile 13812aa
Kevin Olbrich remove comments from tests 9bacbc2
@lzap
Collaborator

Will the test work also without internet connection? I guess so. Please confirm.

@olbrich

The new tests pass without an internet connection. They pretty much use FakeWeb for everything.

@djberg96 djberg96 merged commit 54d8fb4 into djberg96:master
@djberg96
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 24, 2013
  1. add Gemfile for bundler

    Kevin Olbrich authored
  2. fix broken test

    Kevin Olbrich authored
  3. add support for proxies to Net::Ping::HTTP

    Kevin Olbrich authored
    It now inspects the http_proxy, https_proxy and no_proxy environment variables and behaves accordingly.
  4. use gemspec for bundler

    Kevin Olbrich authored
  5. one more tweak to Gemfile

    Kevin Olbrich authored
  6. remove comments from tests

    Kevin Olbrich authored
Commits on Jan 26, 2013
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.