net-ping 1.3.7 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,17 @@
1
+ == 1.4.0 - 14-Feb-2011
2
+ * Added the redirect_limit accessor for the Net::Ping::HTTP class. This caps
3
+ the number of redirects that are allowed before a ping is considered a
4
+ failure. The default is 5.
5
+ * Changed the way in which redirects are detected. Previously I was checking
6
+ against the response class, but decided this was unreliable. I now check the
7
+ response code itself. Any code in the 300 range is considered a redirect.
8
+ * The default port for the Net::Ping::HTTP class is no longer explicitly set
9
+ to 80, but instead uses the result of the URI.parse(uri).port method by
10
+ default. Note that you can still override the port if you explicitly provide
11
+ one in the constructor.
12
+ * The Net::Ping::HTTP class now handles https URI's properly. Note that you may
13
+ get a warning about peer certificates.
14
+
1
15
  == 1.3.7 - 14-Oct-2010
2
16
  * Wrapped the ICMP ping in a while loop that breaks on success to prevent
3
17
  the ping from failing too early. Thanks go to Benny Holmgren for the
data/README CHANGED
@@ -2,11 +2,9 @@
2
2
  A collection of classes that provide different ways to ping computers.
3
3
 
4
4
  == Prerequisites
5
- * Ruby 1.8.0 or later
6
5
  * The win32-open3 (1.8.x) and windows-pr libraries are required on
7
6
  MS Windows when using the Net::Ping::External class unless you're
8
7
  using JRuby.
9
- * Windows 2000 or later is required to use the Ping::WMI class.
10
8
 
11
9
  == Installation
12
10
  gem install net-ping
@@ -36,8 +34,8 @@
36
34
 
37
35
  == Known Issues
38
36
  Older versions of Ruby 1.9.x may not work with UDP pings.
39
- As of JRuby 1.5.1, UDP pings will return false positives because of an
40
- incorrect error class being raised. See JRuby-4896.
37
+ Older versions of JRuby will return false positives in UDP pings
38
+ because of an incorrect error class being raised. See JRuby-4896.
41
39
 
42
40
  == License
43
41
  Artistic 2.0
data/Rakefile CHANGED
@@ -1,10 +1,13 @@
1
1
  require 'rake'
2
+ require 'rake/clean'
2
3
  require 'rake/testtask'
3
4
  include Config
4
5
 
6
+ CLEAN.include("**/*.gem", "**/*.rbc")
7
+
5
8
  namespace 'gem' do
6
9
  desc 'Create the net-ping gem'
7
- task :create do
10
+ task :create => [:clean] do
8
11
  spec = eval(IO.read('net-ping.gemspec'))
9
12
  Gem::Builder.new(spec).build
10
13
  end
data/lib/net/ping.rb CHANGED
@@ -10,7 +10,7 @@ require File.join(File.dirname(__FILE__), 'ping/icmp')
10
10
  require File.join(File.dirname(__FILE__), 'ping/external')
11
11
  require File.join(File.dirname(__FILE__), 'ping/http')
12
12
 
13
- if Config::CONFIG['host_os'] =~ /msdos|mswin|cygwin|mingw|win32/i &&
13
+ if Config::CONFIG['host_os'] =~ /msdos|mswin|cygwin|mingw|win32|windows/i &&
14
14
  RUBY_PLATFORM != 'java'
15
15
  then
16
16
  require File.join(File.dirname(__FILE__), 'ping/wmi')
@@ -1,7 +1,7 @@
1
1
  require 'rbconfig'
2
2
  require File.join(File.dirname(__FILE__), 'ping')
3
3
 
4
- if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i &&
4
+ if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw|windows/i &&
5
5
  RUBY_PLATFORM != 'java'
6
6
  then
7
7
  if RUBY_VERSION.to_f < 1.9
@@ -18,7 +18,7 @@ module Net
18
18
  # The Ping::External class encapsulates methods for external (system) pings.
19
19
  class Ping::External < Ping
20
20
 
21
- CWINDOWS = Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i &&
21
+ CWINDOWS = Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw|windows/i &&
22
22
  RUBY_PLATFORM != 'java'
23
23
 
24
24
  if CWINDOWS
data/lib/net/ping/http.rb CHANGED
@@ -1,34 +1,41 @@
1
1
  require File.join(File.dirname(__FILE__), 'ping')
2
2
  require 'net/http'
3
+ require 'net/https'
3
4
  require 'uri'
4
- require 'rbconfig'
5
5
 
6
6
  # Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
7
7
  # Windows because it (ironically) causes blocking problems.
8
- unless Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
8
+ unless File::ALT_SEPARATOR
9
9
  require 'resolv-replace'
10
10
  end
11
11
 
12
12
  # The Net module serves as a namespace only.
13
13
  module Net
14
-
14
+
15
15
  # The Ping::HTTP class encapsulates methods for HTTP pings.
16
16
  class Ping::HTTP < Ping
17
-
17
+
18
18
  # By default an http ping will follow a redirect and give you the result
19
19
  # of the final URI. If this value is set to false, then it will not
20
20
  # follow a redirect and will return false immediately on a redirect.
21
- #
21
+ #
22
22
  attr_accessor :follow_redirect
23
23
 
24
- # Sets the user agent used for the HTTP request to a custom one.
24
+ # The maximum number of redirects allowed. The default is 5.
25
+ attr_accessor :redirect_limit
26
+
27
+ # The user agent used for the HTTP request. The default is nil.
25
28
  attr_accessor :user_agent
26
29
 
27
- # Creates and returns a new Ping::HTTP object. Note that the default
28
- # port for Ping::HTTP is 80.
29
- #
30
- def initialize(uri=nil, port=80, timeout=5)
30
+ # Creates and returns a new Ping::HTTP object. The default port is the
31
+ # port associated with the URI. The default timeout is 5 seconds.
32
+ #
33
+ def initialize(uri=nil, port=nil, timeout=5)
31
34
  @follow_redirect = true
35
+ @redirect_limit = 5
36
+
37
+ port ||= URI.parse(uri).port if uri
38
+
32
39
  super(uri, port, timeout)
33
40
  end
34
41
 
@@ -37,7 +44,7 @@ module Net
37
44
  # successful and true is returned. Otherwise, false is returned
38
45
  # and the Ping::HTTP#exception method should contain a string
39
46
  # indicating what went wrong.
40
- #
47
+ #
41
48
  # If the HTTP#follow_redirect accessor is set to true (which it is
42
49
  # by default) and a redirect occurs during the ping, then the
43
50
  # HTTP#warning attribute is set to the redirect message, but the
@@ -45,7 +52,7 @@ module Net
45
52
  # response is considered a failed ping.
46
53
  #
47
54
  # If no file or path is specified in the URI, then '/' is assumed.
48
- #
55
+ #
49
56
  def ping(host = @host)
50
57
  super(host)
51
58
  bool = false
@@ -59,9 +66,10 @@ module Net
59
66
  headers = { }
60
67
  headers["User-Agent"] = user_agent unless user_agent.nil?
61
68
  Timeout.timeout(@timeout) do
62
- Net::HTTP.new(uri.host, @port).start do |http|
63
- response = http.request_get(uri_path, headers)
64
- end
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) }
65
73
  end
66
74
  rescue Exception => err
67
75
  @exception = err.message
@@ -71,18 +79,25 @@ module Net
71
79
  else
72
80
  if @follow_redirect
73
81
  @warning = response.message
74
-
75
- while response.is_a?(Net::HTTPRedirection)
76
- redirect = URI.parse(response['location'])
77
- redirect = uri + redirect if redirect.relative?
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?
78
92
  response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
93
+ rlimit += 1
79
94
  end
80
-
95
+
81
96
  if response.is_a?(Net::HTTPSuccess)
82
97
  bool = true
83
98
  else
84
99
  @warning = nil
85
- @exception = response.message
100
+ @exception ||= response.message
86
101
  end
87
102
  else
88
103
  @exception = response.message
data/lib/net/ping/ping.rb CHANGED
@@ -4,13 +4,13 @@ require 'timeout'
4
4
  # The Net module serves as a namespace only.
5
5
  #
6
6
  module Net
7
-
7
+
8
8
  # The Ping class serves as an abstract base class for all other Ping class
9
9
  # types. You should not instantiate this class directly.
10
10
  #
11
11
  class Ping
12
12
  # The version of the net-ping library.
13
- VERSION = '1.3.7'
13
+ VERSION = '1.4.0'
14
14
 
15
15
  # The host to ping. In the case of Ping::HTTP, this is the URI.
16
16
  attr_accessor :host
@@ -44,7 +44,7 @@ module Net
44
44
  # The default constructor for the Net::Ping class. Accepts an optional
45
45
  # +host+, +port+ and +timeout+. The port defaults to your echo port, or
46
46
  # 7 if that happens to be undefined. The default timeout is 5 seconds.
47
- #
47
+ #
48
48
  # The host, although optional in the constructor, must be specified at
49
49
  # some point before the Net::Ping#ping method is called, or else an
50
50
  # ArgumentError will be raised.
@@ -64,7 +64,7 @@ module Net
64
64
 
65
65
  yield self if block_given?
66
66
  end
67
-
67
+
68
68
  # The default interface for the Net::Ping#ping method. Each subclass
69
69
  # should call super() before continuing with their own implementation in
70
70
  # order to ensure that the @exception and @warning instance variables
@@ -81,7 +81,7 @@ module Net
81
81
  @exception = nil
82
82
  @warning = nil
83
83
  end
84
-
84
+
85
85
  alias ping? ping
86
86
  alias pingecho ping
87
87
  end
data/net-ping.gemspec CHANGED
@@ -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.3.7'
6
+ gem.version = '1.4.0'
7
7
  gem.license = 'Artistic 2.0'
8
8
  gem.author = 'Daniel J. Berger'
9
9
  gem.email = 'djberg96@gmail.com'
@@ -16,10 +16,10 @@ Gem::Specification.new do |gem|
16
16
  gem.rubyforge_project = 'shards'
17
17
  gem.extra_rdoc_files = ['README', 'CHANGES', 'doc/ping.txt']
18
18
 
19
- gem.add_development_dependency('test-unit', '>= 2.0.9')
19
+ gem.add_development_dependency('test-unit', '>= 2.1.2')
20
20
 
21
21
  # These dependencies are for Net::Ping::External
22
- if Config::CONFIG['host_os'] =~ /mswin|dos|win32|cygwin|mingw/i &&
22
+ if Config::CONFIG['host_os'] =~ /mswin|dos|win32|windows|cygwin|mingw/i &&
23
23
  RUBY_PLATFORM != 'java'
24
24
  then
25
25
  gem.platform = Gem::Platform::CURRENT
@@ -21,6 +21,6 @@ end
21
21
 
22
22
  class TC_Net_Ping < Test::Unit::TestCase
23
23
  def test_net_ping_version
24
- assert_equal('1.3.7', Net::Ping::VERSION)
24
+ assert_equal('1.4.0', Net::Ping::VERSION)
25
25
  end
26
26
  end
@@ -13,7 +13,7 @@ require 'net/ping/http'
13
13
  class TC_Net_Ping_HTTP < Test::Unit::TestCase
14
14
  def setup
15
15
  @uri = 'http://www.google.com/index.html'
16
- @http = Net::Ping::HTTP.new(@uri, 80, 30)
16
+ @http = Net::Ping::HTTP.new(@uri, 80, 30)
17
17
  @bad = Net::Ping::HTTP.new('http://www.blabfoobarurghxxxx.com') # One hopes not
18
18
  end
19
19
 
@@ -108,11 +108,58 @@ class TC_Net_Ping_HTTP < Test::Unit::TestCase
108
108
  assert_nil(@http.warning)
109
109
  end
110
110
 
111
+ test 'user_agent accessor is defined' do
112
+ assert_respond_to(@http, :user_agent)
113
+ assert_respond_to(@http, :user_agent=)
114
+ end
115
+
116
+ test 'user_agent defaults to nil' do
117
+ assert_nil(@http.user_agent)
118
+ end
119
+
111
120
  test 'ping with user agent' do
112
121
  @http.user_agent = "KDDI-CA32"
113
122
  assert_true(@http.ping)
114
123
  end
115
124
 
125
+ test 'redirect_limit accessor is defined' do
126
+ assert_respond_to(@http, :redirect_limit)
127
+ assert_respond_to(@http, :redirect_limit=)
128
+ end
129
+
130
+ test 'redirect_limit defaults to 5' do
131
+ assert_equal(5, @http.redirect_limit)
132
+ end
133
+
134
+ test 'redirects succeed by default' do
135
+ @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
136
+ assert_true(@http.ping)
137
+ end
138
+
139
+ test 'redirect fail if follow_redirect is set to false' do
140
+ @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
141
+ @http.follow_redirect = false
142
+ assert_false(@http.ping)
143
+ end
144
+
145
+ test 'ping with redirect limit set to zero fails' do
146
+ @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
147
+ @http.redirect_limit = 0
148
+ assert_false(@http.ping)
149
+ assert_equal("Redirect limit exceeded", @http.exception)
150
+ end
151
+
152
+ test 'ping against https site defaults to port 443' do
153
+ @http = Net::Ping::HTTP.new("https://encrypted.google.com/")
154
+ assert_equal(443, @http.port)
155
+ end
156
+
157
+ # This will generate a warning. Nothing I can do about it.
158
+ test 'ping against https site works as expected' do
159
+ @http = Net::Ping::HTTP.new("https://encrypted.google.com/")
160
+ assert_true(@http.ping)
161
+ end
162
+
116
163
  def teardown
117
164
  @uri = nil
118
165
  @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: 21
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 3
9
- - 7
10
- version: 1.3.7
8
+ - 4
9
+ - 0
10
+ version: 1.4.0
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: 2010-10-14 00:00:00 -06:00
18
+ date: 2011-02-15 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,12 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 29
29
+ hash: 15
30
30
  segments:
31
31
  - 2
32
- - 0
33
- - 9
34
- version: 2.0.9
32
+ - 1
33
+ - 2
34
+ version: 2.1.2
35
35
  type: :development
36
36
  version_requirements: *id001
37
37
  description: " The net-ping library provides a ping interface for Ruby. It includes\n separate TCP, HTTP, ICMP, UDP, WMI (for Windows) and external ping\n classes.\n"