Class: Async::HTTP::Protocol::HTTP
- Inherits:
-
Object
- Object
- 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
Instance Method Summary collapse
-
#client(peer, **options) ⇒ Object
Create a client for an outbound connection.
-
#initialize(http1: HTTP1, http2: HTTP2) ⇒ HTTP
constructor
Create a new HTTP protocol instance.
-
#protocol_for(stream) ⇒ Object
Determine if the inbound connection is HTTP/1 or HTTP/2.
-
#server(peer, **options) ⇒ Object
Create a server for an inbound connection.
Constructor Details
Instance Method Details
#client(peer, **options) ⇒ Object
Create a client for an outbound connection. Defaults to HTTP/1 for plaintext connections.
56 57 58 59 60 |
# File 'lib/async/http/protocol/http.rb', line 56 def client(peer, **) = [@http1] || {} return @http1.client(peer, **) end |
#protocol_for(stream) ⇒ Object
Determine if the inbound connection is HTTP/1 or HTTP/2.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/async/http/protocol/http.rb', line 33 def 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
Create a server for an inbound connection. Able to detect HTTP1 and HTTP2.
66 67 68 69 70 71 72 |
# File 'lib/async/http/protocol/http.rb', line 66 def server(peer, **) stream = IO::Stream(peer) protocol = protocol_for(stream) = [protocol] || {} return protocol.server(stream, **) end |