Class: Async::HTTP::Protocol::HTTPS

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/protocol/https.rb

Overview

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

Constant Summary collapse

HANDLERS =

The protocol classes for each supported protocol.

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

Instance Method Summary collapse

Constructor Details

#initialize(handlers = HANDLERS, **options) ⇒ HTTPS

Initialize the HTTPS protocol negotiator.



29
30
31
32
# File 'lib/async/http/protocol/https.rb', line 29

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

Instance Method Details

#add(name, protocol, **options) ⇒ Object

Register a protocol handler for a given ALPN protocol name.



38
39
40
41
# File 'lib/async/http/protocol/https.rb', line 38

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

#client(peer, **options) ⇒ Object

Create a client for an outbound connection.



66
67
68
69
70
71
# File 'lib/async/http/protocol/https.rb', line 66

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

#namesObject



85
86
87
# File 'lib/async/http/protocol/https.rb', line 85

def names
	@handlers.keys.compact
end

#protocol_for(peer) ⇒ Object

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

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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/http/protocol/https.rb', line 49

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

#server(peer, **options) ⇒ Object

Create a server for an inbound connection.



77
78
79
80
81
82
# File 'lib/async/http/protocol/https.rb', line 77

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