class IORequest::SSLSockets::Client

SSL socket client.

Public Class Methods

new( authorizer: Authorizer.empty, certificate: nil, key: nil, &requests_handler ) click to toggle source

Initialize new client. @param authorizer [Authorizer] @param certificate [String] @param key [String]

# File lib/io_request/connection/ssl_sockets.rb, line 112
def initialize(
  authorizer: Authorizer.empty,
  certificate: nil,
  key: nil,
  &requests_handler
)
  @authorizer = authorizer
  @requests_handler = requests_handler

  @client = nil

  initialize_ssl_context(certificate, key)
end

Public Instance Methods

connect(host = 'localhost', port = 8000) click to toggle source

Connect to server. @param host [String] host of server. @param port [Integer] port of server.

# File lib/io_request/connection/ssl_sockets.rb, line 133
def connect(host = 'localhost', port = 8000)
  socket = TCPSocket.new(host, port)

  ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, @ctx)
  ssl_socket.sync_close = true
  ssl_socket.connect

  @client = IORequest::Client.new authorizer: @authorizer
  begin
    @client.open read_write: ssl_socket
    @client.on_request(&@requests_handler)
    @client.on_close do
      @client = nil
    end
  rescue StandardError
    ssl_socket.close
    @client = nil
  end
end
connected?() click to toggle source
# File lib/io_request/connection/ssl_sockets.rb, line 126
def connected?
  !@client.nil?
end
disconnect() click to toggle source

Closes connection to server.

# File lib/io_request/connection/ssl_sockets.rb, line 154
def disconnect
  return unless defined?(@client) && !@client.nil?

  @client.close
  @client = nil
end
request(*args, **options, &block) click to toggle source

Wrapper over {IORequest::Client#request}

# File lib/io_request/connection/ssl_sockets.rb, line 162
def request(*args, **options, &block)
  @client.request(*args, **options, &block)
end

Private Instance Methods

initialize_ssl_context(certificate, key) click to toggle source
# File lib/io_request/connection/ssl_sockets.rb, line 168
def initialize_ssl_context(certificate, key)
  @ctx = OpenSSL::SSL::SSLContext.new
  @ctx.cert = OpenSSL::X509::Certificate.new certificate
  @ctx.key = OpenSSL::PKey::RSA.new key
  @ctx.ssl_version = :TLSv1_2
end