net-ping 1.7.1-universal-mingw32 → 1.7.2-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,122 +1,74 @@
1
- require 'ffi'
2
- require 'rbconfig'
3
-
4
- require File.join(File.dirname(__FILE__), 'ping')
5
-
6
- if File::ALT_SEPARATOR && RUBY_VERSION.to_f < 1.9 && RUBY_PLATFORM != 'java'
7
- require 'win32/open3'
8
- else
9
- require 'open3'
10
- end
11
-
12
- # The Net module serves as a namespace only.
13
- module Net
14
-
15
- # The Ping::External class encapsulates methods for external (system) pings.
16
- class Ping::External < Ping
17
-
18
- if File::ALT_SEPARATOR
19
- extend FFI::Library
20
- ffi_lib 'kernel32'
21
-
22
- attach_function :SetConsoleCP, [:uint], :bool
23
- attach_function :GetConsoleCP, [], :uint
24
- end
25
-
26
- # Pings the host using your system's ping utility and checks for any
27
- # errors or warnings. Returns true if successful, or false if not.
28
- #
29
- # If the ping failed then the Ping::External#exception method should
30
- # contain a string indicating what went wrong. If the ping succeeded then
31
- # the Ping::External#warning method may or may not contain a value.
32
- #
33
- def ping(host = @host)
34
- super(host)
35
-
36
- stdin = stdout = stderr = nil
37
- pstring = "ping "
38
- bool = false
39
- orig_cp = nil
40
-
41
- case RbConfig::CONFIG['host_os']
42
- when /linux|bsd|osx|mach|darwin/i
43
- pstring += "-c 1 #{host}"
44
- when /solaris|sunos/i
45
- pstring += "#{host} 1"
46
- when /hpux/i
47
- pstring += "#{host} -n 1"
48
- when /win32|windows|msdos|mswin|cygwin|mingw/i
49
- orig_cp = GetConsoleCP()
50
- SetConsoleCP(437) if orig_cp != 437 # United States
51
- pstring += "-n 1 #{host}"
52
- else
53
- pstring += "#{host}"
54
- end
55
-
56
- start_time = Time.now
57
-
58
- begin
59
- err = nil
60
-
61
- Timeout.timeout(@timeout){
62
- stdin, stdout, stderr = Open3.popen3(pstring)
63
- err = stderr.gets # Can't chomp yet, might be nil
64
- }
65
-
66
- stdin.close
67
- stderr.close
68
-
69
- if File::ALT_SEPARATOR && GetConsoleCP() != orig_cp
70
- SetConsoleCP(orig_cp)
71
- end
72
-
73
- unless err.nil?
74
- if err =~ /warning/i
75
- @warning = err.chomp
76
- bool = true
77
- else
78
- @exception = err.chomp
79
- end
80
- # The "no answer" response goes to stdout, not stderr, so check it
81
- else
82
- lines = stdout.readlines
83
- stdout.close
84
- if lines.nil? || lines.empty?
85
- bool = true
86
- else
87
- regexp = /
88
- no\ answer|
89
- host\ unreachable|
90
- could\ not\ find\ host|
91
- request\ timed\ out|
92
- 100%\ packet\ loss
93
- /ix
94
-
95
- lines.each{ |line|
96
- if regexp.match(line)
97
- @exception = line.chomp
98
- break
99
- end
100
- }
101
-
102
- bool = true unless @exception
103
- end
104
- end
105
- rescue Exception => error
106
- @exception = error.message
107
- ensure
108
- stdin.close if stdin && !stdin.closed?
109
- stdout.close if stdout && !stdout.closed?
110
- stderr.close if stderr && !stderr.closed?
111
- end
112
-
113
- # There is no duration if the ping failed
114
- @duration = Time.now - start_time if bool
115
-
116
- bool
117
- end
118
-
119
- alias ping? ping
120
- alias pingecho ping
121
- end
122
- 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|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,159 +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
- # Creates and returns a new Ping::HTTP object. The default port is the
41
- # port associated with the URI. The default timeout is 5 seconds.
42
- #
43
- def initialize(uri=nil, port=nil, timeout=5)
44
- @follow_redirect = true
45
- @redirect_limit = 5
46
- @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
47
- @get_request = false
48
-
49
- port ||= URI.parse(uri).port if uri
50
-
51
- super(uri, port, timeout)
52
- end
53
-
54
- # Looks for an HTTP response from the URI passed to the constructor.
55
- # If the result is a kind of Net::HTTPSuccess then the ping was
56
- # successful and true is returned. Otherwise, false is returned
57
- # and the Ping::HTTP#exception method should contain a string
58
- # indicating what went wrong.
59
- #
60
- # If the HTTP#follow_redirect accessor is set to true (which it is
61
- # by default) and a redirect occurs during the ping, then the
62
- # HTTP#warning attribute is set to the redirect message, but the
63
- # return result is still true. If it's set to false then a redirect
64
- # response is considered a failed ping.
65
- #
66
- # If no file or path is specified in the URI, then '/' is assumed.
67
- # If no scheme is present in the URI, then 'http' is assumed.
68
- #
69
- def ping(host = @host)
70
- super(host)
71
- bool = false
72
-
73
- # See https://bugs.ruby-lang.org/issues/8645
74
- host = "http://#{host}" unless host.include?("http")
75
-
76
- uri = URI.parse(host)
77
-
78
- start_time = Time.now
79
-
80
- response = do_ping(uri)
81
-
82
- if response.is_a?(Net::HTTPSuccess)
83
- bool = true
84
- elsif redirect?(response) # Check code, HTTPRedirection does not always work
85
- if @follow_redirect
86
- @warning = response.message
87
- rlimit = 0
88
-
89
- while redirect?(response)
90
- if rlimit >= redirect_limit
91
- @exception = "Redirect limit exceeded"
92
- break
93
- end
94
- redirect = URI.parse(response['location'])
95
- redirect = uri + redirect if redirect.relative?
96
- response = do_ping(redirect)
97
- rlimit += 1
98
- end
99
-
100
- if response.is_a?(Net::HTTPSuccess)
101
- bool = true
102
- else
103
- @warning = nil
104
- @exception ||= response.message
105
- end
106
-
107
- else
108
- @exception = response.message
109
- end
110
- end
111
-
112
- # There is no duration if the ping failed
113
- @duration = Time.now - start_time if bool
114
-
115
- bool
116
- end
117
-
118
- alias ping? ping
119
- alias pingecho ping
120
- alias follow_redirect? follow_redirect
121
- alias uri host
122
- alias uri= host=
123
-
124
- private
125
-
126
- def redirect?(response)
127
- response && response.code.to_i >= 300 && response.code.to_i < 400
128
- end
129
-
130
- def do_ping(uri)
131
- response = nil
132
- proxy = uri.find_proxy || URI.parse("")
133
- begin
134
- uri_path = uri.path.empty? ? '/' : uri.path
135
- headers = { }
136
- headers["User-Agent"] = user_agent unless user_agent.nil?
137
- Timeout.timeout(@timeout) do
138
- http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, uri.port)
139
- @proxied = http.proxy?
140
- if @get_request == true
141
- request = Net::HTTP::Get.new(uri_path)
142
- else
143
- request = Net::HTTP::Head.new(uri_path)
144
- end
145
-
146
- if uri.scheme == 'https'
147
- http.use_ssl = true
148
- http.verify_mode = @ssl_verify_mode
149
- end
150
-
151
- response = http.start { |h| h.request(request) }
152
- end
153
- rescue Exception => err
154
- @exception = err.message
155
- end
156
- response
157
- end
158
- end
159
- 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