Class: HTTPX::SSL

Inherits:
TCP
  • Object
show all
Defined in:
lib/httpx/io/ssl.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
  # :nocov:
  {}
  # :nocov:
end

Constants included from Loggable

Loggable::COLORS

Instance Attribute Summary

Attributes inherited from TCP

#addresses, #ip, #port, #state

Instance Method Summary collapse

Methods inherited from TCP

#closed?, #to_io

Methods included from Loggable

#log, #log_exception

Constructor Details

#initialize(_, _, options) ⇒ SSL

Returns a new instance of SSL.



15
16
17
18
19
20
21
# File 'lib/httpx/io/ssl.rb', line 15

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



40
41
42
43
44
45
46
# File 'lib/httpx/io/ssl.rb', line 40

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/httpx/io/ssl.rb', line 52

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
  @io.connect_nonblock
  @io.post_connection_check(@hostname) if @ctx.verify_mode != OpenSSL::SSL::VERIFY_NONE
  transition(:negotiated)
rescue ::IO::WaitReadable
  @interests = :r
rescue ::IO::WaitWritable
  @interests = :w
end

#connected?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/httpx/io/ssl.rb', line 48

def connected?
  @state == :negotiated
end

#inspectObject

:nocov:



106
107
108
109
# File 'lib/httpx/io/ssl.rb', line 106

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

#interestsObject



23
24
25
# File 'lib/httpx/io/ssl.rb', line 23

def interests
  @interests || super
end

#protocolObject



27
28
29
30
31
# File 'lib/httpx/io/ssl.rb', line 27

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

#read(size, buffer) ⇒ Object



77
78
79
80
81
82
# File 'lib/httpx/io/ssl.rb', line 77

def read(_, buffer)
  super
rescue ::IO::WaitWritable
  buffer.clear
  0
end

#verify_hostname(host) ⇒ Object



33
34
35
36
37
38
# File 'lib/httpx/io/ssl.rb', line 33

def verify_hostname(host)
  return false if @ctx.verify_mode == OpenSSL::SSL::VERIFY_NONE
  return false if !@io.respond_to?(:peer_cert) || @io.peer_cert.nil?

  OpenSSL::SSL.verify_certificate_identity(@io.peer_cert, host)
end

#writeObject



84
85
86
87
88
# File 'lib/httpx/io/ssl.rb', line 84

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