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.



87
88
89
90
91
# File 'lib/falcon/hosts.rb', line 87

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

Instance Method Details

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

Yields:

  • (host)


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

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

#client_endpointsObject



137
138
139
140
141
# File 'lib/falcon/hosts.rb', line 137

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

#each(&block) ⇒ Object



93
94
95
# File 'lib/falcon/hosts.rb', line 93

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

#endpointObject



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

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

#host_context(socket, hostname) ⇒ Object



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

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

#proxyObject



143
144
145
# File 'lib/falcon/hosts.rb', line 143

def proxy
	Proxy.new(Falcon::BadRequest, self.client_endpoints)
end

#ssl_contextObject



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

def ssl_context
	@server_context ||= OpenSSL::SSL::SSLContext.new.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.setup
	end
end