class Puma::MiniSSL::Socket
Public Class Methods
new(socket, engine)
click to toggle source
# File lib/puma/minissl.rb, line 4 def initialize(socket, engine) @socket = socket @engine = engine @peercert = nil end
Public Instance Methods
close()
click to toggle source
# File lib/puma/minissl.rb, line 112 def close begin # Read any drop any partially initialized sockets and any received bytes during shutdown. # Don't let this socket hold this loop forever. # If it can't send more packets within 1s, then give up. while should_drop_bytes? return if read_and_drop(1) == :timeout end rescue IOError, SystemCallError Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue # nothing ensure @socket.close end end
engine_read_all()
click to toggle source
# File lib/puma/minissl.rb, line 31 def engine_read_all output = @engine.read while output and additional_output = @engine.read output << additional_output end output end
flush()
click to toggle source
# File lib/puma/minissl.rb, line 95 def flush @socket.flush end
peeraddr()
click to toggle source
# File lib/puma/minissl.rb, line 128 def peeraddr @socket.peeraddr end
peercert()
click to toggle source
# File lib/puma/minissl.rb, line 132 def peercert return @peercert if @peercert raw = @engine.peercert return nil unless raw @peercert = OpenSSL::X509::Certificate.new raw end
read_and_drop(timeout = 1)
click to toggle source
# File lib/puma/minissl.rb, line 99 def read_and_drop(timeout = 1) return :timeout unless IO.select([@socket], nil, nil, timeout) read_nonblock(1024) :drop rescue Errno::EAGAIN # do nothing :eagain end
read_nonblock(size, *_)
click to toggle source
# File lib/puma/minissl.rb, line 39 def read_nonblock(size, *_) # *_ is to deal with keyword args that were added # at some point (and being used in the wild) while true output = engine_read_all return output if output data = @socket.read_nonblock(size) @engine.inject(data) output = engine_read_all return output if output while neg_data = @engine.extract @socket.write neg_data end end end
readpartial(size)
click to toggle source
# File lib/puma/minissl.rb, line 14 def readpartial(size) while true output = @engine.read return output if output data = @socket.readpartial(size) @engine.inject(data) output = @engine.read return output if output while neg_data = @engine.extract @socket.write neg_data end end end
should_drop_bytes?()
click to toggle source
# File lib/puma/minissl.rb, line 108 def should_drop_bytes? @engine.init? || !@engine.shutdown end
to_io()
click to toggle source
# File lib/puma/minissl.rb, line 10 def to_io @socket end
write(data)
click to toggle source
# File lib/puma/minissl.rb, line 59 def write(data) need = data.bytesize while true wrote = @engine.write data enc = @engine.extract while enc @socket.write enc enc = @engine.extract end need -= wrote return data.bytesize if need == 0 data = data[wrote..-1] end end
write_nonblock(data, *_)
click to toggle source
This is a temporary fix to deal with websockets code using write_nonblock. The problem with implementing it properly is that it means we'd have to have the ability to rewind an engine because after we write+extract, the socket #write_nonblock call might raise an exception and later code would pass the same data in, but the engine would think it had already written the data in. So for the time being (and since write blocking is quite rare), go ahead and actually block in write_nonblock.
# File lib/puma/minissl.rb, line 91 def write_nonblock(data, *_) write data end