@@ -16,6 +16,10 @@ module HTTP
1616 HTTP2_PREFACE = "PRI * HTTP/2.0\r \n \r \n SM\r \n \r \n "
1717 HTTP2_PREFACE_SIZE = HTTP2_PREFACE . bytesize
1818
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.
1923 def self . protocol_for ( stream )
2024 # Detect HTTP/2 connection preface
2125 # https://www.rfc-editor.org/rfc/rfc9113.html#section-3.4
@@ -35,18 +39,28 @@ def self.protocol_for(stream)
3539 end
3640 end
3741
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.
4046 def self . client ( peer , **options )
47+ options = options [ protocol ]
48+
4149 HTTP1 . client ( peer , **options )
4250 end
4351
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.
4456 def self . server ( peer , **options )
4557 stream = ::IO ::Stream ( peer )
58+ protocol = protocol_for ( stream )
4659
47- return protocol_for ( stream ) . server ( stream , ** options )
60+ return protocol . server ( stream , options [ protocol ] )
4861 end
4962
63+ # @returns [Array] The names of the supported protocols.
5064 def self . names
5165 [ "h2" , "http/1.1" , "http/1.0" ]
5266 end
0 commit comments