net-ping 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,12 @@
1
+ == 1.5.2 - 4-Nov-2011
2
+ * The Net::Ping::HTTP class now defaults to using HEAD instead of GET
3
+ for its ping test. You can alter this via the get_request accessor.
4
+ Thanks got to Balazs Kutil for the patch.
5
+ * Added the ssl_verify_mode accessor to the Net::Ping::HTTP class, which
6
+ defaults to VERIFY_NONE. This fixes possible warnings resulting from
7
+ missing SSL certificates when using https. Thanks go to Balazs Kutil
8
+ for the patch.
9
+
1
10
  == 1.5.1 - 15-Sep-2011
2
11
  * Use RbConfig on Ruby 1.9 and later. Thanks go to Torsten Schönebaum
3
12
  for the patch.
@@ -27,12 +27,20 @@ module Net
27
27
  # The user agent used for the HTTP request. The default is nil.
28
28
  attr_accessor :user_agent
29
29
 
30
+ # OpenSSL certificate verification mode. The default is VERIFY_NONE.
31
+ attr_accessor :ssl_verify_mode
32
+
33
+ # Use GET request instead HEAD. The default is false.
34
+ attr_accessor :get_request
35
+
30
36
  # Creates and returns a new Ping::HTTP object. The default port is the
31
37
  # port associated with the URI. The default timeout is 5 seconds.
32
38
  #
33
39
  def initialize(uri=nil, port=nil, timeout=5)
34
40
  @follow_redirect = true
35
41
  @redirect_limit = 5
42
+ @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+ @get_request = false
36
44
 
37
45
  port ||= URI.parse(uri).port if uri
38
46
 
@@ -60,48 +68,35 @@ module Net
60
68
 
61
69
  start_time = Time.now
62
70
 
63
- begin
64
- response = nil
65
- uri_path = uri.path.empty? ? '/' : uri.path
66
- headers = { }
67
- headers["User-Agent"] = user_agent unless user_agent.nil?
68
- Timeout.timeout(@timeout) do
69
- http = Net::HTTP.new(uri.host, uri.port)
70
- http.use_ssl = (uri.scheme == 'https')
71
- request = Net::HTTP::Get.new(uri_path)
72
- response = http.start{ |h| h.request(request) }
73
- end
74
- rescue Exception => err
75
- @exception = err.message
76
- else
77
- if response.is_a?(Net::HTTPSuccess)
78
- bool = true
79
- else
80
- if @follow_redirect
81
- @warning = response.message
82
- rlimit = 0
83
-
84
- # Any response code in the 300 range is a redirect
85
- while response.code.to_i >= 300 && response.code.to_i < 400
86
- if rlimit >= redirect_limit
87
- @exception = "Redirect limit exceeded"
88
- break
89
- end
90
- redirect = URI.parse(response['location'])
91
- redirect = uri + redirect if redirect.relative?
92
- response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
93
- rlimit += 1
94
- end
71
+ response = do_ping(uri)
95
72
 
96
- if response.is_a?(Net::HTTPSuccess)
97
- bool = true
98
- else
99
- @warning = nil
100
- @exception ||= response.message
73
+ if response.is_a?(Net::HTTPSuccess)
74
+ bool = true
75
+ elsif redirect?(response) # Check code, HTTPRedirection does not always work
76
+ if @follow_redirect
77
+ @warning = response.message
78
+ rlimit = 0
79
+
80
+ while redirect?(response)
81
+ if rlimit >= redirect_limit
82
+ @exception = "Redirect limit exceeded"
83
+ break
101
84
  end
85
+ redirect = URI.parse(response['location'])
86
+ redirect = uri + redirect if redirect.relative?
87
+ response = do_ping(redirect)
88
+ rlimit += 1
89
+ end
90
+
91
+ if response.is_a?(Net::HTTPSuccess)
92
+ bool = true
102
93
  else
103
- @exception = response.message
94
+ @warning = nil
95
+ @exception ||= response.message
104
96
  end
97
+
98
+ else
99
+ @exception = response.message
105
100
  end
106
101
  end
107
102
 
@@ -116,5 +111,39 @@ module Net
116
111
  alias follow_redirect? follow_redirect
117
112
  alias uri host
118
113
  alias uri= host=
114
+
115
+ private
116
+
117
+ def redirect?(response)
118
+ response && response.code.to_i >= 300 && response.code.to_i < 400
119
+ end
120
+
121
+ def do_ping(uri)
122
+ response = nil
123
+ begin
124
+ uri_path = uri.path.empty? ? '/' : uri.path
125
+ headers = { }
126
+ headers["User-Agent"] = user_agent unless user_agent.nil?
127
+ Timeout.timeout(@timeout) do
128
+ http = Net::HTTP.new(uri.host, uri.port)
129
+
130
+ if uri.scheme == 'https'
131
+ http.use_ssl = true
132
+ http.verify_mode = @ssl_verify_mode
133
+ end
134
+
135
+ if @get_request == true
136
+ request = Net::HTTP::Get.new(uri_path)
137
+ else
138
+ request = Net::HTTP::Head.new(uri_path)
139
+ end
140
+
141
+ response = http.start{ |h| h.request(request) }
142
+ end
143
+ rescue Exception => err
144
+ @exception = err.message
145
+ end
146
+ response
147
+ end
119
148
  end
120
149
  end
@@ -10,7 +10,7 @@ module Net
10
10
  #
11
11
  class Ping
12
12
  # The version of the net-ping library.
13
- VERSION = '1.5.1'
13
+ VERSION = '1.5.2'
14
14
 
15
15
  # The host to ping. In the case of Ping::HTTP, this is the URI.
16
16
  attr_accessor :host
@@ -3,7 +3,7 @@ require 'rbconfig'
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = 'net-ping'
6
- gem.version = '1.5.1'
6
+ gem.version = '1.5.2'
7
7
  gem.license = 'Artistic 2.0'
8
8
  gem.author = 'Daniel J. Berger'
9
9
  gem.email = 'djberg96@gmail.com'
@@ -30,7 +30,7 @@ end
30
30
 
31
31
  class TC_Net_Ping < Test::Unit::TestCase
32
32
  def test_net_ping_version
33
- assert_equal('1.5.1', Net::Ping::VERSION)
33
+ assert_equal('1.5.2', Net::Ping::VERSION)
34
34
  end
35
35
  end
36
36
 
@@ -16,8 +16,9 @@ class TC_Net_Ping_HTTP < Test::Unit::TestCase
16
16
  @uri_https = 'https://encrypted.google.com'
17
17
 
18
18
  FakeWeb.register_uri(:get, @uri, :body => "PONG")
19
- FakeWeb.register_uri(:get, @uri_https, :body => "PONG")
20
- FakeWeb.register_uri(:get, "http://jigsaw.w3.org/HTTP/300/302.html",
19
+ FakeWeb.register_uri(:head, @uri, :body => "PONG")
20
+ FakeWeb.register_uri(:head, @uri_https, :body => "PONG")
21
+ FakeWeb.register_uri(:head, "http://jigsaw.w3.org/HTTP/300/302.html",
21
22
  :body => "PONG",
22
23
  :location => "#{@uri}",
23
24
  :status => ["302", "Found"])
@@ -163,12 +164,17 @@ class TC_Net_Ping_HTTP < Test::Unit::TestCase
163
164
  assert_equal(443, @http.port)
164
165
  end
165
166
 
166
- # This will generate a warning. Nothing I can do about it.
167
167
  test 'ping against https site works as expected' do
168
168
  @http = Net::Ping::HTTP.new(@uri_https)
169
169
  assert_true(@http.ping)
170
170
  end
171
171
 
172
+ test 'ping with get option' do
173
+ @http = Net::Ping::HTTP.new(@uri)
174
+ @http.get_request = true
175
+ assert_true(@http.ping)
176
+ end
177
+
172
178
  def teardown
173
179
  @uri = nil
174
180
  @http = nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-ping
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 5
9
- - 1
10
- version: 1.5.1
9
+ - 2
10
+ version: 1.5.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel J. Berger
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-15 00:00:00 Z
18
+ date: 2011-11-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: net-ldap