# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require'async/io/host_endpoint'require'async/io/ssl_endpoint'require'async/io/ssl_socket'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# @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?(@url.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<<"?#{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)endenddeftcp_optionsoptions=@options.dupoptions.delete(:scheme)options.delete(:port)options.delete(:hostname)options.delete(:ssl_context)options.delete(:alpn_protocols)options.delete(:protocol)returnoptionsenddefbuild_endpoint(endpoint=nil)endpoint||=Async::IO::Endpoint.tcp(self.hostname,port,tcp_options)ifsecure?# Wrap it in SSL:returnAsync::IO::SSLEndpoint.new(endpoint,ssl_context: self.ssl_context,hostname: @url.hostname,timeout: self.timeout,)endreturnendpointenddefendpoint@endpoint||=build_endpointenddefbind(*args,&block)endpoint.bind(*args,&block)enddefconnect(&block)endpoint.connect(&block)enddefeachreturnto_enumunlessblock_given?self.endpoint.eachdo|endpoint|yieldself.class.new(@url,endpoint,@options)endenddefkey[@url.scheme,@url.userinfo,@url.host,@url.port,@options]enddefeql?otherself.key.eql?other.keyenddefhashself.key.hashendendendend