class Async::HTTP::Endpoint
Represents a way to connect to a remote HTTP server.
def self.for(scheme, hostname, **options)
def self.for(scheme, hostname, **options) # TODO: Consider using URI.for once it becomes available: uri_klass = URI.scheme_list[scheme.upcase] || URI::HTTP self.new( uri_klass.new(scheme, nil, hostname, nil, nil, nil, nil, nil, nil), **options ) end
def self.parse(string, endpoint = nil, **options)
def self.parse(string, endpoint = nil, **options) url = URI.parse(string).normalize return self.new(url, endpoint, **options) end
def address
def address endpoint.address end
def alpn_protocols
def alpn_protocols @options.fetch(:alpn_protocols) {self.protocol.names} end
def authority(ignore_default_port = true)
def authority(ignore_default_port = true) if ignore_default_port and default_port? @url.hostname else "#{@url.hostname}:#{port}" end end
def bind(*args, &block)
def bind(*args, &block) endpoint.bind(*args, &block) end
def build_endpoint(endpoint = nil)
def build_endpoint(endpoint = nil) endpoint ||= tcp_endpoint if secure? # Wrap it in SSL: return Async::IO::SSLEndpoint.new(endpoint, ssl_context: self.ssl_context, hostname: @url.hostname, timeout: self.timeout, ) end return endpoint end
def connect(&block)
def connect(&block) endpoint.connect(&block) end
def default_port
def default_port secure? ? 443 : 80 end
def default_port?
def default_port? port == default_port end
def each
def each return to_enum unless block_given? self.tcp_endpoint.each do |endpoint| yield self.class.new(@url, endpoint, **@options) end end
def endpoint
def endpoint @endpoint ||= build_endpoint end
def eql? other
def eql? other self.key.eql? other.key end
def hash
def hash self.key.hash end
def hostname
def hostname @options[:hostname] || @url.hostname end
def initialize(url, endpoint = nil, **options)
(**alpn_protocols)
-
the
(Array
) -- alpn protocols to negotiate. -
the
(OpenSSL::SSL::SSLContext
) -- context to use for TLS. -
the
(Integer
) -- port to bind to, overrides the URL port. -
the
(String
) -- hostname to connect to (or bind to), overrides the URL hostname (used for SNI). -
the
(String
) -- scheme to use, overrides the URL scheme.
def initialize(url, endpoint = nil, **options) super(**options) raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute? @url = url if endpoint @endpoint = self.build_endpoint(endpoint) else @endpoint = nil end end
def inspect
def inspect "\#<#{self.class} #{self.to_url} #{@options.inspect}>" end
def key
def key [@url, @options] end
def localhost?
def localhost? @url.hostname =~ /^(.*?\.)?localhost\.?$/ end
def path
def path buffer = @url.path || "/" if query = @url.query buffer = "#{buffer}?#{query}" end return buffer end
def port
def port @options[:port] || @url.port || default_port end
def protocol
def protocol @options.fetch(:protocol) do if secure? Protocol::HTTPS else Protocol::HTTP1 end end end
def scheme
def scheme @options[:scheme] || @url.scheme end
def secure?
def secure? ['https', 'wss'].include?(self.scheme) end
def ssl_context
def ssl_context @options[:ssl_context] || ::OpenSSL::SSL::SSLContext.new.tap do |context| if alpn_protocols = self.alpn_protocols context.alpn_protocols = alpn_protocols end context.set_params( verify_mode: self.ssl_verify_mode, min_version: ::OpenSSL::SSL::TLS1_2_VERSION, ) end end
def ssl_verify_mode
def ssl_verify_mode if self.localhost? OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end end
def tcp_endpoint
def tcp_endpoint Async::IO::Endpoint.tcp(self.hostname, port, **tcp_options) end
def tcp_options
def tcp_options options = @options.dup options.delete(:scheme) options.delete(:port) options.delete(:hostname) options.delete(:ssl_context) options.delete(:alpn_protocols) options.delete(:protocol) return options end
def to_s
def to_s "\#<#{self.class} #{self.to_url} #{@options}>" end
def to_url
def to_url url = @url.dup unless default_port? url.port = self.port end return url end