The Ping::HTTP class encapsulates methods for HTTP pings.
By default an http ping will follow a redirect and give you the result of the final URI. If this value is set to false, then it will not follow a redirect and will return false immediately on a redirect.
By default an http ping will follow a redirect and give you the result of the final URI. If this value is set to false, then it will not follow a redirect and will return false immediately on a redirect.
Use GET request instead HEAD. The default is false.
The maximum number of redirects allowed. The default is 5.
OpenSSL certificate verification mode. The default is VERIFY_NONE.
The user agent used for the HTTP request. The default is nil.
Creates and returns a new Ping::HTTP object. The default port is the port associated with the URI. The default timeout is 5 seconds.
# File lib/net/ping/http.rb, line 39 def initialize(uri=nil, port=nil, timeout=5) @follow_redirect = true @redirect_limit = 5 @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE @get_request = false port ||= URI.parse(uri).port if uri super(uri, port, timeout) end
Looks for an HTTP response from the URI passed to the constructor. If the result is a kind of Net::HTTPSuccess then the ping was successful and true is returned. Otherwise, false is returned and the Net::Ping#exception method should contain a string indicating what went wrong.
If the #follow_redirect accessor is set to true (which it is by default) and a redirect occurs during the ping, then the Net::Ping#warning attribute is set to the redirect message, but the return result is still true. If it's set to false then a redirect response is considered a failed ping.
If no file or path is specified in the URI, then '/' is assumed.
# File lib/net/ping/http.rb, line 64 def ping(host = @host) super(host) bool = false uri = URI.parse(host) start_time = Time.now response = do_ping(uri) if response.is_a?(Net::HTTPSuccess) bool = true elsif redirect?(response) # Check code, HTTPRedirection does not always work if @follow_redirect @warning = response.message rlimit = 0 while redirect?(response) if rlimit >= redirect_limit @exception = "Redirect limit exceeded" break end redirect = URI.parse(response['location']) redirect = uri + redirect if redirect.relative? response = do_ping(redirect) rlimit += 1 end if response.is_a?(Net::HTTPSuccess) bool = true else @warning = nil @exception ||= response.message end else @exception = response.message end end # There is no duration if the ping failed @duration = Time.now - start_time if bool bool end