class SSLCheck::Client

Public Class Methods

new() click to toggle source
# File lib/sslcheck/client.rb, line 42
def initialize
  @response = Response.new
end
timeout_seconds() click to toggle source
# File lib/sslcheck/client.rb, line 10
def self.timeout_seconds
  @@timeout_seconds
end
timeout_seconds=(seconds) click to toggle source
# File lib/sslcheck/client.rb, line 14
def self.timeout_seconds=(seconds)
  @@timeout_seconds = seconds
end

Public Instance Methods

get(url) click to toggle source
# File lib/sslcheck/client.rb, line 46
def get(url)
  begin
    Timeout::timeout(Client.timeout_seconds) {
      uri = determine_uri(url)

      sock = TCPSocket.new(uri.host, 443)
      ctx = OpenSSL::SSL::SSLContext.new
      ctx.set_params(
        :verify_mode => OpenSSL::SSL::VERIFY_PEER,
        :timeout     => Client.timeout_seconds,
        :ssl_timeout => Client.timeout_seconds,
      )

      ctx.timeout = Client.timeout_seconds
      ctx.ssl_timeout = Client.timeout_seconds

      @socket = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket|
        socket.sync_close = true
        socket.connect
        @response.host_name = uri.host
        @response.raw_peer_cert = OpenSSL::X509::Certificate.new(socket.peer_cert)
        @response.raw_peer_cert_chain = socket.peer_cert_chain
      end

      @socket.sysclose

    }
  rescue Timeout::Error, Errno::ETIMEDOUT
    @response.errors << SSLCheck::Errors::Connection::Timeout.new({:name => "Timeout Error", :type => :timeout_error, :message => "The connection to #{url} took too long."})
  rescue SocketError
    @response.errors << SSLCheck::Errors::Connection::SocketError.new({:name => "Connection Error", :type => :socket_error, :message => "The connection to #{url} failed."})
  rescue URI::InvalidURIError
    @response.errors << SSLCheck::Errors::Connection::InvalidURI.new({:name => "Invalid URI Error", :type => :invalid_uri, :message => "The URI, #{url}, is not a valid URI."})
  rescue OpenSSL::SSL::SSLError
    @response.errors << SSLCheck::Errors::Connection::SSLVerify.new({:name => "OpenSSL Verification Error", :type => :openssl_error, :message => "There was a peer verification error."})
  end

  @response
end

Private Instance Methods

determine_uri(url) click to toggle source
# File lib/sslcheck/client.rb, line 87
def determine_uri(url)
  return URI.parse(url) if url.match(/^https\:\/\//)
  return URI.parse(url.gsub("http","https")) if url.match(/^http\:\/\//)
  return URI.parse("https://#{url}") if url.match(/^https\:\/\//).nil?
end