Class: Ping::HTTP

Inherits:
Ping
  • Object
show all
Defined in:
lib/net/ping/http.rb

Overview

The Ping::HTTP class encapsulates methods for HTTP pings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, port = nil, timeout = 5) ⇒ HTTP

Creates and returns a new Ping::HTTP object. The default port is the port associated with the URI or 80. The default timeout is 5 seconds.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/net/ping/http.rb', line 47

def initialize(uri=nil, port=nil, timeout=5)
  @follow_redirect = true
  @redirect_limit  = 5
  @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
  @get_request     = false
  @code            = nil

  port ||= URI.parse(uri).port if uri
  port ||= 80

  @port = port

  super(uri, port, timeout)
end

Instance Attribute Details

#codeObject (readonly)

For unsuccessful requests that return a server error, it is useful to know the HTTP status code of the response.



42
43
44
# File 'lib/net/ping/http.rb', line 42

def code
  @code
end

#follow_redirectObject Also known as: follow_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.



23
24
25
# File 'lib/net/ping/http.rb', line 23

def follow_redirect
  @follow_redirect
end

#get_requestObject

Use GET request instead HEAD. The default is false.



35
36
37
# File 'lib/net/ping/http.rb', line 35

def get_request
  @get_request
end

#proxiedObject

was this ping proxied?



38
39
40
# File 'lib/net/ping/http.rb', line 38

def proxied
  @proxied
end

#redirect_limitObject

The maximum number of redirects allowed. The default is 5.



26
27
28
# File 'lib/net/ping/http.rb', line 26

def redirect_limit
  @redirect_limit
end

#ssl_verify_modeObject

OpenSSL certificate verification mode. The default is VERIFY_NONE.



32
33
34
# File 'lib/net/ping/http.rb', line 32

def ssl_verify_mode
  @ssl_verify_mode
end

#user_agentObject

The user agent used for the HTTP request. The default is nil.



29
30
31
# File 'lib/net/ping/http.rb', line 29

def user_agent
  @user_agent
end

Instance Method Details

#ping(host = @host) ⇒ Object

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 Ping::HTTP#exception method should contain a string indicating what went wrong.

If the HTTP#follow_redirect accessor is set to true (which it is by default) and a redirect occurs during the ping, then the HTTP#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. If no scheme is present in the URI, then 'http' is assumed.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/net/ping/http.rb', line 77

def ping(host = @host)
  super(host)
  bool = false

  # See https://bugs.ruby-lang.org/issues/8645
  host = "http://#{host}" unless /\A(http(s)?:\/\/)/.match(host)

  uri = URI.parse(host)

  # A port provided here via the host argument overrides anything
  # provided in constructor.
  #
  port = URI.split(host)[3] || URI.parse(host).port || @port
  port = port.to_i

  start_time = Time.now

  response = do_ping(uri, port)

  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'])
        port = redirect.port
        redirect = uri + redirect if redirect.relative?

        start_time = Time.now
        response = do_ping(redirect, port)
        rlimit += 1
      end

      if response.is_a?(Net::HTTPSuccess)
        bool = true
      else
        @warning   = nil
        @exception ||= response.message
      end

    else
      @exception = response.message
    end
  else
    @exception ||= response.message
  end

  # There is no duration if the ping failed
  @duration = Time.now - start_time if bool

  bool
end