Class: Falcon::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/falcon/hosts.rb

Constant Summary collapse

DEFAULT_ALPN_PROTOCOLS =
['h2', 'http/1.1'].freeze

Instance Method Summary collapse

Constructor Details

#initializeHosts

Returns a new instance of Hosts.



85
86
87
88
89
# File 'lib/falcon/hosts.rb', line 85

def initialize
  @named = {}
  @server_context = nil
  @server_endpoint = nil
end

Instance Method Details

#add(name, host = Host.new) {|host| ... } ⇒ Object

Yields:

  • (host)


127
128
129
130
131
132
133
# File 'lib/falcon/hosts.rb', line 127

def add(name, host = Host.new, &block)
  host = Host.new
  
  yield host if block_given?
  
  @named[name] = host.freeze
end

#client_endpointsObject



135
136
137
138
139
# File 'lib/falcon/hosts.rb', line 135

def client_endpoints
  Hash[
    @named.collect{|name, host| [name, host.endpoint]}
  ]
end

#each(&block) ⇒ Object



91
92
93
# File 'lib/falcon/hosts.rb', line 91

def each(&block)
  @named.each(&block)
end

#endpointObject



95
96
97
98
99
100
101
# File 'lib/falcon/hosts.rb', line 95

def endpoint
  @server_endpoint ||= Async::HTTP::URLEndpoint.parse(
    'https://[::]',
    ssl_context: self.ssl_context,
    reuse_address: true
  )
end

#host_context(socket, hostname) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/falcon/hosts.rb', line 119

def host_context(socket, hostname)
  if host = @named[hostname]
    socket.hostname = hostname
    
    return host.ssl_context
  end
end

#ssl_contextObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/falcon/hosts.rb', line 103

def ssl_context
  @server_context ||= OpenSSL::SSL::SSLContext.new(:TLSv1).tap do |context|
    context.servername_cb = Proc.new do |socket, hostname|
      self.host_context(socket, hostname)
    end
    
    context.session_id_context = "falcon"
    
    context.alpn_protocols = DEFAULT_ALPN_PROTOCOLS
    
    context.set_params
    
    context.freeze
  end
end