Class: Puma::MiniSSL::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/minissl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, engine) ⇒ Socket

Returns a new instance of Socket.



21
22
23
24
25
# File 'lib/puma/minissl.rb', line 21

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

Instance Attribute Details

#peeraddrObject (readonly)



194
195
196
# File 'lib/puma/minissl.rb', line 194

def peeraddr
  @socket.peeraddr
end

#peercertObject (readonly)



199
200
201
202
203
204
205
206
# File 'lib/puma/minissl.rb', line 199

def peercert
  return @peercert if @peercert

  raw = @engine.peercert
  return nil unless raw

  @peercert = OpenSSL::X509::Certificate.new raw
end

#ssl_version_stateObject (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

Version:

  • 5.0.0



45
46
47
# File 'lib/puma/minissl.rb', line 45

def ssl_version_state
  IS_JRUBY ? [nil, nil] : @engine.ssl_vers_st
end

#to_ioObject (readonly)



28
29
30
# File 'lib/puma/minissl.rb', line 28

def to_io
  @socket
end

Instance Method Details

#closeObject



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/puma/minissl.rb', line 179

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.
    return if [:timeout, :eof].include?(read_and_drop(1)) while should_drop_bytes?
  rescue IOError, SystemCallError
    Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
    # nothing
  ensure
    @socket.close
  end
end

#closed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/puma/minissl.rb', line 32

def closed?
  @socket.closed?
end

#engine_read_allObject



74
75
76
77
78
79
80
81
# File 'lib/puma/minissl.rb', line 74

def engine_read_all
  output = @engine.read
  raise SSLError.exception "HTTP connection?" if bad_tlsv1_3?
  while output and additional_output = @engine.read
    output << additional_output
  end
  output
end

#flushObject



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

def flush
  @socket.flush
end

#read_and_drop(timeout = 1) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/puma/minissl.rb', line 163

def read_and_drop(timeout = 1)
  return :timeout unless IO.select([@socket], nil, nil, timeout)
  case @socket.read_nonblock(1024, exception: false)
  when nil
    :eof
  when :wait_readable
    :eagain
  else
    :drop
  end
end

#read_nonblock(size, *_) ⇒ Object



83
84
85
86
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
# File 'lib/puma/minissl.rb', line 83

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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

#should_drop_bytes?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/puma/minissl.rb', line 175

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

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puma/minissl.rb', line 117

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

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.



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

def write_nonblock(data, *_)
  write data
end