@@ -16,6 +16,10 @@ module HTTP
16
16
HTTP2_PREFACE = "PRI * HTTP/2.0\r \n \r \n SM\r \n \r \n "
17
17
HTTP2_PREFACE_SIZE = HTTP2_PREFACE . bytesize
18
18
19
+ # Determine if the inbound connection is HTTP/1 or HTTP/2.
20
+ #
21
+ # @parameter stream [IO::Stream] The stream to detect the protocol for.
22
+ # @returns [Class] The protocol class to use.
19
23
def self . protocol_for ( stream )
20
24
# Detect HTTP/2 connection preface
21
25
# https://www.rfc-editor.org/rfc/rfc9113.html#section-3.4
@@ -35,18 +39,28 @@ def self.protocol_for(stream)
35
39
end
36
40
end
37
41
38
- # Only inbound connections can detect HTTP1 vs HTTP2 for http://.
39
- # Outbound connections default to HTTP1.
42
+ # Create a client for an outbound connection. Defaults to HTTP/1 for plaintext connections.
43
+ #
44
+ # @parameter peer [IO] The peer to communicate with.
45
+ # @parameter options [Hash] Options to pass to the protocol, keyed by protocol class.
40
46
def self . client ( peer , **options )
47
+ options = options [ protocol ]
48
+
41
49
HTTP1 . client ( peer , **options )
42
50
end
43
51
52
+ # Create a server for an inbound connection. Able to detect HTTP1 vs HTTP2.
53
+ #
54
+ # @parameter peer [IO] The peer to communicate with.
55
+ # @parameter options [Hash] Options to pass to the protocol, keyed by protocol class.
44
56
def self . server ( peer , **options )
45
57
stream = ::IO ::Stream ( peer )
58
+ protocol = protocol_for ( stream )
46
59
47
- return protocol_for ( stream ) . server ( stream , ** options )
60
+ return protocol . server ( stream , options [ protocol ] )
48
61
end
49
62
63
+ # @returns [Array] The names of the supported protocols.
50
64
def self . names
51
65
[ "h2" , "http/1.1" , "http/1.0" ]
52
66
end
0 commit comments