Class: Thrift::SSLSocket

Inherits:
Socket show all
Defined in:
lib/thrift/transport/ssl_socket.rb

Instance Attribute Summary collapse

Attributes inherited from Socket

#handle, #timeout

Instance Method Summary collapse

Methods inherited from Socket

#close, #open?, #read, #to_io, #write

Methods inherited from BaseTransport

#close, #flush, #open?, #read, #read_all, #read_byte, #read_into_buffer, #write

Constructor Details

#initialize(host = 'localhost', port = 9090, timeout = nil, ssl_context = nil) ⇒ SSLSocket

Returns a new instance of SSLSocket.



25
26
27
28
# File 'lib/thrift/transport/ssl_socket.rb', line 25

def initialize(host = 'localhost', port = 9090, timeout = nil, ssl_context = nil)
  super(host, port, timeout)
  @ssl_context = ssl_context
end

Instance Attribute Details

#ssl_contextObject

Returns the value of attribute ssl_context.



30
31
32
# File 'lib/thrift/transport/ssl_socket.rb', line 30

def ssl_context
  @ssl_context
end

Instance Method Details

#openObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/thrift/transport/ssl_socket.rb', line 32

def open
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout unless @timeout.nil? || @timeout == 0
  socket = connect_socket(deadline)

  begin
    ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, @ssl_context)
    ssl_socket.sync_close = true
    @handle = ssl_socket

    if deadline
      loop do
        result = @handle.connect_nonblock(exception: false)

        case result
        when @handle
          break
        when :wait_readable
          remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
          if remaining <= 0 || !@handle.to_io.wait_readable(remaining)
            raise TransportException.new(TransportException::TIMED_OUT, "SSL socket: Timed out establishing session with #{@desc}")
          end
        when :wait_writable
          remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
          if remaining <= 0 || !@handle.to_io.wait_writable(remaining)
            raise TransportException.new(TransportException::TIMED_OUT, "SSL socket: Timed out establishing session with #{@desc}")
          end
        else
          raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}: unexpected SSL connect result #{result.inspect}")
        end
      end
    else
      @handle.connect
    end

    @handle.post_connection_check(@host)
    @handle
  rescue TransportException
    close_socket(@handle)
    @handle = nil
    raise
  rescue StandardError
    close_socket(@handle || socket)
    @handle = nil
    raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}")
  end
end

#to_sObject



79
80
81
# File 'lib/thrift/transport/ssl_socket.rb', line 79

def to_s
  "ssl(#{super.to_s})"
end