Skip to content

Commit

Permalink
Support io-endpoint gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 7, 2024
1 parent 586aa14 commit 8fe949d
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 62 deletions.
2 changes: 1 addition & 1 deletion async-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 3.0"

spec.add_dependency "async", ">= 1.25"
spec.add_dependency "async-io", ">= 1.28"
spec.add_dependency "async-pool", ">= 0.2"
spec.add_dependency "io-endpoint", "~> 0.1"
spec.add_dependency "protocol-http", "~> 0.25.0"
spec.add_dependency "protocol-http1", "~> 0.16.0"
spec.add_dependency "protocol-http2", "~> 0.15.0"
Expand Down
2 changes: 0 additions & 2 deletions examples/google/codeotaku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
URL = "https://www.codeotaku.com/index"
ENDPOINT = Async::HTTP::Endpoint.parse(URL)

Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)

if count = ENV['COUNT']&.to_i
terms = terms.first(count)
end
Expand Down
2 changes: 0 additions & 2 deletions examples/google/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
URL = "https://www.google.com/search"
ENDPOINT = Async::HTTP::Endpoint.parse(URL)

# Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)

class Google < Protocol::HTTP::Middleware
def search(term)
Console.logger.info(self) {"Searching for #{term}..."}
Expand Down
6 changes: 4 additions & 2 deletions fixtures/async/http/a_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,10 @@ def after

it "can't get /" do
expect do
client.get("/")
end.to raise_exception(Async::TimeoutError)
Console.debug(self) {"Connecting to #{endpoint.inspect}"}
response = client.get("/")
Console.debug(self) {"Got response #{response.inspect}"}
end.to raise_exception(::IO::TimeoutError)
end
end

Expand Down
5 changes: 3 additions & 2 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
gemspec

# gem "async", path: "../async"
# gem "async-io", path: "../async-io"
# gem "traces", path: "../traces"
# gem "io-endpoint", path: "../io-endpoint"
gem "sus-fixtures-async-http", path: "../sus-fixtures-async-http"

# gem "protocol-http", path: "../protocol-http"
# gem "protocol-http1", path: "../protocol-http1"
Expand All @@ -28,7 +29,7 @@
gem "covered"
gem "sus"
gem "sus-fixtures-async"
gem "sus-fixtures-async-http", "~> 0.7"
# gem "sus-fixtures-async-http", "~> 0.7"
gem "sus-fixtures-openssl"

gem "bake"
Expand Down
5 changes: 1 addition & 4 deletions lib/async/http/body/pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ def initialize(input, output = Writable.new, task: Task.current)
@input = input
@output = output

head, tail = IO::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)

@head = IO::Stream.new(head)
@tail = tail
@head, @tail = ::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)

@reader = nil
@writer = nil
Expand Down
12 changes: 6 additions & 6 deletions lib/async/http/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
# Copyright, 2019-2023, by Samuel Williams.
# Copyright, 2021-2022, by Adam Daniels.

require 'async/io/host_endpoint'
require 'async/io/ssl_endpoint'
require 'async/io/ssl_socket'
require 'io/endpoint'
require 'io/endpoint/host_endpoint'
require 'io/endpoint/ssl_endpoint'

require_relative 'protocol/http1'
require_relative 'protocol/https'

module Async
module HTTP
# Represents a way to connect to a remote HTTP server.
class Endpoint < Async::IO::Endpoint
class Endpoint < ::IO::Endpoint::Generic
def self.parse(string, endpoint = nil, **options)
url = URI.parse(string).normalize

Expand Down Expand Up @@ -163,7 +163,7 @@ def build_endpoint(endpoint = nil)

if secure?
# Wrap it in SSL:
return Async::IO::SSLEndpoint.new(endpoint,
return ::IO::Endpoint::SSLEndpoint.new(endpoint,
ssl_context: self.ssl_context,
hostname: @url.hostname,
timeout: self.timeout,
Expand Down Expand Up @@ -225,7 +225,7 @@ def tcp_options
end

def tcp_endpoint
Async::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/async/http/protocol/http1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ def self.trailer?
end

def self.client(peer)
stream = IO::Stream.new(peer, sync: true)

return HTTP1::Client.new(stream, VERSION)
return HTTP1::Client.new(peer, VERSION)
end

def self.server(peer)
stream = IO::Stream.new(peer, sync: true)

return HTTP1::Server.new(stream, VERSION)
return HTTP1::Server.new(peer, VERSION)
end

def self.names
Expand Down
1 change: 1 addition & 0 deletions lib/async/http/protocol/http1/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def call(request, task: Task.current)
write_body(@version, body, false, trailer)
end

Console.logger.debug(self) {"Waiting for response..."}
response = Response.read(self, request)
@ready = true

Expand Down
8 changes: 5 additions & 3 deletions lib/async/http/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require_relative 'request'
require_relative 'response'

require 'io/connected'

module Async
module HTTP
module Protocol
Expand All @@ -31,11 +33,11 @@ def http2?
end

def read_line?
@stream.read_until(CRLF)
@stream.gets(CRLF, chomp: true)
end

def read_line
@stream.read_until(CRLF) or raise EOFError, "Could not read line!"
read_line? or raise EOFError, "Could not read line!"
end

def peer
Expand All @@ -50,7 +52,7 @@ def concurrency

# Can we use this connection to make requests?
def viable?
@ready && @stream&.connected?
@ready && @stream.connected?
end

def reusable?
Expand Down
2 changes: 2 additions & 0 deletions lib/async/http/protocol/http1/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def fail_request(status)
end

def next_request
Console.debug(self, "Reading request...", persistent: @persistent)

# The default is true.
return unless @persistent

Expand Down
8 changes: 2 additions & 6 deletions lib/async/http/protocol/http10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ def self.trailer?
end

def self.client(peer)
stream = IO::Stream.new(peer, sync: true)

return HTTP1::Client.new(stream, VERSION)
return HTTP1::Client.new(peer, VERSION)
end

def self.server(peer)
stream = IO::Stream.new(peer, sync: true)

return HTTP1::Server.new(stream, VERSION)
return HTTP1::Server.new(peer, VERSION)
end

def self.names
Expand Down
8 changes: 2 additions & 6 deletions lib/async/http/protocol/http11.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ def self.trailer?
end

def self.client(peer)
stream = IO::Stream.new(peer, sync: true)

return HTTP1::Client.new(stream, VERSION)
return HTTP1::Client.new(peer, VERSION)
end

def self.server(peer)
stream = IO::Stream.new(peer, sync: true)

return HTTP1::Server.new(stream, VERSION)
return HTTP1::Server.new(peer, VERSION)
end

def self.names
Expand Down
8 changes: 4 additions & 4 deletions lib/async/http/protocol/http2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def self.trailer?
}

def self.client(peer, settings = CLIENT_SETTINGS)
stream = IO::Stream.new(peer, sync: true)
peer.sync = true

client = Client.new(stream)
client = Client.new(peer)

client.send_connection_preface(settings)
client.start_connection
Expand All @@ -46,9 +46,9 @@ def self.client(peer, settings = CLIENT_SETTINGS)
end

def self.server(peer, settings = SERVER_SETTINGS)
stream = IO::Stream.new(peer, sync: true)
peer.sync = true

server = Server.new(stream)
server = Server.new(peer)

server.read_connection_preface(settings)
server.start_connection
Expand Down
6 changes: 5 additions & 1 deletion lib/async/http/protocol/http2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

require 'async/semaphore'

require 'io/connected'

module Async
module HTTP
module Protocol
Expand Down Expand Up @@ -92,10 +94,12 @@ def read_in_background(parent: Task.current)
begin
while !self.closed?
self.consume_window
Console.debug(self, @framer) {"Reading frame..."}
self.read_frame
end
rescue SocketError, IOError, EOFError, Errno::ECONNRESET, Errno::EPIPE, Async::Wrapper::Cancelled
rescue SocketError, IOError, EOFError, Errno::ECONNRESET, Errno::EPIPE, Async::Wrapper::Cancelled => error
# Ignore.
Console.warn(self, error)
rescue ::Protocol::HTTP2::GoawayError => error
# Error is raised if a response is actively reading from the
# connection. The connection is silently closed if GOAWAY is
Expand Down
2 changes: 1 addition & 1 deletion lib/async/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def accept(peer, address, task: Task.current)
ensure
connection&.close
end

def run
@endpoint.accept(&self.method(:accept))
end
Expand Down
5 changes: 3 additions & 2 deletions test/async/http/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'sus/fixtures/openssl'
require 'sus/fixtures/async/http'
require 'localhost/authority'
require 'io/endpoint/ssl_endpoint'

ABody = Sus::Shared("a body") do
with 'echo server' do
Expand Down Expand Up @@ -97,11 +98,11 @@
let(:client_context) {authority.client_context}

def make_server_endpoint(bound_endpoint)
Async::IO::SSLEndpoint.new(super, ssl_context: server_context)
::IO::Endpoint::SSLEndpoint.new(super, ssl_context: server_context)
end

def make_client_endpoint(bound_endpoint)
Async::IO::SSLEndpoint.new(super, ssl_context: client_context)
::IO::Endpoint::SSLEndpoint.new(super, ssl_context: client_context)
end

it_behaves_like ABody
Expand Down
2 changes: 1 addition & 1 deletion test/async/http/body/pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def aftrer
end

it "returns an io socket" do
expect(io).to be_a(Async::IO::Socket)
expect(io).to be_a(::Socket)
expect(io.read).to be == data
end

Expand Down
4 changes: 2 additions & 2 deletions test/async/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
require 'sus/fixtures/async/http'

describe Async::HTTP::Client do
include Sus::Fixtures::Async::ReactorContext

with 'basic server' do
include Sus::Fixtures::Async::HTTP::ServerContext

Expand All @@ -26,8 +28,6 @@
end

with 'non-existant host' do
include Sus::Fixtures::Async::ReactorContext

let(:endpoint) {Async::HTTP::Endpoint.parse('http://the.future')}
let(:client) {Async::HTTP::Client.new(endpoint)}

Expand Down
4 changes: 2 additions & 2 deletions test/async/http/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end

it "should be connecting to 127.0.0.1" do
expect(subject.endpoint).to be_a Async::IO::SSLEndpoint
expect(subject.endpoint).to be_a ::IO::Endpoint::SSLEndpoint
expect(subject.endpoint).to have_attributes(hostname: be == '127.0.0.1')
expect(subject.endpoint.endpoint).to have_attributes(hostname: be == '127.0.0.1')
end
Expand All @@ -49,7 +49,7 @@
end

it "should be connecting to localhost" do
expect(subject.endpoint).to be_a Async::IO::SSLEndpoint
expect(subject.endpoint).to be_a ::IO::Endpoint::SSLEndpoint
expect(subject.endpoint).to have_attributes(hostname: be == '127.0.0.1')
expect(subject.endpoint.endpoint).to have_attributes(hostname: be == 'localhost')
end
Expand Down
12 changes: 5 additions & 7 deletions test/async/http/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@
expect(proxy.client.pool).to be(:empty?)

proxy.connect do |peer|
stream = Async::IO::Stream.new(peer)
peer.write(data)
peer.close_write

stream.write(data)
stream.close_write

expect(stream.read).to be == data
expect(peer.read).to be == data
end

proxy.close
Expand All @@ -102,7 +100,7 @@
proxy = Async::HTTP::Proxy.tcp(client, "localhost", 1)
expect(proxy.client.pool).to be(:empty?)

stream = Async::IO::Stream.new(proxy.connect)
stream = proxy.connect

stream.write(data)
stream.close_write
Expand Down Expand Up @@ -131,7 +129,7 @@
Console.logger.debug(self) {"Making connection to #{endpoint}..."}

Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
upstream = Async::IO::Stream.new(endpoint.connect)
upstream = endpoint.connect
Console.logger.debug(self) {"Connected to #{upstream}..."}

reader = Async do |task|
Expand Down
4 changes: 2 additions & 2 deletions test/async/http/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
end

def make_server_endpoint(bound_endpoint)
Async::IO::SSLEndpoint.new(super, ssl_context: server_context)
::IO::Endpoint::SSLEndpoint.new(super, ssl_context: server_context)
end

def make_client_endpoint(bound_endpoint)
Async::IO::SSLEndpoint.new(super, ssl_context: client_context)
::IO::Endpoint::SSLEndpoint.new(super, ssl_context: client_context)
end

it "client can get a resource via https" do
Expand Down

0 comments on commit 8fe949d

Please sign in to comment.