Class: Dialed::HTTP::ConnectionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dialed/http/connection_builder.rb

Constant Summary collapse

DirectConnectionConfiguration =
Data.define(:uri, :version, :ssl_context)
TunneledConnectionConfiguration =
Data.define(:uri, :proxy_uri, :version, :ssl_context)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionBuilder

Returns a new instance of ConnectionBuilder.



20
21
22
23
24
25
26
# File 'lib/dialed/http/connection_builder.rb', line 20

def initialize
  @version = :h2
  @proxy_uri = nil
  @ssl_context = build_ssl_context
  @uri = Addressable::URI.new
  @uri_defaults = { scheme: 'https', port: 443 }
end

Instance Attribute Details

#proxy_uriObject (readonly)

Returns the value of attribute proxy_uri.



11
12
13
# File 'lib/dialed/http/connection_builder.rb', line 11

def proxy_uri
  @proxy_uri
end

#schemeObject

Returns the value of attribute scheme.



11
12
13
# File 'lib/dialed/http/connection_builder.rb', line 11

def scheme
  @scheme
end

#ssl_contextObject

Returns the value of attribute ssl_context.



10
11
12
# File 'lib/dialed/http/connection_builder.rb', line 10

def ssl_context
  @ssl_context
end

#uriObject

Returns the value of attribute uri.



11
12
13
# File 'lib/dialed/http/connection_builder.rb', line 11

def uri
  @uri
end

#versionObject

Returns the value of attribute version.



11
12
13
# File 'lib/dialed/http/connection_builder.rb', line 11

def version
  @version
end

Class Method Details

.apply_defaultsObject



16
17
18
# File 'lib/dialed/http/connection_builder.rb', line 16

def self.apply_defaults
  new.tap(&:apply_defaults!)
end

Instance Method Details

#add_certificate(path) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/dialed/http/connection_builder.rb', line 93

def add_certificate(path)
  pathname = Pathname(path)
  pathname = Pathname(File.expand_path(path)) if pathname.relative?

  certificate = OpenSSL::X509::Certificate.new(pathname.read)
  cert_store.add_cert(certificate)
  self
end

#alpn_protocolsObject



85
86
87
# File 'lib/dialed/http/connection_builder.rb', line 85

def alpn_protocols
  ssl_context.alpn_protocols
end

#alpn_protocols=(protocols) ⇒ Object



81
82
83
# File 'lib/dialed/http/connection_builder.rb', line 81

def alpn_protocols=(protocols)
  ssl_context.alpn_protocols = protocols
end

#apply_defaults!Object



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

def apply_defaults!
  defaults_to_apply = @uri_defaults.select do |key, _value|
    uri_value = uri.send(key)
    next if uri_value.present?

    true
  end

  uri.merge!(defaults_to_apply)
  self
end

#buildObject

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dialed/http/connection_builder.rb', line 55

def build
  apply_defaults!
  raise Dialed::Error, 'Cannot build. Invalid' unless valid?

  if proxy_uri
    configuration = TunneledConnectionConfiguration.new(
      uri:         uri,
      version:     version,
      ssl_context: ssl_context,
      proxy_uri:   proxy_uri
    )
    TunneledConnection.new(configuration)
  else
    configuration = DirectConnectionConfiguration.new(
      uri:         uri,
      version:     version,
      ssl_context: ssl_context
    )
    DirectConnection.new(configuration)
  end
end

#build_ssl_contextObject



28
29
30
31
32
33
# File 'lib/dialed/http/connection_builder.rb', line 28

def build_ssl_context
  OpenSSL::SSL::SSLContext.new.tap do |ssl_context|
    ssl_context.alpn_protocols = %w[h2 http/1.1]
    ssl_context.verify_hostname = true
  end
end

#cert_storeObject



77
78
79
# File 'lib/dialed/http/connection_builder.rb', line 77

def cert_store
  ssl_context.cert_store ||= OpenSSL::X509::Store.new
end

#cert_store=(cert_store) ⇒ Object



89
90
91
# File 'lib/dialed/http/connection_builder.rb', line 89

def cert_store=(cert_store)
  ssl_context.cert_store = cert_store
end

#proxy(&block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dialed/http/connection_builder.rb', line 139

def proxy(&block)
  if block_given?
    uri = ProxyUri.new.tap(&block)
    uri.infer_scheme_if_missing!
    raise ArgumentError, "Invalid proxy URI: #{uri.inspect}" unless uri.valid?

    @proxy_uri = uri

    self
  else
    @proxy_uri
  end
  self
end

#proxy=(proxy_uri) ⇒ Object Also known as: proxy_uri=



154
155
156
157
158
159
# File 'lib/dialed/http/connection_builder.rb', line 154

def proxy=(proxy_uri)
  parsed = Addressable::URI.parse(proxy_uri)
  proxy do |self_proxy|
    self_proxy.merge!(parsed)
  end
end

#uri_valid?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dialed/http/connection_builder.rb', line 47

def uri_valid?
  uri.send(:validate).nil?
end

#valid?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dialed/http/connection_builder.rb', line 51

def valid?
  uri_valid? && !ssl_context.nil? && version.present?
end

#verify_none=(verify_none) ⇒ Object Also known as: insecure=



124
125
126
# File 'lib/dialed/http/connection_builder.rb', line 124

def verify_none=(verify_none)
  ssl_context.verify_mode = (OpenSSL::SSL::VERIFY_NONE if verify_none)
end

#verify_peer=(verify_peer) ⇒ Object



120
121
122
# File 'lib/dialed/http/connection_builder.rb', line 120

def verify_peer=(verify_peer)
  ssl_context.verify_mode = (OpenSSL::SSL::VERIFY_PEER if verify_peer)
end