Class: Puma::MiniSSL::Socket
- Inherits:
-
Object
- Object
- Puma::MiniSSL::Socket
- Defined in:
- lib/puma/minissl.rb
Instance Attribute Summary collapse
- #peeraddr ⇒ Object readonly
-
#peercert ⇒ OpenSSL::X509::Certificate?
readonly
OpenSSL is loaded in
MiniSSL::ContextBuilderwhenMiniSSL::Context#verify_modeis notVERIFY_NONE. -
#ssl_version_state ⇒ Object
readonly
Returns a two element array, first is protocol version (SSL_get_version), second is 'handshake' state (SSL_state_string).
- #to_io ⇒ Object readonly
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #engine_read_all ⇒ Object
- #flush ⇒ Object
-
#initialize(socket, engine) ⇒ Socket
constructor
A new instance of Socket.
- #read_nonblock(size, *_, exception: true) ⇒ Object
- #readpartial(size) ⇒ Object
- #wait_writable(timeout = nil) ⇒ Object
- #write(data) ⇒ Object (also: #syswrite, #<<)
-
#write_nonblock(data, exception: true) ⇒ Object
As with SSL_write, retry with the same data after IO::EAGAINWaitWritable.
- #write_pending? ⇒ Boolean
Constructor Details
#initialize(socket, engine) ⇒ Socket
Returns a new instance of Socket.
23 24 25 26 27 28 29 30 31 |
# File 'lib/puma/minissl.rb', line 23 def initialize(socket, engine) @socket = socket @engine = engine @peercert = nil @reuse = nil @write_buffer = +'' @write_owed = 0 @write_owed_data = nil end |
Instance Attribute Details
#peeraddr ⇒ Object (readonly)
229 230 231 |
# File 'lib/puma/minissl.rb', line 229 def peeraddr @socket.peeraddr end |
#peercert ⇒ OpenSSL::X509::Certificate? (readonly)
OpenSSL is loaded in MiniSSL::ContextBuilder when
MiniSSL::Context#verify_mode is not VERIFY_NONE.
When VERIFY_NONE, MiniSSL::Engine#peercert is nil, regardless of
whether the client sends a cert.
239 240 241 242 243 244 245 246 |
# File 'lib/puma/minissl.rb', line 239 def peercert return @peercert if @peercert raw = @engine.peercert return nil unless raw @peercert = OpenSSL::X509::Certificate.new raw end |
#ssl_version_state ⇒ Object (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
51 52 53 |
# File 'lib/puma/minissl.rb', line 51 def ssl_version_state IS_JRUBY ? [nil, nil] : @engine.ssl_vers_st end |
#to_io ⇒ Object (readonly)
34 35 36 |
# File 'lib/puma/minissl.rb', line 34 def to_io @socket end |
Instance Method Details
#close ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/puma/minissl.rb', line 194 def close begin queue_ciphertext unless @engine.shutdown flush_write_buffer_nonblock rescue IOError, SystemCallError # nothing ensure @socket.close end end |
#closed? ⇒ Boolean
38 39 40 |
# File 'lib/puma/minissl.rb', line 38 def closed? @socket.closed? end |
#engine_read_all ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/puma/minissl.rb', line 79 def engine_read_all output = @engine.read while output and additional_output = @engine.read output << additional_output end output end |
#flush ⇒ Object
189 190 191 192 |
# File 'lib/puma/minissl.rb', line 189 def flush flush_write_buffer @socket.flush end |
#read_nonblock(size, *_, exception: true) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/puma/minissl.rb', line 87 def read_nonblock(size, *_, exception: true) # *_ is to deal with keyword args that were added # at some point (and being used in the wild) output = engine_read_all return output if output unless flush_write_buffer_nonblock raise IO::EAGAINWaitReadable if exception return :wait_readable end 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 what's 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 if exception return :wait_readable 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 queue_ciphertext flush_write_buffer_nonblock raise IO::EAGAINWaitReadable if exception :wait_readable end |
#readpartial(size) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/puma/minissl.rb', line 63 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 queue_ciphertext flush_write_buffer end end |
#wait_writable(timeout = nil) ⇒ Object
185 186 187 |
# File 'lib/puma/minissl.rb', line 185 def wait_writable(timeout = nil) @socket.wait_writable timeout end |
#write(data) ⇒ Object Also known as: syswrite, <<
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/puma/minissl.rb', line 130 def write(data) return 0 if data.empty? data_size = data.bytesize flush_write_buffer if @write_owed_data && data.byteslice(0, @write_owed_data.bytesize).b == @write_owed_data data = data.byteslice(@write_owed..-1) @write_owed = 0 @write_owed_data = nil return data_size if data.empty? end need = data.bytesize while true wrote = @engine.write data queue_ciphertext flush_write_buffer need -= wrote return data_size if need == 0 data = data.byteslice(wrote..-1) end end |
#write_nonblock(data, exception: true) ⇒ Object
As with SSL_write, retry with the same data after IO::EAGAINWaitWritable
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/puma/minissl.rb', line 162 def write_nonblock(data, exception: true) return 0 if data.empty? @write_owed = 0 if @write_owed_data && data.byteslice(0, @write_owed_data.bytesize).b != @write_owed_data if @write_owed == 0 @write_owed = @engine.write data @write_owed_data = nil queue_ciphertext end unless flush_write_buffer_nonblock @write_owed_data ||= data.byteslice(0, @write_owed).b raise IO::EAGAINWaitWritable if exception return :wait_writable end wrote = @write_owed @write_owed = 0 @write_owed_data = nil wrote end |
#write_pending? ⇒ Boolean
126 127 128 |
# File 'lib/puma/minissl.rb', line 126 def write_pending? !@write_buffer.empty? end |