lib/async/http/endpoint.rb in async-http-0.45.9 vs lib/async/http/endpoint.rb in async-http-0.46.0
- old
+ new
@@ -25,15 +25,16 @@
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
def self.parse(string, **options)
url = URI.parse(string).normalize
- self.new(url, **options)
+ return self.new(url, nil, **options)
end
# @option scheme [String] the scheme to use, overrides the URL scheme.
# @option port [Integer] the port to bind to, overrides the URL port.
# @option hostname [String] the hostname to use, overrides the URL hostname.
@@ -65,25 +66,26 @@
def inspect
"\#<#{self.class} #{self.to_url} #{@options.inspect}>"
end
attr :url
- attr :options
def address
endpoint.address
end
def secure?
['https', 'wss'].include?(@url.scheme)
end
def protocol
- if secure?
- Protocol::HTTPS
- else
- Protocol::HTTP1
+ @options.fetch(:protocol) do
+ if secure?
+ Protocol::HTTPS
+ else
+ Protocol::HTTP1
+ end
end
end
def default_port
secure? ? 443 : 80
@@ -122,22 +124,21 @@
end
return buffer
end
- DEFAULT_ALPN_PROTOCOLS = ['h2', 'http/1.1'].freeze
-
def alpn_protocols
- @options[:alpn_protocols] || DEFAULT_ALPN_PROTOCOLS
+ @options.fetch(:alpn_protocols) {self.protocol.names}
end
- LOCALHOST = 'localhost'.freeze
+ def localhost?
+ self.hostname =~ /^(.*?\.)?localhost\.?$/
+ end
# We don't try to validate peer certificates when talking to localhost because they would always be self-signed.
def ssl_verify_mode
- case self.hostname
- when LOCALHOST
+ if self.localhost?
OpenSSL::SSL::VERIFY_NONE
else
OpenSSL::SSL::VERIFY_PEER
end
end
@@ -160,9 +161,10 @@
options.delete(:scheme)
options.delete(:port)
options.delete(:hostname)
options.delete(:ssl_context)
options.delete(:alpn_protocols)
+ options.delete(:protocol)
return options
end
def build_endpoint(endpoint = nil)
\ No newline at end of file