Async::HTTPSourceAsyncHTTPProtocolHTTPS

class HTTPS

A server that supports both HTTP1.0 and HTTP1.1 semantics by detecting the version of the request.

Definitions

HANDLERS = {...}

The protocol classes for each supported protocol.

Implementation

HANDLERS = {
	"h2" => HTTP2,
	"http/1.1" => HTTP11,
	"http/1.0" => HTTP10,
	nil => HTTP11,
}

def initialize(handlers = HANDLERS, **options)

Initialize the HTTPS protocol negotiator.

Signature

parameter handlers Hash

A mapping of ALPN protocol names to protocol classes.

parameter options Hash

Per-protocol options keyed by protocol class.

Implementation

def initialize(handlers = HANDLERS, **options)
	@handlers = handlers
	@options = options
end

def add(name, protocol, **options)

Register a protocol handler for a given ALPN protocol name.

Signature

parameter name String

The ALPN protocol name.

parameter protocol Class

The protocol class to handle connections.

parameter options Hash

Options to pass when creating client or server instances.

Implementation

def add(name, protocol, **options)
	@handlers[name] = protocol
	@options[protocol] = options
end

def protocol_for(peer)

Determine the protocol of the peer and return the appropriate protocol class.

Use TLS Application Layer Protocol Negotiation (ALPN) to determine the protocol.

Signature

parameter peer IO

The peer to communicate with.

returns Class

The protocol class to use.

Implementation

def protocol_for(peer)
	# alpn_protocol is only available if openssl v1.0.2+
	name = peer.alpn_protocol
	
	Console.debug(self){"Negotiating protocol #{name.inspect}..."}
	
	if protocol = HANDLERS[name]
		return protocol
	else
		raise ArgumentError, "Could not determine protocol for connection (#{name.inspect})."
	end
end

def client(peer, **options)

Create a client for an outbound connection.

Signature

parameter peer IO

The peer to communicate with.

parameter options Hash

Options to pass to the client instance.

Implementation

def client(peer, **options)
	protocol = protocol_for(peer)
	options = options[protocol] || {}
	
	protocol.client(peer, **options)
end

def server(peer, **options)

Create a server for an inbound connection.

Signature

parameter peer IO

The peer to communicate with.

parameter options Hash

Options to pass to the server instance.

Implementation

def server(peer, **options)
	protocol = protocol_for(peer)
	options = options[protocol] || {}
	
	protocol.server(peer, **options)
end

def names

Signature

returns Array

The names of the supported protocol, used for Application Layer Protocol Negotiation (ALPN).

Implementation

def names
	@handlers.keys.compact
end