123456789_123456789_123456789_123456789_123456789_

Class: Puma::MiniSSL::Socket

Relationships & Source Files
Inherits: Object
Defined in: lib/puma/minissl.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(socket, engine) ⇒ Socket

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 11

def initialize(socket, engine)
  @socket = socket
  @engine = engine
  @peercert = nil
end

Instance Attribute Details

#closed?Boolean (readonly)

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 21

def closed?
  @socket.closed?
end

#should_drop_bytes?Boolean (readonly)

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 135

def should_drop_bytes?
  @engine.init? || !@engine.shutdown
end

Instance Method Details

#<<(data)

Alias for #write.

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 107

alias_method :<<, :write

#close

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 139

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 [:timeout, :eof].include?(read_and_drop(1))
    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

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 42

def engine_read_all
  output = @engine.read
  while output and additional_output = @engine.read
    output << additional_output
  end
  output
end

#flush

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 122

def flush
  @socket.flush
end

#peeraddr

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 155

def peeraddr
  @socket.peeraddr
end

#peercert

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 159

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)

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 126

def read_and_drop(timeout = 1)
  return :timeout unless IO.select([@socket], nil, nil, timeout)
  return :eof unless read_nonblock(1024)
  :drop
rescue Errno::EAGAIN
  # do nothing
  :eagain
end

#read_nonblock(size, *_)

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 50

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, exception: false)
    if data == :wait_readable || data == :wait_writable
      # It would make more sense to let @socket.read_nonblock raise
      # EAGAIN if necessary but it seems like it'll misbehave on Windows.
      # I don't have a Windows machine to debug this so I can't explain
      # exactly whats happening in that OS. Please let me know if you
      # find out!
      #
      # In the meantime, we can emulate the correct behavior by
      # capturing :wait_readable & :wait_writable and raising EAGAIN
      # ourselves.
      raise IO::EAGAINWaitReadable
    elsif data.nil?
      return nil
    end

    @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)

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 25

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

#syswrite(data)

Alias for #write.

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 106

alias_method :syswrite, :write

#to_io

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 17

def to_io
  @socket
end

#write(data) Also known as: #syswrite, #<<

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 84

def write(data)
  return 0 if data.empty?

  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, *_)

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.

[ GitHub ]

  
# File 'lib/puma/minissl.rb', line 118

def write_nonblock(data, *_)
  write data
end