Module: Async::HTTP::Protocol::HTTP

Defined in:
lib/async/http/protocol/http.rb

Overview

HTTP is an http:// server that auto-selects HTTP/1.1 or HTTP/2 by detecting the HTTP/2 connection preface.

Constant Summary collapse

HTTP2_PREFACE =
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
HTTP2_PREFACE_SIZE =
HTTP2_PREFACE.bytesize

Class Method Summary collapse

Class Method Details

.client(peer, **options) ⇒ Object

Only inbound connections can detect HTTP1 vs HTTP2 for http://. Outbound connections default to HTTP1.



40
41
42
# File 'lib/async/http/protocol/http.rb', line 40

def self.client(peer, **options)
	HTTP1.client(peer, **options)
end

.namesObject



50
51
52
# File 'lib/async/http/protocol/http.rb', line 50

def self.names
	["h2", "http/1.1", "http/1.0"]
end

.protocol_for(stream) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/async/http/protocol/http.rb', line 19

def self.protocol_for(stream)
	# Detect HTTP/2 connection preface
	# https://www.rfc-editor.org/rfc/rfc9113.html#section-3.4
	preface = stream.peek do |read_buffer|
		if read_buffer.bytesize >= HTTP2_PREFACE_SIZE
			break read_buffer[0, HTTP2_PREFACE_SIZE]
		elsif read_buffer.bytesize > 0
			# If partial read_buffer already doesn't match, no need to wait for more bytes.
			break read_buffer unless HTTP2_PREFACE[read_buffer]
		end
	end
	
	if preface == HTTP2_PREFACE
		HTTP2
	else
		HTTP1
	end
end

.server(peer, **options) ⇒ Object



44
45
46
47
48
# File 'lib/async/http/protocol/http.rb', line 44

def self.server(peer, **options)
	stream = ::IO::Stream(peer)
	
	return protocol_for(stream).server(stream, **options)
end