# frozen_string_literal: true# Released under the MIT License.# 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'async/io/shared_endpoint'require_relative'protocol/http1'require_relative'protocol/https'moduleAsyncmoduleHTTP# Represents a way to connect to a remote HTTP server.classEndpoint<Async::IO::Endpointdefself.parse(string,endpoint=nil,**options)url=URI.parse(string).normalizereturnself.new(url,endpoint,**options)end# Construct an endpoint with a specified scheme, hostname, optional path, and options.defself.for(scheme,hostname,path="/",**options)# TODO: Consider using URI.for once it becomes available:uri_klass=URI.scheme_list[scheme.upcase]||URI::HTTPself.new(uri_klass.new(scheme,nil,hostname,nil,nil,path,nil,nil,nil).normalize,**options)end# @option scheme [String] the scheme to use, overrides the URL scheme.# @option hostname [String] the hostname to connect to (or bind to), overrides the URL hostname (used for SNI).# @option port [Integer] the port to bind to, overrides the URL port.# @option ssl_context [OpenSSL::SSL::SSLContext] the context to use for TLS.# @option alpn_protocols [Array<String>] the alpn protocols to negotiate.definitialize(url,endpoint=nil,**options)super(**options)raiseArgumentError,"URL must be absolute (include scheme, host): #{url}"unlessurl.absolute?@url=urlifendpoint@endpoint=self.build_endpoint(endpoint)else@endpoint=nilendenddefto_urlurl=@url.dupunlessdefault_port?url.port=self.portendreturnurlenddefto_s"\#<#{self.class}#{self.to_url}#{@options}>"enddefinspect"\#<#{self.class}#{self.to_url}#{@options.inspect}>"endattr:urldefaddressendpoint.addressenddefsecure?['https','wss'].include?(self.scheme)enddefprotocol@options.fetch(:protocol)doifsecure?Protocol::HTTPSelseProtocol::HTTP1endendenddefdefault_portsecure??443:80enddefdefault_port?port==default_portenddefport@options[:port]||@url.port||default_portend# The hostname is the server we are connecting to:defhostname@options[:hostname]||@url.hostnameenddefscheme@options[:scheme]||@url.schemeenddefauthority(ignore_default_port=true)ifignore_default_portanddefault_port?@url.hostnameelse"#{@url.hostname}:#{port}"endend# Return the path and query components of the given URL.defpathbuffer=@url.path||"/"ifquery=@url.querybuffer="#{buffer}?#{query}"endreturnbufferenddefalpn_protocols@options.fetch(:alpn_protocols){self.protocol.names}enddeflocalhost?@url.hostname=~/^(.*?\.)?localhost\.?$/end# We don't try to validate peer certificates when talking to localhost because they would always be self-signed.defssl_verify_modeifself.localhost?OpenSSL::SSL::VERIFY_NONEelseOpenSSL::SSL::VERIFY_PEERendenddefssl_context@options[:ssl_context]||OpenSSL::SSL::SSLContext.new.tapdo|context|ifalpn_protocols=self.alpn_protocolscontext.alpn_protocols=alpn_protocolsendcontext.set_params(verify_mode: self.ssl_verify_mode)endenddefbuild_endpoint(endpoint=nil)endpoint||=tcp_endpointifsecure?# Wrap it in SSL:returnAsync::IO::SSLEndpoint.new(endpoint,ssl_context: self.ssl_context,hostname: @url.hostname,timeout: self.timeout,)endreturnendpointenddefendpoint@endpoint||=build_endpointenddefendpoint=(endpoint)@endpoint=build_endpoint(endpoint)enddefbind(*arguments,&block)endpoint.bind(*arguments,&block)enddefconnect(&block)endpoint.connect(&block)enddefeachreturnto_enumunlessblock_given?self.tcp_endpoint.eachdo|endpoint|yieldself.class.new(@url,endpoint,**@options)endenddefkey[@url,@options]enddefeql?otherself.key.eql?other.keyenddefhashself.key.hashendprotecteddeftcp_optionsoptions=@options.dupoptions.delete(:scheme)options.delete(:port)options.delete(:hostname)options.delete(:ssl_context)options.delete(:alpn_protocols)options.delete(:protocol)returnoptionsenddeftcp_endpointAsync::IO::Endpoint.tcp(self.hostname,port,**tcp_options)endendendend