class Async::HTTP::URLEndpoint

def self.parse(string, **options)

def self.parse(string, **options)
	url = URI.parse(string).normalize
	
	self.new(url, **options)
end

def address

def address
	endpoint.address
end

def alpn_protocols

def alpn_protocols
	@options.fetch(:alpn_protocols, DEFAULT_ALPN_PROTOCOLS)
end

def bind(*args, &block)

def bind(*args, &block)
	endpoint.bind(*args, &block)
end

def connect(*args, &block)

def connect(*args, &block)
	endpoint.connect(*args, &block)
end

def default_port

def default_port
	secure? ? 443 : 80
end

def each

def each
	return to_enum unless block_given?
	
	self.endpoint.each do |endpoint|
		yield self.class.new(@url, endpoint, @options)
	end
end

def endpoint

def endpoint
	unless @endpoint
		@endpoint = Async::IO::Endpoint.tcp(hostname, port)
		
		if secure?
			# Wrap it in SSL:
			@endpoint = Async::IO::SSLEndpoint.new(@endpoint,
				ssl_context: ssl_context,
				hostname: self.hostname
			)
		end
	end
	
	return @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.fetch(:hostname, @url.hostname)
end

def initialize(url, endpoint = nil, **options)

def initialize(url, endpoint = nil, **options)
	super(**options)
	
	raise ArgumentError.new("URL must be absolute (include scheme, host): #{url}") unless url.absolute?
	
	@url = url
	@endpoint = endpoint
end

def key

def key
	[@url.scheme, @url.userinfo, @url.host, @url.port, @options]
end

def port

def port
	@url.port || default_port
end

def protocol

def protocol
	if secure?
		Protocol::HTTPS
	else
		Protocol::HTTP1
	end
end

def secure?

def secure?
	['https', 'wss'].include?(@url.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
	end
end

def to_s

def to_s
	"\#<#{self.class} #{@url} #{@options.inspect}>"
end