Class: Puma::MiniSSL::Socket
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/puma/minissl.rb |
Class Method Summary
- .new(socket, engine) ⇒ Socket constructor
Instance Attribute Summary
- #closed? ⇒ Boolean readonly
- #peeraddr readonly
- #peercert readonly
-
#ssl_version_state
readonly
Returns a two element array, first is protocol version (SSL_get_version), second is ‘handshake’ state (SSL_state_string).
- #to_io readonly
-
#bad_tlsv1_3? ⇒ Boolean
readonly
private
Used to check the handshake status, in particular when a TCP connection is made with
TLSv1.3
as an available protocol.
Instance Method Summary
-
#<<(data)
Alias for #write.
- #close
- #engine_read_all
- #flush
- #read_nonblock(size, *_)
- #readpartial(size)
-
#syswrite(data)
Alias for #write.
- #write(data) (also: #syswrite, #<<)
-
#write_nonblock(data, *_)
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.
Constructor Details
.new(socket, engine) ⇒ Socket
# File 'lib/puma/minissl.rb', line 21
def initialize(socket, engine) @socket = socket @engine = engine @peercert = nil end
Instance Attribute Details
#bad_tlsv1_3? ⇒ Boolean
(readonly, private)
Used to check the handshake status, in particular when a TCP connection is made with TLSv1.3
as an available protocol
# File 'lib/puma/minissl.rb', line 52
def bad_tlsv1_3? HAS_TLS1_3 && @engine.ssl_vers_st == ['TLSv1.3', 'SSLERR'] end
#closed? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/puma/minissl.rb', line 32
def closed? @socket.closed? end
#peeraddr (readonly)
[ GitHub ]# File 'lib/puma/minissl.rb', line 180
def peeraddr @socket.peeraddr end
#peercert (readonly)
[ GitHub ]# File 'lib/puma/minissl.rb', line 185
def peercert return @peercert if @peercert raw = @engine.peercert return nil unless raw @peercert = OpenSSL::X509::Certificate.new raw end
#ssl_version_state (readonly)
Returns a two element array, first is protocol version (SSL_get_version), second is ‘handshake’ state (SSL_state_string)
Used for dropping tcp connections to ssl. See OpenSSL ssl/ssl_stat.c SSL_state_string for info
# File 'lib/puma/minissl.rb', line 45
def ssl_version_state IS_JRUBY ? [nil, nil] : @engine.ssl_vers_st end
#to_io (readonly)
[ GitHub ]# File 'lib/puma/minissl.rb', line 28
def to_io @socket end
Instance Method Details
#<<(data)
Alias for #write.
# File 'lib/puma/minissl.rb', line 141
alias_method :<<, :write
#close
[ GitHub ]# File 'lib/puma/minissl.rb', line 164
def close begin unless @engine.shutdown while alert_data = @engine.extract @socket.write alert_data end end rescue IOError, SystemCallError Puma::Util.purge_interrupt_queue # nothing ensure @socket.close end end
#engine_read_all
[ GitHub ]# File 'lib/puma/minissl.rb', line 74
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 160
def flush @socket.flush end
#read_nonblock(size, *_)
[ GitHub ]# File 'lib/puma/minissl.rb', line 82
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? raise SSLError.exception "HTTP connection?" if bad_tlsv1_3? 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 57
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.
# File 'lib/puma/minissl.rb', line 140
alias_method :syswrite, :write
#write(data) Also known as: #syswrite, #<<
[ GitHub ]# File 'lib/puma/minissl.rb', line 117
def write(data) return 0 if data.empty? data_size = data.bytesize need = data_size while true wrote = @engine.write data enc_wr = ''.dup while (enc = @engine.extract) enc_wr << enc end @socket.write enc_wr unless enc_wr.empty? need -= wrote return data_size if need == 0 data = data.byteslice(wrote..-1) end end
#write_nonblock(data, *_)
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 156
def write_nonblock(data, *_) write data end