lib/httpx/connection.rb in httpx-0.22.5 vs lib/httpx/connection.rb in httpx-0.23.0
- old
+ new
@@ -27,22 +27,19 @@
# to, provided that the IP is the same and the port and scheme as well. This will allow to
# share the same socket to send HTTP/2 requests to different hosts.
#
class Connection
extend Forwardable
- include Registry
include Loggable
include Callbacks
using URIExtensions
using NumericExtensions
require "httpx/connection/http2"
require "httpx/connection/http1"
- BUFFER_SIZE = 1 << 14
-
def_delegator :@io, :closed?
def_delegator :@write_buffer, :empty?
attr_reader :type, :io, :origin, :origins, :state, :pending, :options
@@ -55,19 +52,19 @@
@type = type
@origins = [uri.origin]
@origin = Utils.to_uri(uri.origin)
@options = Options.new(options)
@window_size = @options.window_size
- @read_buffer = Buffer.new(BUFFER_SIZE)
- @write_buffer = Buffer.new(BUFFER_SIZE)
+ @read_buffer = Buffer.new(@options.buffer_size)
+ @write_buffer = Buffer.new(@options.buffer_size)
@pending = []
on(:error, &method(:on_error))
if @options.io
# if there's an already open IO, get its
# peer address, and force-initiate the parser
transition(:already_open)
- @io = IO.registry(@type).new(@origin, nil, @options)
+ @io = build_socket
parser
else
transition(:idle)
end
@@ -82,11 +79,11 @@
# to initiate the io object.
def addresses=(addrs)
if @io
@io.add_addresses(addrs)
else
- @io = IO.registry(@type).new(@origin, addrs, @options)
+ @io = build_socket(addrs)
end
end
def addresses
@io && @io.addresses
@@ -102,11 +99,11 @@
@origins.include?(uri.origin) &&
# if there is more than one origin to match, it means that this connection
# was the result of coalescing. To prevent blind trust in the case where the
# origin came from an ORIGIN frame, we're going to verify the hostname with the
# SSL certificate
- (@origins.size == 1 || @origin == uri.origin || (@io && @io.verify_hostname(uri.host)))
+ (@origins.size == 1 || @origin == uri.origin || (@io.is_a?(SSL) && @io.verify_hostname(uri.host)))
) && @options == options
) || (match_altsvcs?(uri) && match_altsvc_options?(uri, options))
end
def mergeable?(connection)
@@ -116,11 +113,11 @@
return false unless connection.addresses
(
(open? && @origin == connection.origin) ||
- !(@io.addresses & connection.addresses).empty?
+ !(@io.addresses & (connection.addresses || [])).empty?
) && @options == connection.options
end
# coalescable connections need to be mergeable!
# but internally, #mergeable? is called before #coalescable?
@@ -451,11 +448,11 @@
transition(:active)
end
def build_parser(protocol = @io.protocol)
- parser = registry(protocol).new(@write_buffer, @options)
+ parser = self.class.parser_type(protocol).new(@write_buffer, @options)
set_parser_callbacks(parser)
parser
end
def set_parser_callbacks(parser)
@@ -594,10 +591,21 @@
@io.close if @io
@read_buffer.clear
remove_instance_variable(:@timeout) if defined?(@timeout)
end
+ def build_socket(addrs = nil)
+ transport_type = case @type
+ when "tcp" then TCP
+ when "ssl" then SSL
+ when "unix" then UNIX
+ else
+ raise Error, "unsupported transport (#{@type})"
+ end
+ transport_type.new(@origin, addrs, @options)
+ end
+
def on_error(error)
if error.instance_of?(TimeoutError)
if @total_timeout && @connected_at &&
Utils.elapsed_time(@connected_at) > @total_timeout
@@ -661,8 +669,19 @@
return if response && response.finished?
@write_buffer.clear
error = error_type.new(request, request.response, read_timeout)
on_error(error)
+ end
+
+ class << self
+ def parser_type(protocol)
+ case protocol
+ when "h2" then HTTP2
+ when "http/1.1" then HTTP1
+ else
+ raise Error, "unsupported protocol (##{protocol})"
+ end
+ end
end
end
end