Class: HTTPX::SSL

Inherits:
TCP
  • Object
show all
Defined in:
lib/httpx/io.rb

Direct Known Subclasses

ProxySSL

Constant Summary collapse

TLS_OPTIONS =
if OpenSSL::SSL::SSLContext.instance_methods.include?(:alpn_protocols)
  { alpn_protocols: %w[h2 http/1.1] }
else
  {}
end

Constants included from Loggable

Loggable::COLORS

Instance Attribute Summary

Attributes inherited from TCP

#ip, #port

Instance Method Summary collapse

Methods inherited from TCP

#closed?, #to_io

Methods included from Loggable

#log

Constructor Details

#initialize(_, _, options) ⇒ SSL

Returns a new instance of SSL.



156
157
158
159
160
161
162
# File 'lib/httpx/io.rb', line 156

def initialize(_, _, options)
  @ctx = OpenSSL::SSL::SSLContext.new
  ctx_options = TLS_OPTIONS.merge(options.ssl)
  @ctx.set_params(ctx_options) unless ctx_options.empty?
  super
  @state = :negotiated if @keep_open
end

Instance Method Details

#closeObject



174
175
176
177
178
179
180
# File 'lib/httpx/io.rb', line 174

def close
  super
  # allow reconnections
  # connect only works if initial @io is a socket
  @io = @io.io if @io.respond_to?(:io)
  @negotiated = false
end

#connectObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/httpx/io.rb', line 186

def connect
  super
  if @keep_open
    @state = :negotiated
    return
  end
  return if @state == :negotiated ||
            @state != :connected
  unless @io.is_a?(OpenSSL::SSL::SSLSocket)
    @io = OpenSSL::SSL::SSLSocket.new(@io, @ctx)
    @io.hostname = @hostname
    @io.sync_close = true
  end
  # TODO: this might block it all
  @io.connect_nonblock
  transition(:negotiated)
rescue ::IO::WaitReadable,
       ::IO::WaitWritable
end

#connected?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/httpx/io.rb', line 182

def connected?
  @state == :negotiated
end

#inspectObject



232
233
234
235
# File 'lib/httpx/io.rb', line 232

def inspect
  id = @io.closed? ? "closed" : @io.to_io.fileno
  "#<SSL(fd: #{id}): #{@ip}:#{@port} state: #{@state}>"
end

#protocolObject



168
169
170
171
172
# File 'lib/httpx/io.rb', line 168

def protocol
  @io.alpn_protocol || super
rescue StandardError
  super
end

#read(size, buffer) ⇒ Object



207
208
209
210
211
# File 'lib/httpx/io.rb', line 207

def read(*)
  super
rescue ::IO::WaitWritable
  0
end

#schemeObject



164
165
166
# File 'lib/httpx/io.rb', line 164

def scheme
  "https"
end

#writeObject



213
214
215
216
217
# File 'lib/httpx/io.rb', line 213

def write(*)
  super
rescue ::IO::WaitReadable
  0
end