-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Async::HTTP::Protocol::HTTP to auto-detect h1,h2 for inbound http…
…:// connections
- Loading branch information
Showing
6 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2023, by Thomas Morgan. | ||
|
||
require_relative 'http1' | ||
require_relative 'http2' | ||
|
||
module Async | ||
module HTTP | ||
module Protocol | ||
# HTTP is an http:// server that auto-selects HTTP/1.1 or HTTP/2 by detecting the HTTP/2 | ||
# connection preface. | ||
module HTTP | ||
HTTP2_PREFACE = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" | ||
HTTP2_PREFACE_SIZE = HTTP2_PREFACE.bytesize | ||
|
||
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 | ||
|
||
# Only inbound connections can detect HTTP1 vs HTTP2 for http://. | ||
# Outbound connections default to HTTP1. | ||
def self.client(peer, **kwargs) | ||
HTTP1.client(peer, **kwargs) | ||
end | ||
|
||
def self.server(peer, **kwargs) | ||
stream = IO::Stream.new(peer, sync: true) | ||
protocol_for(stream).server(stream, **kwargs) | ||
end | ||
|
||
def self.names | ||
["h2", "http/1.1", "http/1.0"] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2023, by Thomas Morgan. | ||
|
||
require 'async/http/protocol/http' | ||
require 'async/http/a_protocol' | ||
|
||
describe Async::HTTP::Protocol::HTTP do | ||
with 'server' do | ||
include Sus::Fixtures::Async::HTTP::ServerContext | ||
let(:protocol) {subject} | ||
|
||
with 'http11 client' do | ||
it 'should make a successful request' do | ||
response = client.get('/') | ||
expect(response).to be(:success?) | ||
expect(response.version).to be == 'HTTP/1.1' | ||
response.read | ||
end | ||
end | ||
|
||
with 'http2 client' do | ||
def make_client(endpoint, **options) | ||
options[:protocol] = Async::HTTP::Protocol::HTTP2 | ||
super | ||
end | ||
|
||
it 'should make a successful request' do | ||
response = client.get('/') | ||
expect(response).to be(:success?) | ||
expect(response.version).to be == 'HTTP/2' | ||
response.read | ||
end | ||
end | ||
end | ||
end |