Class: Puma::MiniSSL::Socket

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

Instance Method Summary collapse

Constructor Details

#initialize(socket, engine) ⇒ Socket

Returns a new instance of Socket.



11
12
13
14
15
# File 'lib/puma/minissl.rb', line 11

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

Instance Method Details

#closeObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puma/minissl.rb', line 140

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @socket.closed?
end

#engine_read_allObject



42
43
44
45
46
47
48
# 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

#flushObject



123
124
125
# File 'lib/puma/minissl.rb', line 123

def flush
  @socket.flush
end

#peeraddrObject



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

def peeraddr
  @socket.peeraddr
end

#peercertObject



160
161
162
163
164
165
166
167
# File 'lib/puma/minissl.rb', line 160

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



127
128
129
130
131
132
133
134
# File 'lib/puma/minissl.rb', line 127

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

    begin
      data = @socket.read_nonblock(size, exception: false)
      if data == :wait_readable || data == :wait_writable
        if @socket.to_io.respond_to?(data)
          @socket.to_io.__send__(data)
        elsif data == :wait_readable
          IO.select([@socket.to_io])
        else
          IO.select(nil, [@socket.to_io])
        end
      elsif !data
        return nil
      else
        break
      end
    end while true

    @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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

#should_drop_bytes?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/puma/minissl.rb', line 136

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

#to_ioObject



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

def to_io
  @socket
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/puma/minissl.rb', line 85

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

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.



119
120
121
# File 'lib/puma/minissl.rb', line 119

def write_nonblock(data, *_)
  write data
end