Class: OpenSSL::SSL::SSLSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/polyphony/extensions/openssl.rb

Overview

OpenSSL socket helper methods (to make it compatible with Socket API) and overrides

Instance Method Summary collapse

Constructor Details

#initialize(socket, context = nil) ⇒ SSLSocket

Initializese a new SSL socket

Parameters:

  • socket (TCPSocket)

    socket to wrap

  • context (OpenSSL::SSL::SSLContext) (defaults to: nil)

    optional SSL context



20
21
22
23
# File 'lib/polyphony/extensions/openssl.rb', line 20

def initialize(socket, context = nil)
  socket = socket.respond_to?(:io) ? socket.io || socket : socket
  context ? orig_initialize(socket, context) : orig_initialize(socket)
end

Instance Method Details

#dont_lingerObject

Sets DONT_LINGER option



26
27
28
# File 'lib/polyphony/extensions/openssl.rb', line 26

def dont_linger
  io.dont_linger
end

#no_delayObject

Sets NO_DELAY option



31
32
33
# File 'lib/polyphony/extensions/openssl.rb', line 31

def no_delay
  io.no_delay
end

#read(maxlen = nil, buf = nil, buffer_pos = 0) ⇒ String

Reads from the socket. If maxlen is given, reads up to maxlen bytes from the socket, otherwise reads to EOF. If buf is given, it is used as the buffer to read into, otherwise a new string is allocated. If buffer_pos is given, reads into the given offset (in bytes) in the given buffer. If the given buffer offset is negative, it is calculated from the current end of the buffer (-1 means the read data will be appended to the end of the buffer).

If no bytes are available and EOF is not hit, this method will block until the socket is ready to read from.

Parameters:

  • maxlen (Integer, nil) (defaults to: nil)

    maximum bytes to read from socket

  • buf (String, nil) (defaults to: nil)

    buffer to read into

  • buffer_pos (Number) (defaults to: 0)

    buffer position to read into

Returns:

  • (String)

    buffer used for reading



106
107
108
109
110
111
112
113
114
115
# File 'lib/polyphony/extensions/openssl.rb', line 106

def read(maxlen = nil, buf = nil, buffer_pos = 0)
  return readpartial(maxlen, buf, buffer_pos) if buf

  buf = +''
  return readpartial(maxlen, buf) if maxlen

  readpartial(4096, buf, -1) while true
rescue EOFError
  buf
end

#read_loop(maxlen = 8192) {|String| ... } ⇒ OpenSSL::SSL::SSLSocket Also known as: recv_loop

Receives up to maxlen bytes at a time in an infinite loop. Read buffers will be passed to the given block.

Parameters:

  • maxlen (Integer) (defaults to: 8192)

    maximum bytes to receive

Yields:

  • (String)

    read data

Returns:



156
157
158
159
160
# File 'lib/polyphony/extensions/openssl.rb', line 156

def read_loop(maxlen = 8192)
  while (data = sysread(maxlen))
    yield data
  end
end

#readpartial(maxlen, buf = +'',, buffer_pos = 0, raise_on_eof = true) ⇒ String?

Reads up to maxlen from the socket. If buf is given, it is used as the buffer to read into, otherwise a new string is allocated. If buffer_pos is given, reads into the given offset (in bytes) in the given buffer. If the given buffer offset is negative, it is calculated from the current end of the buffer (-1 means the read data will be appended to the end of the buffer). If raise_on_eof is true (the default,) an EOFError will be raised on EOF, otherwise nil will be returned.

If no bytes are available and EOF is not hit, this method will block until the socket is ready to read from.

Parameters:

  • maxlen (Integer, nil)

    maximum bytes to read from socket

  • buf (String, nil) (defaults to: +'',)

    buffer to read into

  • buffer_pos (Number) (defaults to: 0)

    buffer position to read into

  • raise_on_eof (bool) (defaults to: true)

    whether to raise an exception on EOF

Returns:

  • (String, nil)

    buffer used for reading or nil on EOF

Raises:

  • (EOFError)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/polyphony/extensions/openssl.rb', line 133

def readpartial(maxlen, buf = +'', buffer_pos = 0, raise_on_eof = true)
  if buffer_pos != 0
    if (result = sysread(maxlen, +''))
      if buffer_pos == -1
        result = buf + result
      else
        result = buf[0...buffer_pos] + result
      end
    end
  else
    result = sysread(maxlen, buf)
  end

  raise EOFError if !result && raise_on_eof
  result
end

#reuse_addrObject

Sets REUSE_ADDR option



36
37
38
# File 'lib/polyphony/extensions/openssl.rb', line 36

def reuse_addr
  io.reuse_addr
end