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

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.



152
153
154
155
156
157
158
# File 'lib/httpx/io.rb', line 152

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

#close ⇒ Object



170
171
172
173
174
175
176
# File 'lib/httpx/io.rb', line 170

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

#connect ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/httpx/io.rb', line 182

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)


178
179
180
# File 'lib/httpx/io.rb', line 178

def connected?
  @state == :negotiated
end

#inspect ⇒ Object



228
229
230
231
# File 'lib/httpx/io.rb', line 228

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

#protocol ⇒ Object



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

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

#read(size, buffer) ⇒ Object



203
204
205
206
207
# File 'lib/httpx/io.rb', line 203

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

#scheme ⇒ Object



160
161
162
# File 'lib/httpx/io.rb', line 160

def scheme
  "https"
end

#write ⇒ Object



209
210
211
212
213
# File 'lib/httpx/io.rb', line 209

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