net-ping 1.7.2-universal-mingw32 → 1.7.3-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/net/ping.rb CHANGED
@@ -1,17 +1,17 @@
1
- # By doing a "require 'net/ping'" you are requiring every subclass. If you
2
- # want to require a specific ping type only, do "require 'net/ping/tcp'",
3
- # for example.
4
- #
5
- require 'rbconfig'
6
-
7
- require File.join(File.dirname(__FILE__), 'ping/tcp')
8
- require File.join(File.dirname(__FILE__), 'ping/udp')
9
- require File.join(File.dirname(__FILE__), 'ping/icmp')
10
- require File.join(File.dirname(__FILE__), 'ping/external')
11
- require File.join(File.dirname(__FILE__), 'ping/http')
12
-
13
- RbConfig = Config unless Object.const_defined?(:RbConfig)
14
-
15
- if File::ALT_SEPARATOR
16
- require File.join(File.dirname(__FILE__), 'ping/wmi')
17
- end
1
+ # By doing a "require 'net/ping'" you are requiring every subclass. If you
2
+ # want to require a specific ping type only, do "require 'net/ping/tcp'",
3
+ # for example.
4
+ #
5
+ require 'rbconfig'
6
+
7
+ require File.join(File.dirname(__FILE__), 'ping/tcp')
8
+ require File.join(File.dirname(__FILE__), 'ping/udp')
9
+ require File.join(File.dirname(__FILE__), 'ping/icmp')
10
+ require File.join(File.dirname(__FILE__), 'ping/external')
11
+ require File.join(File.dirname(__FILE__), 'ping/http')
12
+
13
+ RbConfig = Config unless Object.const_defined?(:RbConfig)
14
+
15
+ if File::ALT_SEPARATOR
16
+ require File.join(File.dirname(__FILE__), 'ping/wmi')
17
+ end
@@ -1,74 +1,86 @@
1
- require 'open3'
2
- require 'rbconfig'
3
-
4
- require File.join(File.dirname(__FILE__), 'ping')
5
-
6
- # The Net module serves as a namespace only.
7
- module Net
8
-
9
- # The Ping::External class encapsulates methods for external (system) pings.
10
- class Ping::External < Ping
11
- # Pings the host using your system's ping utility and checks for any
12
- # errors or warnings. Returns true if successful, or false if not.
13
- #
14
- # If the ping failed then the Ping::External#exception method should
15
- # contain a string indicating what went wrong. If the ping succeeded then
16
- # the Ping::External#warning method may or may not contain a value.
17
- #
18
- def ping(host = @host)
19
- super(host)
20
-
21
- pcmd = ['ping']
22
- bool = false
23
-
24
- case RbConfig::CONFIG['host_os']
25
- when /linux|bsd|osx|mach|darwin/i
26
- pcmd += ['-c1', host]
27
- when /solaris|sunos/i
28
- pcmd += [host, '1']
29
- when /hpux/i
30
- pcmd += [host, '-n1']
31
- when /win32|windows|msdos|mswin|cygwin|mingw/i
32
- pcmd += ['-n', '1', host]
33
- else
34
- pcmd += [host]
35
- end
36
-
37
- start_time = Time.now
38
-
39
- begin
40
- err = nil
41
-
42
- Timeout.timeout(@timeout){
43
- Open3.popen3(*pcmd) do |stdin, stdout, stderr, thread|
44
- err = stderr.gets # Can't chomp yet, might be nil
45
-
46
- case thread.value.exitstatus
47
- when 0
48
- bool = true # Success, at least one response.
49
- if err & err =~ /warning/i
50
- @warning = err.chomp
51
- end
52
- when 2
53
- bool = false # Transmission successful, no response.
54
- @exception = err.chomp
55
- else
56
- bool = false # An error occurred
57
- @exception = err.chomp
58
- end
59
- end
60
- }
61
- rescue Exception => error
62
- @exception = error.message
63
- end
64
-
65
- # There is no duration if the ping failed
66
- @duration = Time.now - start_time if bool
67
-
68
- bool
69
- end
70
-
71
- alias ping? ping
72
- alias pingecho ping
73
- end
74
- end
1
+ require 'open3'
2
+ require 'rbconfig'
3
+
4
+ require File.join(File.dirname(__FILE__), 'ping')
5
+
6
+ # The Net module serves as a namespace only.
7
+ module Net
8
+
9
+ # The Ping::External class encapsulates methods for external (system) pings.
10
+ class Ping::External < Ping
11
+ # Pings the host using your system's ping utility and checks for any
12
+ # errors or warnings. Returns true if successful, or false if not.
13
+ #
14
+ # If the ping failed then the Ping::External#exception method should
15
+ # contain a string indicating what went wrong. If the ping succeeded then
16
+ # the Ping::External#warning method may or may not contain a value.
17
+ #
18
+ def ping(host = @host)
19
+ super(host)
20
+
21
+ pcmd = ['ping']
22
+ bool = false
23
+
24
+ case RbConfig::CONFIG['host_os']
25
+ when /linux/i
26
+ pcmd += ['-c', '1', '-W', @timeout.to_s, host]
27
+ when /aix/i
28
+ pcmd += ['-c', '1', '-w', @timeout.to_s, host]
29
+ when /bsd|osx|mach|darwin/i
30
+ pcmd += ['-c', '1', '-t', @timeout.to_s, host]
31
+ when /solaris|sunos/i
32
+ pcmd += [host, @timeout.to_s]
33
+ when /hpux/i
34
+ pcmd += [host, '-n1', '-m', @timeout.to_s]
35
+ when /win32|windows|msdos|mswin|cygwin|mingw/i
36
+ pcmd += ['-n', '1', '-w', (@timeout * 1000).to_s, host]
37
+ else
38
+ pcmd += [host]
39
+ end
40
+
41
+ start_time = Time.now
42
+
43
+ begin
44
+ err = nil
45
+
46
+ Open3.popen3(*pcmd) do |stdin, stdout, stderr, thread|
47
+ stdin.close
48
+ err = stderr.gets # Can't chomp yet, might be nil
49
+
50
+ case thread.value.exitstatus
51
+ when 0
52
+ bool = true # Success, at least one response.
53
+ if err & err =~ /warning/i
54
+ @warning = err.chomp
55
+ end
56
+ when 2
57
+ bool = false # Transmission successful, no response.
58
+ @exception = err.chomp if err
59
+ else
60
+ bool = false # An error occurred
61
+ if err
62
+ @exception = err.chomp
63
+ else
64
+ stdout.each_line do |line|
65
+ if line =~ /(timed out|could not find host|packet loss)/i
66
+ @exception = line.chomp
67
+ break
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ rescue Exception => error
74
+ @exception = error.message
75
+ end
76
+
77
+ # There is no duration if the ping failed
78
+ @duration = Time.now - start_time if bool
79
+
80
+ bool
81
+ end
82
+
83
+ alias ping? ping
84
+ alias pingecho ping
85
+ end
86
+ end
data/lib/net/ping/http.rb CHANGED
@@ -1,171 +1,171 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
- require 'net/http'
3
- require 'net/https'
4
- require 'uri'
5
- require 'open-uri'
6
-
7
- # Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
8
- # Windows because it (ironically) causes blocking problems.
9
- unless File::ALT_SEPARATOR or RUBY_VERSION >= "1.9.3"
10
- require 'resolv-replace'
11
- end
12
-
13
- # The Net module serves as a namespace only.
14
- module Net
15
-
16
- # The Ping::HTTP class encapsulates methods for HTTP pings.
17
- class Ping::HTTP < Ping
18
-
19
- # By default an http ping will follow a redirect and give you the result
20
- # of the final URI. If this value is set to false, then it will not
21
- # follow a redirect and will return false immediately on a redirect.
22
- #
23
- attr_accessor :follow_redirect
24
-
25
- # The maximum number of redirects allowed. The default is 5.
26
- attr_accessor :redirect_limit
27
-
28
- # The user agent used for the HTTP request. The default is nil.
29
- attr_accessor :user_agent
30
-
31
- # OpenSSL certificate verification mode. The default is VERIFY_NONE.
32
- attr_accessor :ssl_verify_mode
33
-
34
- # Use GET request instead HEAD. The default is false.
35
- attr_accessor :get_request
36
-
37
- # was this ping proxied?
38
- attr_accessor :proxied
39
-
40
- # For unsuccessful requests that return a server error, it is
41
- # useful to know the HTTP status code of the response.
42
- attr_reader :code
43
-
44
- # Creates and returns a new Ping::HTTP object. The default port is the
45
- # port associated with the URI. The default timeout is 5 seconds.
46
- #
47
- def initialize(uri=nil, port=nil, timeout=5)
48
- @follow_redirect = true
49
- @redirect_limit = 5
50
- @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
51
- @get_request = false
52
- @code = nil
53
-
54
- port ||= URI.parse(uri).port if uri
55
-
56
- super(uri, port, timeout)
57
- end
58
-
59
- # Looks for an HTTP response from the URI passed to the constructor.
60
- # If the result is a kind of Net::HTTPSuccess then the ping was
61
- # successful and true is returned. Otherwise, false is returned
62
- # and the Ping::HTTP#exception method should contain a string
63
- # indicating what went wrong.
64
- #
65
- # If the HTTP#follow_redirect accessor is set to true (which it is
66
- # by default) and a redirect occurs during the ping, then the
67
- # HTTP#warning attribute is set to the redirect message, but the
68
- # return result is still true. If it's set to false then a redirect
69
- # response is considered a failed ping.
70
- #
71
- # If no file or path is specified in the URI, then '/' is assumed.
72
- # If no scheme is present in the URI, then 'http' is assumed.
73
- #
74
- def ping(host = @host)
75
- super(host)
76
- bool = false
77
-
78
- # See https://bugs.ruby-lang.org/issues/8645
79
- host = "http://#{host}" unless host.include?("http")
80
-
81
- uri = URI.parse(host)
82
-
83
- # A port provided here overrides anything provided in constructor
84
- port = URI.split(host)[3] || @port
85
- port = port.to_i
86
-
87
- start_time = Time.now
88
-
89
- response = do_ping(uri, port)
90
-
91
- if response.is_a?(Net::HTTPSuccess)
92
- bool = true
93
- elsif redirect?(response) # Check code, HTTPRedirection does not always work
94
- if @follow_redirect
95
- @warning = response.message
96
- rlimit = 0
97
-
98
- while redirect?(response)
99
- if rlimit >= redirect_limit
100
- @exception = "Redirect limit exceeded"
101
- break
102
- end
103
- redirect = URI.parse(response['location'])
104
- redirect = uri + redirect if redirect.relative?
105
- response = do_ping(redirect, port)
106
- rlimit += 1
107
- end
108
-
109
- if response.is_a?(Net::HTTPSuccess)
110
- bool = true
111
- else
112
- @warning = nil
113
- @exception ||= response.message
114
- end
115
-
116
- else
117
- @exception = response.message
118
- end
119
- else
120
- @exception ||= response.message
121
- end
122
-
123
- # There is no duration if the ping failed
124
- @duration = Time.now - start_time if bool
125
-
126
- bool
127
- end
128
-
129
- alias ping? ping
130
- alias pingecho ping
131
- alias follow_redirect? follow_redirect
132
- alias uri host
133
- alias uri= host=
134
-
135
- private
136
-
137
- def redirect?(response)
138
- response && response.code.to_i >= 300 && response.code.to_i < 400
139
- end
140
-
141
- def do_ping(uri, port)
142
- response = nil
143
- proxy = uri.find_proxy || URI.parse("")
144
- begin
145
- uri_path = uri.path.empty? ? '/' : uri.path
146
- headers = { }
147
- headers["User-Agent"] = user_agent unless user_agent.nil?
148
- Timeout.timeout(@timeout) do
149
- http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, port)
150
- @proxied = http.proxy?
151
- if @get_request == true
152
- request = Net::HTTP::Get.new(uri_path)
153
- else
154
- request = Net::HTTP::Head.new(uri_path)
155
- end
156
-
157
- if uri.scheme == 'https'
158
- http.use_ssl = true
159
- http.verify_mode = @ssl_verify_mode
160
- end
161
-
162
- response = http.start { |h| h.request(request) }
163
- end
164
- rescue Exception => err
165
- @exception = err.message
166
- end
167
- @code = response.code if response
168
- response
169
- end
170
- end
171
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'open-uri'
6
+
7
+ # Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
8
+ # Windows because it (ironically) causes blocking problems.
9
+ unless File::ALT_SEPARATOR or RUBY_VERSION >= "1.9.3"
10
+ require 'resolv-replace'
11
+ end
12
+
13
+ # The Net module serves as a namespace only.
14
+ module Net
15
+
16
+ # The Ping::HTTP class encapsulates methods for HTTP pings.
17
+ class Ping::HTTP < Ping
18
+
19
+ # By default an http ping will follow a redirect and give you the result
20
+ # of the final URI. If this value is set to false, then it will not
21
+ # follow a redirect and will return false immediately on a redirect.
22
+ #
23
+ attr_accessor :follow_redirect
24
+
25
+ # The maximum number of redirects allowed. The default is 5.
26
+ attr_accessor :redirect_limit
27
+
28
+ # The user agent used for the HTTP request. The default is nil.
29
+ attr_accessor :user_agent
30
+
31
+ # OpenSSL certificate verification mode. The default is VERIFY_NONE.
32
+ attr_accessor :ssl_verify_mode
33
+
34
+ # Use GET request instead HEAD. The default is false.
35
+ attr_accessor :get_request
36
+
37
+ # was this ping proxied?
38
+ attr_accessor :proxied
39
+
40
+ # For unsuccessful requests that return a server error, it is
41
+ # useful to know the HTTP status code of the response.
42
+ attr_reader :code
43
+
44
+ # Creates and returns a new Ping::HTTP object. The default port is the
45
+ # port associated with the URI. The default timeout is 5 seconds.
46
+ #
47
+ def initialize(uri=nil, port=nil, timeout=5)
48
+ @follow_redirect = true
49
+ @redirect_limit = 5
50
+ @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
51
+ @get_request = false
52
+ @code = nil
53
+
54
+ port ||= URI.parse(uri).port if uri
55
+
56
+ super(uri, port, timeout)
57
+ end
58
+
59
+ # Looks for an HTTP response from the URI passed to the constructor.
60
+ # If the result is a kind of Net::HTTPSuccess then the ping was
61
+ # successful and true is returned. Otherwise, false is returned
62
+ # and the Ping::HTTP#exception method should contain a string
63
+ # indicating what went wrong.
64
+ #
65
+ # If the HTTP#follow_redirect accessor is set to true (which it is
66
+ # by default) and a redirect occurs during the ping, then the
67
+ # HTTP#warning attribute is set to the redirect message, but the
68
+ # return result is still true. If it's set to false then a redirect
69
+ # response is considered a failed ping.
70
+ #
71
+ # If no file or path is specified in the URI, then '/' is assumed.
72
+ # If no scheme is present in the URI, then 'http' is assumed.
73
+ #
74
+ def ping(host = @host)
75
+ super(host)
76
+ bool = false
77
+
78
+ # See https://bugs.ruby-lang.org/issues/8645
79
+ host = "http://#{host}" unless host.include?("http")
80
+
81
+ uri = URI.parse(host)
82
+
83
+ # A port provided here overrides anything provided in constructor
84
+ port = URI.split(host)[3] || @port
85
+ port = port.to_i
86
+
87
+ start_time = Time.now
88
+
89
+ response = do_ping(uri, port)
90
+
91
+ if response.is_a?(Net::HTTPSuccess)
92
+ bool = true
93
+ elsif redirect?(response) # Check code, HTTPRedirection does not always work
94
+ if @follow_redirect
95
+ @warning = response.message
96
+ rlimit = 0
97
+
98
+ while redirect?(response)
99
+ if rlimit >= redirect_limit
100
+ @exception = "Redirect limit exceeded"
101
+ break
102
+ end
103
+ redirect = URI.parse(response['location'])
104
+ redirect = uri + redirect if redirect.relative?
105
+ response = do_ping(redirect, port)
106
+ rlimit += 1
107
+ end
108
+
109
+ if response.is_a?(Net::HTTPSuccess)
110
+ bool = true
111
+ else
112
+ @warning = nil
113
+ @exception ||= response.message
114
+ end
115
+
116
+ else
117
+ @exception = response.message
118
+ end
119
+ else
120
+ @exception ||= response.message
121
+ end
122
+
123
+ # There is no duration if the ping failed
124
+ @duration = Time.now - start_time if bool
125
+
126
+ bool
127
+ end
128
+
129
+ alias ping? ping
130
+ alias pingecho ping
131
+ alias follow_redirect? follow_redirect
132
+ alias uri host
133
+ alias uri= host=
134
+
135
+ private
136
+
137
+ def redirect?(response)
138
+ response && response.code.to_i >= 300 && response.code.to_i < 400
139
+ end
140
+
141
+ def do_ping(uri, port)
142
+ response = nil
143
+ proxy = uri.find_proxy || URI.parse("")
144
+ begin
145
+ uri_path = uri.path.empty? ? '/' : uri.path
146
+ headers = { }
147
+ headers["User-Agent"] = user_agent unless user_agent.nil?
148
+ Timeout.timeout(@timeout) do
149
+ http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, port)
150
+ @proxied = http.proxy?
151
+ if @get_request == true
152
+ request = Net::HTTP::Get.new(uri_path)
153
+ else
154
+ request = Net::HTTP::Head.new(uri_path)
155
+ end
156
+
157
+ if uri.scheme == 'https'
158
+ http.use_ssl = true
159
+ http.verify_mode = @ssl_verify_mode
160
+ end
161
+
162
+ response = http.start { |h| h.request(request) }
163
+ end
164
+ rescue Exception => err
165
+ @exception = err.message
166
+ end
167
+ @code = response.code if response
168
+ response
169
+ end
170
+ end
171
+ end